#### Lab 9: Turtle graphics

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

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.

##### 1` `Making trips

; 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])

(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

; 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])

50 * sin(dir * pi / 180)

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.

##### 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?

(check-expect (prefix-all "Thanks, " (list "Daniel" "Alicia")) (list "Thanks, Daniel" "Thanks, Alicia"))

##### 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—

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

##### 5.4` `Challenge: build-list

(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?

(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.

(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])))

(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.