Description
Accumulator Practice
Exercise (Reviewed) 1 Design the function digits>num which takes a nonempty list of digits (i.e. natural numbers between 0 and 9) and combines all the digits into a single number in base10. For example:
(checkexpect (digits>num (list 1 2 3)) 123)
Exercise (Reviewed) 2 Design the function longeststreak which takes in a list of natural numbers and computes the largest number of times a single value appears in the list in a row. (The actual repeated value doesn’t matter; we only care how many times it repeats.) For example:

(checkexpect (longeststreak (list 2 2 2)) 3)
(checkexpect (longeststreak (list 1 2 1 2)) 1)
(checkexpect (longeststreak (list 1 1 2 2 2 3 4 4)) 3)
(checkexpect (longeststreak (list 1 2 2 2 2 3 4 4 4 4 5)) 4)
EtchaSketch
In this section of the lab we will create a simple linedrawing program. The program starts with a blank canvas and the user can draw lines of a fixed size by pressing the arrow keys.
Step 1: What stays the same?
For your convenience we have provided some constants to use below. You can modify them or add to them if you find that you need to keep track of something else.

(require 2htdp/image)
(define WIDTH 600)
(define HEIGHT 600)
(define BACKGROUND (emptyscene WIDTH HEIGHT))
(define LINELENGTH 10)
(define LINECOLOR “black”)
Step 2: What changes?
To keep track of what changes we will keep a list of all the directional keys the user has pressed. Here is a data definition for a direction:

; – “left”
; – “right”
; – “up”
; – “down”
; and represents one of the 4 possible directions
Exercise 3 Define the template for functions that take in a Dir.
Step 3: Which handlers do we need?
Exercise 4 Write down the signatures and purpose statements for the handler functions you need. This is your “wishlist” of functions that you will need to create. Keep in mind that we will need to use a list of Dirs as our world state.
Step 4: Design your handlers
We will start by designing the function that draws the lines in the given directions. However, let’s break this down into some simpler functions first:
Exercise 5 Design the function moveposn that takes a Posn, a Dir, and a Number and produces that position shifted by the given amount in the given direction. For example, (moveposn (makeposn 1 2) “left” 3) would produce (makeposn 2 2).
Exercise 6 Design the function drawfromstart, which takes a [Listof Dir], a Posn, and an Image. It then adds lines starting at the given position and going in each direction. Each line should start where the last one ended. You can use the predefined function addline to add lines to an image. Note: You should not use a list abstraction for this problem.
Exercise 7 Use drawfromstart to design the function for your todraw clause.
Now we need to design the onkey handler. When a user presses an arrow key it should add a direction to the end of the list of directions.
Exercise 8 Design the function for your onkey clause. When an arrow key is pressed, add the appropriate direction to the end of the list so far. When another key is pressed, nothing should happen.
Step 5: Put it all together!
Exercise 9 Design the function etchasketch which runs your linedrawing program.
Generating Fractals
For the next part of the lab you will design functions to draw (and iterate) something called the dragon curve which is a fractal. Put simply, a fractal is just an image that is the same at various levels of detail. We will make use of the functions we wrote for our linedrawing program in order to develop this program.
Step 1: What stays the same?
We should be able to use the same constants as we had in our linedrawing program so there is nothing further we need to do for this step.
Step 2: What changes?
We will use a natural number to keep track of the number of iterations of the fractal to show. When someone presses the up arrow we will increase the number, and when someone presses the down arrow we will decrease the number (unless it is already zero). Since a NaturalNumber is atomic data there is no need for us to design any new data.
Step 3: Which handlers do we need?
Like our linedrawing program we will need a drawing function and a function that can handle keyboard inputs so that you can increase or decrease the number of iterations by pressing the arrow keys.
Step 4: Design your handlers
We will start by designing the function that draws the fractal. Remember that all we are given is the number of iterations. Therefore the first thing we need to do is find out what lines to draw.
Exercise 10 Design the function rotatedir which takes a Dir and rotates it 90 degrees counterclockwise.
Exercise 11 Design the function rotatealldirs which takes a [Listof Dir] and rotates every Dir in the given list 90 degrees counterclockwise.
Exercise 12 Design the function generatefractaldirs which takes a natural number (representing the number of iterations left to draw) and a list of Dirs and performs the following algorithm:

If the number is zero, return the list unchanged.

If the number is positive, return a new modified list as follows:

Rotate every Dir in the input list

Reverse the list of rotated directions

Append this rotated/reversed list to the end of the input list

Recursively call the function with one less iteration

Exercise 13 Design the function for the todraw clause which takes a natural number and draws that many iterations of the dragon fractal using the generatefractaldirs function and the drawing function from our linedrawing program. To start, you should give generatefractaldirs the initial list (list “down”).
Exercise 14 Design the function for the onkey clause. Remember that your world state is a natural number. This number should increase by one if the up arrow key is pressed and decrease by one if the down arrow key is pressed. Otherwise nothing will happen.
Step 5: Put it all together!
Exercise 15 Design the function dragonfractal which takes an initial number of iterations and runs the dragon fractal program. Play around with it but don’t be surprised if the program breaks after about 10 iterations (the image becomes too large so you may get something unexpected).