Best way of drawing with turtles

Hi, I have to teach functional programming and I chose Scheme as the main language I will use for that course. I chose DrRacket as the IDE my students will use for obvious practical reasons.

I will stick to R5RS and will not really use racket-specific features except one day: since the students will use DrRacket anyway, and since using recursive functions for drawing fractals is something I find interesting in my teaching, I investigated a bit about a convenient way of using turtles in Racket.

If I understand well, there are two ways of doing it:

  • without installing anything more, just typing something like:
(require (lib "turtles.ss" "graphics"))
(turtles)
(draw 100)
  • by installing something called teachpacks (my students will have to type raco pkg install teachpacks) and then doing as:
(require teachpacks/racket-turtle)
(draw
   (list
      (forward 100)))

(and more generally using the documentation at Racket Turtle)

Is one of these ways better than the other? Of course, for the second one, my students will have to install the library, but I don't think they will get in any trouble. The documentation looks better for that second way, but since I will only draw lines, it doesn't make a big difference either.

Small comment: For the first example I prefer

(require graphics/turtles)
(turtles)
(draw 100)

I'm not sure if there is a difference, but it looks nicer.

[And more importantly, I don't know if the second approach is better or worse.]

If you have the main-distribution package, which most installations do, then you have the htdp package. That provides the graphics/turtles and related modules at https://docs.racket-lang.org/turtles/index.html. No installation required.

In #lang racket and derivatives, using (require graphics/turtles) is preferred. Of course, this is not Scheme.

The HTDP turtles library has both "imperative" and "functional" turtles. The teachpack library has what appears to be a rather different set of drawing functions. In the student languages, you can use the "Add Teachpack" menu item from "Languages." (But it gives an error in any other configuration.)

1 Like

Thank you for your answer. I was not aware of the functional turtles which look great! I could easily use them from the IDE, but I couldn't figure out how to display the final rendering when running my script from the cli. I only get (object:turtle-snip% ...) on my terminal.

Can you share the program? With the regular turtles I can’t reproduce it.

From the docs, I would try something like

(define my-turtle …)
(require pict racket/gui)
(show-pict (turtle-pict my-turtle))

DrRacket does the “right thing” with snip objects.

You could also try running the program with gracket, but I doubt it makes a difference.

I tried to display my drawing from the CLI again by using your previous snippet of code, but then I get:

koch_functional_turtle.rkt:21:12: turtle-pict: unbound identifier
  in: turtle-pict
  location...:
   koch_functional_turtle.rkt:21:12

Do I have to import turtle-pict?

Yes.

(require graphics/value-turtles)

There was a missing "s" in the snippet of code above; I finally could make it work. It should be spelled:

(define my-turtle …)
(require pict racket/gui)
(show-pict (turtles-pict my-turtle))
2 Likes

Oops :slight_smile: glad you caught that