8.7

### Lab 7: Lists

Note: Whenever you design or write a function, you need to follow the design recipe.

#### 1Lists of items

Do this section in groups of at least 2 people.

Your food-truck business already depends on shared services such as roads and sanitation. It can also take advantage of built-in list structures to let customers place orders that consist of any number of items.

Exercise 1. Complete the following data definition for an Order, by write a new data definition for an Item:
 ; An Order is one of: ; - empty ; - (cons Item Order) ; An Item is ???
You can make Item any kind of data definition: it could be
• an atomic thing like a Number,

• a range like a Number between 6 and 12,

• an enumeration like a TrafficLight,

• a structure like a Person,

• a union like a Rocket, or

• a recursive union like a TrainOfWagons.

Exercise 2. Define at least 3 examples of Orders. Define as many examples as it takes to make use of every line of your data definitions.

Exercise 3. Write the template process-order for a function that processes an Order, as well as the other templates for processing the other data definitions you introduced.
• Point out where the data definition for Order refers to the data definition for Item. Does your template process-order refer to your template process-item in the corresponding place? It should.

• Point out where the data definition for Order refers to itself. Does your template process-order refer to itself in the corresponding place? It should.

Exercise 4. Feel free to do this exercise and the next exercise in parallel. However, it is very important that y’all use the same data definitions that y’all just wrote.

Design a function price-item that takes an Item and returns its price (a number).

Then, design a function price-order that takes an Order and returns its price (a number). For example, the price of empty should be 0. Guided by the template, the function price-order should use the function price-item and itself.

Exercise 5. Design a function draw-item that takes an Item and returns a crude image of the food.

Then, design a function draw-order that takes an Order and returns a crude image of the foods. For example, the drawing of empty should be a tray or bag or plate or glass with nothing. Guided by the template, the function draw-order should use the function draw-item and itself.

Exercise 6. Go visit another group and try out their menu:
1. Read their data definition carefully, but don’t bother reading the rest of their code.

2. Make an Order. Make sure to obey their data definition for what an Order is.

3. Give your order to the draw-order function in their Interactions Window. Is it appetizing?

4. Give the same order to the price-order function in their Interactions Window. Is it worth it?

One important topic studied in computer science is how to send secret messages without being found out. For example, a voter should be able to send a secret vote to a counting machine. To make sure nobody else can read the secret message, we send a different, corresponding message that nobody else can read. This is called encrypting the secret message. Recovering the secret message from the encrypted message is called decrypting it.

A basic method to encrypt a secret message is to replace some letters with other letters. This method is called a substitution cipher. ROT13 (“rotate by 13 places”) is a simple substitution cipher that replaces a letter with the 13th letter away from it in the alphabet.

So, for example, "a" would be replaced with "n", "O" would be replaced with "B", and so on. We can write secret messages using this cipher, and will be decrypting some secret messages in this lab.

Exercise 7. Here we provide a data definition for a 1String, and a definition for rot13. Given a 1String, rot13 checks if it is a lowercase letter between "a" and "n", if so, it will add 13 to the integer representation of the 1String, which results in the 13th letter after it in the alphabet. Similarly, rot13 checks if is a lowercase letter between "n" and "z", and subtracts 13 to get the 13th letter before it in the alphabet. rot13 does the same thing for uppercase letters. The else case handles 1Strings like " " and ".", which don’t rotate.
 ; A 1String is a String of length 1 ; rot13 : 1String -> 1String ; returns the letter 13 spaces ahead in the alphabet (define (rot13 letter) (cond [(or (and (string<=? "a" letter) (string<=? letter "m")) (and (string<=? "A" letter) (string<=? letter "M"))) (int->string (+ (string->int letter) 13))] [(or (and (string<=? "n" letter) (string<=? letter "z")) (and (string<=? "N" letter) (string<=? letter "Z"))) (int->string (- (string->int letter) 13))] [else letter]))

Write tests for the rot13 function. Make sure to test every possible case of the cond clause.

Exercise 8. Recall data definitions for lists from class. Write down the data definition for a ListOf1Strings. Define three examples of it. Write down the template for a function that processes a ListOf1Strings; make it look like a function called process-1strings, and do not put it in a comment.

Exercise 9. Design a function combine-1strings which takes a ListOf1Strings and returns it as a single String. combine-1strings should return "" if the ListOf1Strings is empty.

Exercise 10. Download the following five text files into the same folder as the file where you are completing this assignment:

Go ahead and try to read the files. Looks like a bunch of gibberish! Let’s get to cracking these messages.

Exercise 11. Design a function convert-to-r13 which takes a ListOf1Strings and returns a ListOf1Strings with each 1String in the list converted to its rotated representation.

Exercise 12. Design a function file-to-r13 which takes a String (a file name) and returns the file as a ListOf1Strings with each 1String converted to its rotated representation.

You will need to use the 2htdp/batch-io library to examine the contents of files. You should use its read-1strings function to create a ListOf1Strings from a text file.

Use the files you downloaded as tests. They should start looking less like gibberish!

Exercise 13. Finally, put it all together. Design a function decrypt-file which takes a String (a file name) and returns the contents as a single String converted to its rotated representation. Remember combine-1strings from Exercise 9.

Now you should be able to read the secret messages! Terng wbo!

#### 3Writing our own secret messages

Exercise 14. Design a function encrypt-string which takes a String and returns the rotated representation of the String. Use the built-in function explode, which takes a String and converts it to a ListOf1Strings.

Exercise 15. Design a function encrypt-string-to-file that takes two Strings (a message to encrypt and a file name) and writes the encrypted message to the given file name. Use the write-file function from the 2htdp/batch-io library. The write-file function returns the given file name, and your function should also return the given file name.

Finally, make sure you can read your own secret messages using decrypt-file.

#### 4Punyyratr

Exercise 16. Qrfvta gur shapgvbaf rapelcg-fgevat-a naq qrpelcg-fgevat-a juvpu gnxr n Fgevat naq n AnghenyAhzore a naq rapelcg (be qrpelcg) gur fgevat hfvat EBGa. Gung vf, (rapelcg-fgevat-a f 13) fubhyq or gur fnzr nf (rapelcg-fgevat f).

Exercise 17. Guvf rkrepvfr vf rapelcgrq jvgu EBG17.

Uvjzxe kyv wletkzfe kizkyvdzlj-tzgyvi. Zk jyflcu ljv ifkrkzfe czbv vetipgk-jkizex, slk zk jyflcu ifkrkv vrty tyrirtkvi rttfiuzex kf zkj gfjzkzfe ze kyv jkizex. Kyrk zj, zk jyflcu rtk czbv IFK1 fe kyv wzijk tyrirtkvi, czbv IFK13 fe kyv 13ky tyrirtkvi, IFK26 (nyzty ufvj efkyzex) fe kyv 26ky tyrirtkvi, reu IFK1 fe kyv 27ky tyrirtkvi rxrze. Kyve uvjzxe r uvtipgkzfe wletkzfe.