On this page:
Hypnosis
Before You Go...
Fun(ctions) with Nats
6.6

Lab 5 The Magic of Recursion

lab!

Purpose: The purpose of this lab is to you some hands-on experience with lists.

Textbook references: Chapter 9: Designing with Self-Referential Data Definitions

Hypnosis

James Braid is trying to perform hypnosis on one of his patients but he needs your help. He has asked you to develop a program which shows a set of concentric rings, growing and shrinking. The user will provide to the program the size of the circle in the center, and each tick the program will add a new ring, of a random size and color. When the screen is full the rings will disappear one by one until only the inner circle remains. Then the process will start over again. The TAs will demonstrate the program now.

James has given you this helpful function which converts a number into one of seven different colors.

; choose-color : Nat -> Color
; Choose the color corresponding with the given number
(check-expect (choose-color 0) "red")
(check-expect (choose-color 1) "orange")
(check-expect (choose-color 2) "yellow")
(check-expect (choose-color 3) "green")
(check-expect (choose-color 4) "blue")
(check-expect (choose-color 5) "purple")
(check-expect (choose-color 6) "pink")
(define (choose-color n)
  (cond [(= n 0) "red"]
        [(= n 1) "orange"]
        [(= n 2) "yellow"]
        [(= n 3) "green"]
        [(= n 4) "blue"]
        [(= n 5) "purple"]
        [else "pink"]))

Exercise 1 How can you use the function above to generate a random color? If you are having trouble please ask a staff member for assistance.

Exercise 2 Design the world program described above. Your program should take as input the size of the inner circle, nothing more. However, you may need to keep track of more than this in your world state. Remember to follow the steps for designing world programs (check Piazza or the textbook if you can’t recall what these steps are).

Before You Go...

If you had trouble finishing any of the exercises in the lab or homework, or just feel like you’re struggling with any of the class material, please feel free to come to office hours and talk to a TA or tutor for additional assistance. We love to teach and you will learn. It’s symbiotic!

Fun(ctions) with Nats

; A Nat is one of:
; - 0
; (add1 Nat)

Exercise 3 Create a template for the data definition given above. How can we design the template to showcase the unique recursive properties of natural numbers? If you are struggling with this please see the design recipe page on the course website.

Exercise 4 Design the function double which takes a Nat and returns the double of that Nat. You should not use + or * but you can use add1.

Exercise 5 Design the function even-nat? which takes a Nat and returns #true if the Nat is an even number. Yes, zero is even. You may not use even? or odd? to write this function.

Exercise 6 Design the function nat+ which takes two Nats and returns their sum. You may not use + to write this function.

Exercise 7 Design the function nat* which takes two Nats and returns their product. You may not use * or + to write this function but you may use nat+.

Exercise 8 Design the function factorial which gives the factorial of a Nat. You may use only the functions you designed in the last 4 exercises when writing this function.