On this page:
1 Making trips
2 Measuring trips
3 Drawing trips
4 Repeating trips
5 More midterm review
5.1 Filter
5.2 Map
5.3 Fold
5.4 Challenge:   build-list
8.5

Lab 9: Turtle graphics

Use the “Intermediate Student with lambda” language on this and further assignments in this course.

In this lab (and some future assignments), you’ll draw pictures by telling a turtle carrying a pen to move and turn. For example, to draw a square, the turtle can follow these instructions:
  • Move forward by a distance of 50.

  • Turn left 90 degrees.

  • Move forward by a distance of 50.

  • Turn left 90 degrees.

  • Move forward by a distance of 50.

  • Turn left 90 degrees.

  • Move forward by a distance of 50.

This way of drawing pictures is called turtle graphics.

1 Making trips

Start with these data and structure definitions:
; A Trip is [ListOf Step]
 
; A Step is one of:
; - (make-draw Number)
; - (make-turn Number)
; *Interpretation*: angle is how many degrees to turn left (counterclockwise)
(define-struct draw [distance])
(define-struct turn [angle])

Below are two example trips. The first example is the instructions above for drawing a square. The second example makes a shape like the letter Z.
(define square-trip
  (list (make-draw 50)
        (make-turn 90)
        (make-draw 50)
        (make-turn 90)
        (make-draw 50)
        (make-turn 90)
        (make-draw 50)))
(define z-trip
  (list (make-draw 80)
        (make-turn -135)
        (make-draw 120)
        (make-turn 135)
        (make-draw 80)))

Exercise 1. Define two more examples of Trips. Call them trip1 and trip2.

Exercise 2. Write the template process-step for a function that processes a Step. Then write the template process-trip for a function that processes a Trip.

Exercise 3. Point out where the data definition for Trip refers to the data definitions for Step and Trip. (Hint: expand the definition of ListOf.) Does your template process-trip refer to your templates process-step and process-trip in the corresponding places? It should.

2 Measuring trips

Exercise 4. Design a function step-length that computes the length of a Step. In other words, this function should compute how much ink is used by the given Step. Turning doesn’t draw anything, so the length of a turn is 0.

Exercise 5. Design a function trip-length that computes the length of a Trip. In other words, this function should compute how much ink is used by the given Trip. That is the total amount of ink used by all the Steps in the Trip. Follow the template, so use step-length and trip-length.

3 Drawing trips

The purpose of this section is to draw a Trip on an Image. The following data definition and structure definition let us keep track of where the turtle is and which way it is facing:
; A Turtle is (make-turtle Number Number Number)
; *Interpretation*: dir=0 faces east,
;                   dir=90 faces north,
;                   dir=180 faces west,
;                   dir=270 faces south
(define-struct turtle [x y dir])

Exercise 6. Design a function move that takes a Step and a Turtle before taking the step and computes the Turtle after taking the step. For example, if the turtle is currently facing north, then taking the step (make-turn 45) should turn the turtle to face northwest, whereas taking the step (make-turn -90) should turn the turtle to face east. To take another example, if the turtle is currently facing north, then taking the step (make-draw 50) should decrease the y coordinate by 50. More generally, if the turtle is currently facing dir, then taking the step (make-draw 50) should decrease the y coordinate by

50 * sin(dir * pi / 180)

and increase the x coordinate by

50 * cos(dir * pi / 180)

Exercise 7. Design a function draw-step that draws a given Step taken by a given Turtle on a given Image. Use the function scene+line provided by the 2htdp/image library in your examples and your definition; choose your favorite color. Also use the function move you just designed. Recall that turning doesn’t draw anything.

Exercise 8. Now design a function draw-trip that draws a given Trip taken by a given Turtle on a given Image. Use the functions move and draw-step you just designed.

Try draw-trip on example trips such as square-trip and z-trip.

Save your lab work for use next time! If you submit it using Handin as assignment lab9, you will be able to retrieve it from anywhere.

4 Repeating trips

Let’s make some pretty pictures.

Exercise 9. Design a function repeat that takes a NaturalNumber and a Trip and returns a new Trip that repeats the given Trip the given number of times. Hint: Follow the template for processing a NaturalNumber, and use append. This function actually works not just for trips but also for other lists.

Now this trip should be a hexagon:
(define hexagon-trip
  (repeat 6 (list (make-draw 50) (make-turn 60))))
And this trip should look like a ring:
(define ring-trip
  (repeat 36 (list (make-draw 100) (make-turn 130))))

5 More midterm review

Exercise 10. Recall the data definition of Mobile from Problem set 6: Unions and recursion. Design the function count-leaves which counts how many leaves a Mobile has.

Exercise 11. Design the function count-big-leaves which counts how many leaves of weight more than 10 a Mobile has.

5.1 Filter

Exercise 12. Design a function remove-multiples-of-10 that removes every number divisible by 10 from a list of numbers. Use filter.

Hint: Study the signature and purpose of filter in Figure 95 of the textbook. What is X?

Exercise 13. Design a function has-multiple? that takes a list of numbers and a number as inputs, and checks whether any number in the given list is a multiple of the given number. Use ormap.

Hint: Study the signature and purpose of ormap in Figure 95 of the textbook. What is X?

5.2 Map

Exercise 14. Design a function move-invaders which takes a list of Posns and increases each Y coordinate by 1. Use map.

Hint: Study the signature and purpose of map in Figure 95 of the textbook. What is X and what is Y?

Exercise 15. Design a function prefix-all which takes a string and a list of strings, and puts the given string in front of every string in the given list.
(check-expect (prefix-all "Thanks, " (list "Daniel" "Alicia"))
              (list "Thanks, Daniel" "Thanks, Alicia"))
Use map.

5.3 Fold

Exercise 16. Design a function count which accepts a list of strings and uses foldr to count the number of items in the list. You may not use the built-in length function in this exercise. Instead, design a helper function and give it to foldr, which will perform the counting.

Hint 1: Study the signature and purpose of foldr in Figure 96 of the textbook. What is X and what is Y? Before you define the helper function, remember to write down its signature, purpose, and examples.

Hint 2: When foldr is used to sum together a list of numbers, the helper function + gets two inputs—the first element of a list, and the sum of the rest of the list—and returns the sum of the entire list. When foldr is used to concatenate together a list of strings, the helper function string-append gets two inputs—the first element of a list, and the concatenation of the rest of the list—and returns the concatenation of the entire list. Similarly, when you use foldr to find the length of a list, your helper function will get two inputs—the first element of a list, and the length of the rest of the list—and should return the length of the entire list. In each instance, the result (sum or concatenation or length) of the rest of the list is already computed, just like when you are filling in a list-processing template.

Exercise 17. Abstract your count function to work with a list of any data type.

5.4 Challenge: build-list

Exercise 18. Anticipate (then determine) the values of the following expressions.
(build-list 10 sqr)
(build-list 10 (lambda (x) x))
(build-list 10 add1)
(build-list 10 (lambda (x) (+ 1 x)))
(build-list 10 (lambda (x) 1))
(build-list 5 (lambda (x) (/ x 10)))

Hint: Study the signature and purpose of build-list in Figure 95 of the textbook. There, N means NaturalNumber. But what is X?

Exercise 19. Design a function evens-first that takes a natural number as input and returns the list of the first that many even numbers starting with 0.
(check-expect (evens-first 4)
              (list 0 2 4 6))
(check-expect (evens-first 7)
              (list 0 2 4 6 8 10 12))

Hint: Study the signature and purpose of build-list in Figure 95 of the textbook. What is X? Before you define the helper function to pass to build-list, remember to write down its signature, purpose, and examples.

Exercise 20. Anticipate (then determine) the values of the following expressions.
(build-list 10 (lambda (x) (cond [(= x 3) 1] [else 0])))
(build-list 10 (lambda (x) (cond [(= x 4) 1] [else 0])))
(build-list 10 (lambda (x) (cond [(= x 5) 1] [else 0])))

Exercise 21. Design a function diagonal that receives one input, a number, and returns a list of that many lists of 0 and 1 in the following diagonal arrangement:
(check-expect (diagonal 3)
              (list (list 1 0 0)
                    (list 0 1 0)
                    (list 0 0 1)))
(check-expect (diagonal 10)
              (list (list 1 0 0 0 0 0 0 0 0 0)
                    (list 0 1 0 0 0 0 0 0 0 0)
                    (list 0 0 1 0 0 0 0 0 0 0)
                    (list 0 0 0 1 0 0 0 0 0 0)
                    (list 0 0 0 0 1 0 0 0 0 0)
                    (list 0 0 0 0 0 1 0 0 0 0)
                    (list 0 0 0 0 0 0 1 0 0 0)
                    (list 0 0 0 0 0 0 0 1 0 0)
                    (list 0 0 0 0 0 0 0 0 1 0)
                    (list 0 0 0 0 0 0 0 0 0 1)))

Hint: Study the signature and purpose of build-list in Figure 95 of the textbook. What is X? Before you define each helper function to pass to build-list, remember to write down its signature, purpose, and examples.