To make sure you can convert programs so that all calls to scripts are in script position.
To test your solutions, keep the originals in your file, copy the define-script macro from the posted class code, and make sure you get the same answer from both the original and converted code.
The following code prompts a user to order a pizza. Rewrite this so that order-pizza, get-toppings, and print-order all work as scripts.
;; order-pizza : -> void ;; prompts user for order and prints summary (define (order-pizza) (let ([crust (prompt-read "What style crust do you want?")]) (cond [(eq? crust 'thin) (print-order crust (get-toppings (list 'pepper 'onion)))] [(eq? crust 'pan) (print-order crust (get-toppings (list 'sausage)))]))) ;; get-toppings : list-of-symbol -> list-of-symbol ;; prompts user for whether to include standard toppings (define (get-toppings tlist) (cond [(symbol=? 'yes (prompt-read (format "This pizza comes with toppings ~a. Do you want them?" tlist))) tlist] [else empty])) ;; print-order : symbol list[symbol] -> void ;; prints summary of pizza order (define (print-order crust toppings) (printf "You ordered a ~a pizza with toppings ~a~n" crust toppings)) (order-pizza)
The following code provides the core of a mastermind program that you want to release on the web. Mastermind is a game in which the player tries to guess a hidden sequence of five colors. After each guess, the game replies with the number of colors in the sequence that are in the right position (the number of white pegs) and the number of colors that are in the sequence but in the wrong position (the number of black pegs). The game continues until the player has guessed the entire sequence of colors.
Convert both prompt-guess and mastermind to scripts (treat all other helper functions as built-in primitives, meaning you should leave them as defines and not convert them or move calls to them).
;; prompts users for a guess at mastermind ;; enter list using parens at prompt (define (prompt-guess) (begin (printf "Enter a list of colors ") (read))) ;; guess-correct? : list[symbol] list[symbol] -> boolean ;; determines whether two lists are identical in contents (including order)w (define (guess-correct? newguess target) (andmap symbol=? newguess target)) ;; calc-white : list[symbol] list[symbol] -> number ;; count number of positions that match exactly between two lists (define (calc-white tlist glist) (length (filter (lambda (p) (symbol=? (first p) (rest p))) (map cons tlist glist)))) ;; calc-white : list[symbol] list[symbol] -> number ;; count number of positions from first list that do match exactly but do appear in ;; second list [ASSUMES that colors in both lists are unique] (define (calc-black tlist glist) (let ([unmatched (filter (lambda (p) (not (symbol=? (first p) (rest p)))) (map cons tlist glist))]) (let ([gum (map first unmatched)] [tum (map rest unmatched)]) (length (filter (lambda (g) (memq g tum)) gum))))) ;; mastermind : list[symbol] list[symbol] number -> number ;; plays mastermind, returning number of tries needed to get right answer (define (mastermind target guess tries) (cond [(guess-correct? guess target) tries] [else (begin (printf "You get ~a white and ~a black pegs~n" (calc-white target guess) (calc-black target guess)) (mastermind target (prompt-guess) (+ 1 tries)))])) (mastermind (list 'red 'blue 'yellow) (prompt-guess) 1)
Turn in a single file hwk7.ss (or hwk7.scm) containing all code and documentation for this assignment. Make sure that both students' names are in a comment at the top of the file.
Refer to the Homework Expectations when preparing your solutions.