daily pastebin goal
32%
SHARE
TWEET

Untitled

a guest Jan 12th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;;;; Problem A1
  2.  
  3. ;; 1.)
  4.  
  5. (define (fahrenheit->celsius f)
  6.   (* 5/9 (- f 32)))
  7.  
  8. (define (celsius->fahrenheit c)
  9.   (+ (* 9/5 c) 32))
  10.  
  11. (celsius->fahrenheit (fahrenheit->celsius 212))
  12.  
  13.  
  14. ;;Step 1 - Function application
  15. (celsius->fahrenheit (* 5/9 (- 212 32)))
  16.  
  17. ;;Step 2 - Arithmetic
  18. (celsius->fahrenheit (* 5/9 180))
  19.  
  20. ;;Step 3 - Arithmetic
  21. (celsius->fahrenheit 100)
  22.  
  23. ;;Step 4 - Function application
  24. (+ (* 9/5 100) 32)
  25.  
  26. ;;Step 5 - Arithmetic
  27. (+ 180 32)
  28.  
  29. ;;Step 6 - Arithmetic
  30. 212
  31.  
  32.  
  33. ;; 2.)
  34.  
  35. (define-struct customer (title first last))
  36.  
  37. (define (formal* loc)
  38.   (cond [(empty? loc) empty]
  39.         [else (cons (formal (first loc))
  40.                     (formal* (rest loc)))]))
  41.  
  42. (define (formal c)
  43.   (string-append "Dear "
  44.                  (customer-title c)
  45.                  " "
  46.                  (customer-last c)))
  47.  
  48. (formal* (cons (make-customer "Dr." "Olin" "Shivers")
  49.            (cons (make-customer "Mstr." "ZC" "Flatt")
  50.                  empty)))
  51.  
  52. ;;Step 1 - Function application
  53.   (cond [(empty? (cons (make-customer "Dr." "Olin" "Shivers")
  54.            (cons (make-customer "Mstr." "ZC" "Flatt")
  55.                  empty))) empty]
  56.         [else (cons (formal (first (cons (make-customer "Dr." "Olin" "Shivers")
  57.            (cons (make-customer "Mstr." "ZC" "Flatt")
  58.                  empty))))
  59.                     (formal* (rest (cons (make-customer "Dr." "Olin" "Shivers")
  60.            (cons (make-customer "Mstr." "ZC" "Flatt")
  61.                  empty)))))])
  62.  
  63. ;;Step 2 - Conditional
  64.   (cond [false empty]
  65.         [else (cons (formal (first (cons (make-customer "Dr." "Olin" "Shivers")
  66.            (cons (make-customer "Mstr." "ZC" "Flatt")
  67.                  empty))))
  68.                     (formal* (rest (cons (make-customer "Dr." "Olin" "Shivers")
  69.            (cons (make-customer "Mstr." "ZC" "Flatt")
  70.                  empty)))))])
  71.  
  72. ;;Step 3 - Conditional
  73.   (cond [else (cons (formal (first (cons (make-customer "Dr." "Olin" "Shivers")
  74.            (cons (make-customer "Mstr." "ZC" "Flatt")
  75.                  empty))))
  76.                     (formal* (rest (cons (make-customer "Dr." "Olin" "Shivers")
  77.            (cons (make-customer "Mstr." "ZC" "Flatt")
  78.                  empty)))))])
  79.  
  80. ;;Step 4 - Conditional
  81. (cons (formal (first (cons (make-customer "Dr." "Olin" "Shivers")
  82.     (cons (make-customer "Mstr." "ZC" "Flatt") empty))))
  83.         (formal* (rest (cons (make-customer "Dr." "Olin" "Shivers")
  84.         (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
  85.  
  86. ;;Step 5 - Function Application
  87. (cons (formal (make-customer "Dr." "Olin" "Shivers"))
  88.         (formal* (rest (cons (make-customer "Dr." "Olin" "Shivers")
  89.         (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
  90.  
  91. ;;Step 6 - Function Application
  92. (cons (string-append "Dear "
  93.                  (customer-title (make-customer "Dr." "Olin" "Shivers"))
  94.                  " "
  95.                  (customer-last (make-customer "Dr." "Olin" "Shivers")))
  96.         (formal* (rest (cons (make-customer "Dr." "Olin" "Shivers")
  97.         (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
  98.  
  99. ;;Step 7 - Function Application
  100. (cons (string-append "Dear "
  101.                  "Dr."
  102.                  " "
  103.                  (customer-last (make-customer "Dr." "Olin" "Shivers")))
  104.         (formal* (rest (cons (make-customer "Dr." "Olin" "Shivers")
  105.         (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
  106.  
  107. ;;Step 8 - Function Application
  108. (cons (string-append "Dear "
  109.                  "Dr."
  110.                  " "
  111.                  "Shivers")
  112.         (formal* (rest (cons (make-customer "Dr." "Olin" "Shivers")
  113.         (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
  114.  
  115. ;;Step 9 - Function Application
  116. (cons "Dear Dr. Shivers"
  117.         (formal* (rest (cons (make-customer "Dr." "Olin" "Shivers")
  118.         (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
  119.  
  120. ;;Step 10 - Function Application
  121. (cons "Dear Dr. Shivers"
  122.         (formal* (cons (make-customer "Mstr." "ZC" "Flatt") empty)))
  123.  
  124. ;;Step 11 - Function Application
  125. (cons "Dear Dr. Shivers"
  126.   (cond [(empty? (cons (make-customer "Mstr." "ZC" "Flatt") empty)) empty]
  127.         [else (cons (formal (first (cons (make-customer "Mstr." "ZC" "Flatt") empty)))
  128.                     (formal* (rest (cons (make-customer "Mstr." "ZC" "Flatt") empty))))]))
  129.  
  130. ;;Step 12 - Conditional
  131. (cons "Dear Dr. Shivers"
  132.   (cond [false empty]
  133.         [else (cons (formal (first (cons (make-customer "Mstr." "ZC" "Flatt") empty)))
  134.                     (formal* (rest (cons (make-customer "Mstr." "ZC" "Flatt") empty))))]))
  135.  
  136. ;;Step 13 - Conditional
  137. (cons "Dear Dr. Shivers"
  138.   (cond [else (cons (formal (first (cons (make-customer "Mstr." "ZC" "Flatt") empty)))
  139.                     (formal* (rest (cons (make-customer "Mstr." "ZC" "Flatt") empty))))]))
  140.  
  141. ;;Step 14 - Conditional
  142. (cons "Dear Dr. Shivers"
  143.   (cons (formal (first (cons (make-customer "Mstr." "ZC" "Flatt") empty)))
  144.         (formal* (rest (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
  145.  
  146. ;;Step 15 - Function Application
  147. (cons "Dear Dr. Shivers"
  148.   (cons (formal (make-customer "Mstr." "ZC" "Flatt"))
  149.         (formal* (rest (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
  150.  
  151. ;;Step 16 - Function Application
  152. (cons "Dear Dr. Shivers"
  153.   (cons (string-append "Dear "
  154.                  (customer-title (make-customer "Mstr." "ZC" "Flatt"))
  155.                  " "
  156.                  (customer-last (make-customer "Mstr." "ZC" "Flatt")))
  157.         (formal* (rest (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
  158.  
  159. ;;Step 17 - Function application
  160. (cons "Dear Dr. Shivers"
  161.   (cons (string-append "Dear "
  162.                  "Mstr."
  163.                  " "
  164.                  (customer-last (make-customer "Mstr." "ZC" "Flatt")))
  165.         (formal* (rest (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
  166.  
  167. ;;Step 18 - Function Application
  168. (cons "Dear Dr. Shivers"
  169.   (cons (string-append "Dear "
  170.                  "Mstr."
  171.                  " "
  172.                  "Flatt")
  173.         (formal* (rest (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
  174.  
  175. ;;Step 19 - Function application
  176. (cons "Dear Dr. Shivers"
  177.   (cons "Dear Mstr. Flatt"
  178.         (formal* (rest (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
  179.  
  180. ;;Step 20 - Function Application
  181. (cons "Dear Dr. Shivers"
  182.   (cons "Dear Mstr. Flatt"
  183.         (formal* empty)))
  184.  
  185. ;;Step 21 - Function Application
  186. (cons "Dear Dr. Shivers"
  187.   (cons "Dear Mstr. Flatt"
  188.         (cond [(empty? empty) empty]
  189.         [else (cons (formal (first empty))
  190.                     (formal* (rest empty)))])))
  191.  
  192. ;;Step 22 - Conditional
  193. (cons "Dear Dr. Shivers"
  194.   (cons "Dear Mstr. Flatt"
  195.         (cond [true empty]
  196.         [else (cons (formal (first empty))
  197.                     (formal* (rest empty)))])))
  198.  
  199. ;;Step 23 - Conditional
  200. (cons "Dear Dr. Shivers"
  201.   (cons "Dear Mstr. Flatt"
  202.      empty))
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209. ;;;;; Problem A2
  210.  
  211. (require 2htdp/image)
  212.  
  213. (define-struct ball (x y color))
  214. ;; Ball = (make-ball Number Number Color)
  215. ;; Color is one of 'red, 'yellow, 'blue, etc.
  216.  
  217. ;;A LOB is one of
  218. ;; -- (cons Ball LOB)
  219. ;; -- empty
  220.  
  221. (define lob1 (cons (make-ball 50 50 'red)
  222.                    (cons (make-ball 78 92 'blue)
  223.                          (cons (make-ball 159 41 'green)
  224.                              empty))))
  225.  
  226. ;; (define (lob-temp a-lob)
  227. ;;   (cond [(empty? a-lob) ...]
  228. ;;         [else ( ... (lob-temp (rest a-lob)) ... )]))
  229.  
  230. ;; lob-length : LOB -> Number
  231. ;; Counts the number of balls in a list of Balls
  232. (check-expect (lob-length lob1) 3)
  233.  
  234. (define (lob-length lob)
  235.   (cond [(empty? lob) 0]
  236.         [else (+ 1 (lob-length (rest lob)))]))
  237.  
  238.  
  239. ;; lob-x : LOB -> List
  240. ;; Extracts all x coordinates in a list of Balls
  241. (check-expect (lob-x lob1) (cons 50 (cons 78 (cons 159 empty))))
  242.  
  243. (define (lob-x lob)
  244.   (cond [(empty? lob) empty]
  245.         [else (cons (ball-x (first lob)) (lob-x (rest lob)))]))
  246.  
  247.  
  248. ;; lob-draw : LOB -> Image
  249. ;; Places all balls in a list onto an empty scene
  250. (check-expect (lob-draw lob1) .)
  251.  
  252.  
  253. (define (lob-draw lob)
  254.   (cond [(empty? lob) (empty-scene 300 300)]
  255.         [else
  256.          (place-image
  257.             (circle 3 "solid" (ball-color (first lob)))
  258.             (ball-x (first lob)) (ball-y (first lob))
  259.                     (lob-draw (rest lob)))]))
  260.  
  261.  
  262.  
  263. ;; lob-filter : LOB -> List
  264. ;; Removes from the list any ball whose coordinates do not fall within 300x300
  265. (check-expect
  266.  (lob-filter
  267.    (cons (make-ball 268 156 'purple) (cons (make-ball 346 295 'pink) lob1)))
  268.      (cons (make-ball 268 156 'purple) lob1))
  269.  
  270.  
  271. (define (lob-filter lob)
  272.    (cond [(empty? lob) empty]
  273.          [(or
  274.            (> (ball-x (first lob)) 300)
  275.            (< (ball-x (first lob)) 0)
  276.            (> (ball-y (first lob)) 300)
  277.            (< (ball-y (first lob)) 0)) (lob-filter (rest lob))]
  278.          [else (cons (first lob) (lob-filter (rest lob)))]))
  279.  
  280.  
  281.  
  282. ;; lob-member? : LOB Ball -> Boolean
  283. ;; Tells if the given Ball matches another Ball in the given LOB
  284. (check-expect (lob-member? lob1 (make-ball 78 92 'blue)) true)
  285. (check-expect (lob-member? lob1 (make-ball 78 4 'blue)) false)
  286.  
  287. (define (lob-member? lob b)
  288.    (cond [(empty? lob) false]
  289.          [(and
  290.            (= (ball-x (first lob)) (ball-x b))
  291.            (= (ball-y (first lob)) (ball-y b))
  292.            (symbol=? (ball-color (first lob)) (ball-color b))) true]
  293.          [else (lob-member? (rest lob) b)]))
  294.  
  295.  
  296.  
  297.  
  298.  
  299. ;;;;; Problem A3
  300.  
  301. (require 2htdp/universe)
  302.  
  303. (define-struct txt (content x y))
  304. ;; Txt = (make-txt String Number Number)
  305.  
  306. ;; LoTxt is one of:
  307. ;; -- empty
  308. ;; -- (cons Txt LoTxt)
  309.  
  310. (define orientx 200)
  311. (define orienty 150)
  312.  
  313. (define txtlist
  314.   (cons (make-txt "On your mark." orientx orienty)
  315.     (cons (make-txt "Get set."
  316.                     orientx (+ 25 orienty))
  317.       (cons (make-txt "Go!"
  318.                     orientx (+ 50 orienty))                      
  319.            empty))))
  320.  
  321. (define-struct world (image hidden))
  322. ;; World = (make-world Image LoTxt)
  323. ;; intepretation:
  324. ;;  The world's image represents the image that the audience can see.
  325. ;;  The world's list of Txt represents the yet-to-be-revealed elements.
  326.  
  327. (define WORLD-0 (make-world (empty-scene 400 400) txtlist))
  328.  
  329. ;; display : World -> Image
  330. ;; Renders current world state
  331. (check-expect (display WORLD-0) .)
  332.  
  333.  
  334. (define (display w)
  335.   (world-image w))
  336.        
  337.  
  338. ;; next : World -> World
  339. ;; Consumes current world, produces next world with next text added
  340.  
  341. (define (next w)
  342.   (cond [(empty? (world-hidden w)) WORLD-0]
  343.         [else (make-world
  344.                (place-image (text
  345.                              (txt-content (first (world-hidden w))) 20 'blue)
  346.                                  (txt-x (first (world-hidden w)))
  347.                                  (txt-y (first (world-hidden w)))
  348.                                  (world-image w)) (rest (world-hidden w)))]))
RAW Paste Data
Top