Guest User

Untitled

a guest
Jan 15th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 18.13 KB | None | 0 0
  1. ;; 10.1.5
  2. ;; a lotp is one of:
  3. ;;       - empty
  4. ;;       - (cons tp lotp) where tp is a toy-price
  5. ;;       - and lotp is a list-of-toy-prices
  6.  
  7. #;(define (lotp-temp lotp)
  8.     (cond [(empty? lotp) ... ]
  9.           [else ... (first lotp) ... (lotp-temp (rest lotp)) ... ]))
  10.  
  11. ;; eliminate-exp : number lotp -> lotp
  12. ;; consumes a number and a list of toy prices and
  13. ;; produces a list of all prices in lotp <= the number
  14.  
  15. (define (eliminate-exp ua lotp)
  16.   (cond [(empty? lotp) empty]
  17.         [(> (first lotp) ua) (eliminate-exp ua (rest lotp))]
  18.         [else (cons (first lotp) (eliminate-exp ua (rest lotp)))]))
  19.  
  20. (check-expect (eliminate-exp 1.0
  21.                              (cons 2.95 (cons .95 (cons 1.0 (cons 5 empty)))))
  22.               (cons .95 (cons 1.0 empty)))
  23. (check-expect (eliminate-exp 1.0 (cons .95 (cons 1.0 empty)))
  24.               (cons .95 (cons 1.0 empty)))
  25. (check-expect (eliminate-exp 1.0 empty) empty)
  26.  
  27. ;; 10.1.6
  28. ;; a lotd is one of:
  29. ;;       - empty
  30. ;;       - (cons t lot) where td is a toy-description
  31. ;;          and lot is a list-of-toy-descriptions
  32.  
  33. #;(define (lotd-temp lotd)
  34.     (cond [(empty? lotd) ...]
  35.           [else ... (first lotd) ... (lotd-temp (rest lotd)) ... ]))
  36.  
  37. ;; name-robot : lotd -> lotd
  38. ;; replaces all occurrences of 'robot in a list with 'r2d2
  39.  
  40. (define (name-robot lotd)
  41.   (cond [(empty? lotd) empty]
  42.         [else (cond [(symbol=? 'robot (first lotd))
  43.                      (cons 'r2d2 (name-robot (rest lotd)))]
  44.                     [else (cons (first lotd) (name-robot (rest lotd)))])]))
  45.  
  46. (check-expect (name-robot empty) empty)
  47. (check-expect (name-robot (cons 'doll empty)) (cons 'doll empty))
  48. (check-expect (name-robot (cons 'doll (cons 'robot empty)))
  49.               (cons 'doll (cons 'r2d2 empty)))
  50. (check-expect (name-robot (cons 'robot empty)) (cons 'r2d2 empty))
  51.  
  52. ;; a los is one of:
  53. ;;       - empty
  54. ;;       - (cons s los) where s is a symbol and los is a list-of-symbols
  55.  
  56. #;(define (los-temp los)
  57.     (cond [(empty? los) ... ]
  58.           [else ... (first los) ... (los-temp (rest los)) ... ]))
  59.  
  60. ;; substitute : symbol symbol los -> los
  61. ;; consumes two symbols, old and new, and a los, and produces a new los
  62. ;; where all occurrences of old are replaced by new
  63.  
  64. (define (substitute new old los)
  65.   (cond [(empty? los) empty]
  66.         [else (cond [(symbol=? old (first los))
  67.                      (cons new (substitute new old (rest los)))]
  68.                     [else (cons (first los)
  69.                                 (substitute new old (rest los)))])]))        
  70.  
  71. (check-expect (substitute 'Barbie 'doll
  72.                           (cons 'robot (cons 'doll (cons 'dress empty))))
  73.               (cons 'robot (cons 'Barbie (cons 'dress empty))))
  74. (check-expect (substitute 'Barbie 'doll empty) empty)
  75. (check-expect (substitute 'Barbie 'doll
  76.                           (cons 'robot empty)) (cons 'robot empty))
  77.  
  78. ;; 10.1.7
  79. ;; a lon is one of:
  80. ;;       - empty
  81. ;;       - (cons n lon) where n is a name and lon is a list of names
  82.  
  83. #;(define (lon-temp lon)
  84.     (cond [(empty? lon) ... ]
  85.           [else ... (first lon) .. (rest lon) ... ]))
  86.  
  87. ;; recall : symbol lon -> lon
  88. ;; consumes a symbol and lon and produces a
  89. ;; lon with all instances of the symbol removed
  90.  
  91. (define (recall ty lon)
  92.   (cond [(empty? lon) empty]
  93.         [else (cond [(symbol=? ty (first lon)) (recall ty (rest lon))]
  94.                     [else (cons (first lon) (recall ty (rest lon)))])]))
  95.  
  96. (check-expect (recall 'robot empty) empty)
  97. (check-expect (recall 'robot (cons 'robot (cons 'doll (cons 'dress empty))))
  98.               (cons 'doll (cons 'dress empty)))
  99.  
  100. ;; 10.1.8
  101. ;; a lon is one of:
  102. ;;       - empty
  103. ;;       - (cons n lon) where n is a number and lon is a list of numbers
  104.  
  105. #;(define (lon-temp lon)
  106.     (cond [(empty? lon) ... ]
  107.           [else ... (first lon) .. (rest lon) ... ]))
  108.  
  109. ;; quadratic-roots : a b c -> one of:
  110. ;;       - symbol ('degenerate or 'none)
  111. ;;       - number
  112. ;;       - lon
  113. ;; solves quadratic equations
  114.  
  115. (define (quadratic-roots a b c)
  116.   (cond [(= a 0) 'degenerate]
  117.         [(< (sqr b) (* 4 a c)) 'none]
  118.         [(= (sqr b) (* 4 a c)) (/ (* -1 b) (* 2 a))]
  119.         [(> (sqr b) (* 4 a c))
  120.          (cons (/
  121.                 (+ (* -1 b)
  122.                    (sqrt (- (sqr b) (* 4 a c))))
  123.                 (* 2 a))
  124.                (cons (/
  125.                       (- (* -1 b)
  126.                          (sqrt (- (sqr b) (* 4 a c))))
  127.                       (* 2 a)) empty))]))
  128.  
  129. (check-expect (quadratic-roots 1 0 -1) (cons 1 (cons -1 empty)))
  130. (check-expect (quadratic-roots 2 4 2) -1)
  131. (check-expect (quadratic-roots 0 4 2) 'degenerate)
  132. (check-expect (quadratic-roots 2 1 2) 'none)
  133.  
  134. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  135. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  136.  
  137. (require 2htdp/image)
  138. (require 2htdp/universe)
  139. (define SCENE-WIDTH 200)
  140. (define SCENE-HEIGHT 33)
  141. (define cursor (rectangle 1 25 "solid" "red"))
  142.  
  143. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  144.  
  145. ;; a 1string is a string of length 1
  146.  
  147. ;; a lo1string is one of:
  148. ;;       - empty
  149. ;;       - (cons 1string lo1string)
  150.  
  151. #;(define (lo1string-temp lo1string)
  152.     (cond [(empty? lo1string) ... ]
  153.           [else ... (first lo1string) ...
  154.                 (lo1string-temp (rest lo1string)) ... ]))
  155.  
  156. ;; An Editor is a (make-editor lo1string lo1string)
  157. ;; An editor is interpreted as (append pre post)
  158. ;; with a cursor displayed between pre and post
  159. (define-struct editor (pre post))
  160.  
  161. #;(define (processor a-editor)
  162.     ... (editor-pre a-editor)...
  163.     ... (editor-post a-editor)... )
  164.  
  165. ;; render : editor -> image
  166. ;; produces the image of the text stored in the editor
  167. (define (render an-editor)
  168.   (beside (lo1s->image (editor-pre an-editor))
  169.           cursor
  170.           (lo1s->image (editor-post an-editor))))
  171.  
  172. (check-expect (render (make-editor (cons "s" (cons "t" empty))
  173.                                    (cons "h" (cons "i" (cons "l" empty))))) .)
  174.  
  175. ;; lo1s->image : lo1string -> image
  176. ;; consumes a lo1string and produces an image
  177. (define (lo1s->image lo1string)
  178.   (cond [(empty? lo1string) empty-image]
  179.         [else (beside (1s->image (first lo1string))
  180.                       (lo1s->image (rest lo1string)))]))
  181.  
  182. (check-expect (lo1s->image empty) empty-image)
  183. (check-expect (lo1s->image (cons "s" (cons "t" empty))) .)
  184.  
  185. ;; 1s->image : string -> image
  186. ;; consumes a 1string and produces an image
  187. (define (1s->image 1s)
  188.   (text 1s 20 "black"))
  189. (check-expect (1s->image "s") .)
  190.  
  191. ;; editor->scene : editor -> scene
  192. ;; produces a scene based on the contents of the editor
  193. (define (editor->scene editor)
  194.   (place-image (render editor)
  195.                (text-x editor) (/ SCENE-HEIGHT 2)
  196.                (empty-scene SCENE-WIDTH SCENE-HEIGHT)))
  197.  
  198. (check-expect (editor->scene (make-editor
  199.                               (cons "s" (cons "t" empty))
  200.                               (cons "h" (cons "i" (cons "l" empty)))))            
  201.               .)
  202.  
  203. ;; text-x : String -> Number
  204. ;; determines the x coordinate of the center of the text
  205. (define (text-x editor)
  206.   (+ 4
  207.      (/ (image-width (render editor))
  208.         2)))
  209.  
  210. ;; edit : Editor Key -> Editor
  211. ;; alters the editor's string contents based on key input
  212. (define (edit e k)
  213.   (cond
  214.     [(key=? "\b" k) (backspace e)]        
  215.     [(key=? "right" k) (move-right e)]
  216.     [(key=? "left" k) (move-left e)]
  217.     [(or (key=? "down" k) (key=? "up" k) (key=? "\u007F" k)) e]
  218.     [(<= (- SCENE-WIDTH 5)
  219.          (image-width (render (make-editor
  220.            (cons-append (editor-pre e) k)
  221.            (editor-post e))))) e]
  222.     [else (make-editor
  223.            (cons-append (editor-pre e) k)
  224.            (editor-post e))]))
  225.  
  226.  
  227. ;; backspace : e -> e
  228. ;; takes in an editor e and outputs an edited e
  229. (define (backspace e)
  230.   (cond [(equal? "" (editor-pre e)) e]
  231.         [else (make-editor (remove-last-cons (editor-pre e))
  232.                            (editor-post e))]))
  233.  
  234. (check-expect (backspace
  235.                (make-editor empty
  236.                             (cons "5" empty)))
  237.               (make-editor empty (cons "5" empty)))
  238. (check-expect (backspace
  239.                (make-editor (cons "s" (cons "t" empty)) (cons "t" empty)))
  240.               (make-editor (cons "s" empty) (cons "t" empty)))
  241.  
  242. ;; remove-last-cons : lo1string -> lo1string
  243. ;; takes in a lo1string and outputs a lo1string with the last cons removed
  244. (define (remove-last-cons lo1string)
  245.   (cond [(empty? lo1string) empty]
  246.         [(empty? (rest lo1string)) empty]
  247.         [else (cons (first lo1string) (remove-last-cons (rest lo1string)))]))
  248.  
  249. (check-expect (remove-last-cons empty) empty)
  250. (check-expect (remove-last-cons (cons 4 empty)) empty)
  251. (check-expect (remove-last-cons (cons 5 (cons 4 empty))) (cons 5 empty))
  252.  
  253. ;; cons-append : lo1string 1string -> lo1string
  254. ;; appends a 1string to a lo1string and outputs the edited lo1string
  255. (define (cons-append lo1string 1string)
  256.   (cond [(empty? lo1string) (cons 1string empty)]
  257.         [else (cons (first lo1string) (cons-append (rest lo1string) 1string))]))
  258.  
  259. (check-expect (cons-append empty "s") (cons "s" empty))
  260. (check-expect (cons-append (cons "t" (cons "x" empty)) "s")
  261.               (cons "t" (cons "x" (cons "s" empty))))
  262.  
  263. ;; move-right : e -> e
  264. ;; takes in an editor e and outputs an editor e
  265. (define (move-right e)
  266.   (cond [(equal? "" (editor-pre e)) e]
  267.         [else (make-editor
  268.                (cons-append (editor-pre e)
  269.                             (first (editor-post e)))
  270.                (rest (editor-post e)))]))
  271.  
  272. (check-expect (move-right (make-editor (cons "s" (cons "t" empty))
  273.                                        (cons "t" empty)))
  274.               (make-editor (cons "s" (cons "t" (cons "t" empty)))
  275.                            empty))
  276. (check-expect (move-right (make-editor empty (cons "s" empty)))
  277.               (make-editor (cons "s" empty) empty))
  278.  
  279. ;; move-left : e -> e
  280. ;; takes in an editor e and outputs an editor e
  281. (define (move-left e)
  282.   (cond [(equal? "" (editor-pre e)) e]
  283.         [else (make-editor
  284.                (remove-last-cons (editor-pre e))
  285.                (append (last-cons (editor-pre e)) (editor-post e)))]))
  286.  
  287. (check-expect (move-left (make-editor (cons "s" (cons "t" empty))
  288.                                       (cons "t" empty)))
  289.               (make-editor (cons "s" empty)
  290.                            (cons "t" (cons "t" empty))))
  291.  
  292. ;; last-cons : lo1string -> lo1string
  293. ;; produce a 1-element list containing the last string in a lo1string
  294. (define (last-cons lo1string)
  295.   (cond [(empty? lo1string) empty]
  296.         [(empty? (rest lo1string)) (cons (first lo1string) empty)]
  297.         [else (last-cons (rest lo1string))]))
  298.  
  299.  
  300. (check-expect (last-cons (cons "s" (cons "t" empty))) (cons "t" empty))
  301. (check-expect (last-cons empty) empty)
  302. (check-expect (last-cons (cons "s" empty)) (cons "s" empty))
  303. (check-expect (last-cons (cons "s" (cons "t" (cons "x" empty))))
  304.               (cons "x" empty))
  305.  
  306. (big-bang (make-editor (cons "" empty) (cons "" empty))
  307.           (on-key edit)
  308.           (to-draw editor->scene))
  309.  
  310. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  311. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  312.  
  313. ;; pocket calculator
  314.  
  315. ;; An arithmetic expression is one of:
  316. ;; - a simple arithmetic expression
  317. ;; - a complex arithmetic expression
  318.  
  319. (define-struct simple (a o b))
  320. ;; A simple arithmetic expression is a
  321. ;; a number
  322. ;; (make-simple a o b)
  323. ;; where a is a number, o one of ('+ '- '* '/) , and b is a number
  324. (define simple1 (make-simple 1 '+ 2))
  325. (define simple2 (make-simple 2 '* 4))
  326. (define simple3 (make-simple 6 '/ 3))
  327. (define simple4 (make-simple 7 '- 2))
  328.  
  329.  
  330. (define-struct complexx (s1 o s2))
  331. ;; A complex arithmetic expression is a
  332. ;; (make-complex s1 o s2)
  333. ;;  where s1 and s2 are simple arithmetic expressions and o is a symbol
  334. (define complex1 (make-complexx simple1 '- simple2))
  335. (define complex2 (make-complexx 4 '* simple3))
  336. (define complex3 (make-complexx simple3 '* -1))
  337. (define complex4 (make-complexx simple4 '/ 2))
  338.  
  339. #;(define (eval-arith-temp a-arex)
  340.   (cond
  341.     [(simple? a-arex)
  342.      ( ... (simple-a a-arex) ...
  343.            (simple-o a-arex) ...
  344.            (simple-b a-arex) ... )]
  345.     [(complexx? a-arex)
  346.      ( ... (complexx-s1 a-arex) ...
  347.            (complexx-o a-arex) ...
  348.            (complexx-s2 a-arex) ... )]
  349.     [else ... ]))
  350.  
  351. ;; eval-arith: arithmetic expression -> number
  352. ;; Evaluates an arithmetic expression
  353. (define (eval-arith a-arex)
  354.   (cond
  355.     [(simple? a-arex)
  356.      (cond
  357.        [(symbol=? (simple-o a-arex) '+)
  358.         (+ (simple-a a-arex) (simple-b a-arex))]
  359.        [(symbol=? (simple-o a-arex) '-)
  360.         (- (simple-a a-arex) (simple-b a-arex))]
  361.        [(symbol=? (simple-o a-arex) '*)
  362.         (* (simple-a a-arex) (simple-b a-arex))]
  363.        [(symbol=? (simple-o a-arex) '/)
  364.         (/ (simple-a a-arex) (simple-b a-arex))])]
  365.     [(complexx? a-arex)
  366.      (cond
  367.        [(symbol=? (complexx-o a-arex) '+)
  368.         (+ (eval-arith (complexx-s1 a-arex)) (eval-arith (complexx-s2 a-arex)))]
  369.        [(symbol=? (complexx-o a-arex) '-)
  370.         (- (eval-arith (complexx-s1 a-arex)) (eval-arith (complexx-s2 a-arex)))]
  371.        [(symbol=? (complexx-o a-arex) '*)
  372.         (* (eval-arith (complexx-s1 a-arex)) (eval-arith (complexx-s2 a-arex)))]
  373.        [(symbol=? (complexx-o a-arex) '/)
  374.         (/ (eval-arith (complexx-s1 a-arex))
  375.            (eval-arith (complexx-s2 a-arex)))])]
  376.     [else a-arex]))
  377.  
  378. (check-expect (eval-arith 1) 1)
  379. (check-expect (eval-arith simple1) 3)
  380. (check-expect (eval-arith simple2) 8)
  381. (check-expect (eval-arith complex1) -5)
  382. (check-expect (eval-arith complex2) 8)
  383.  
  384. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  385.  
  386. ;; An arithmetic-expression is one of:
  387. ;; - a simple arithmetic expression
  388. ;; - a complex arithmetic expression
  389.  
  390. ;; A simple arithmetic expression is one of:
  391. ;; - a number
  392. ;; - a variable
  393. ;; - (make-simplee a o b)
  394. ;; - where a is a number or a symbol, o one of ('+ '- '* '/) ,
  395. ;;   and b is a number or a symbol
  396. (define-struct simplee (a o b))
  397. (define simplee1 (make-simplee 'x '+ 2))
  398. (define simplee2 (make-simplee 'y '* 4))
  399. (define simplee3 (make-simplee 'z '/ 3))
  400. (define simplee4 (make-simplee 7 '- 2))
  401.  
  402. ;; A complex arithmetic expression is a
  403. ;; (make-complexxx s1 o s2)
  404. ;;  where s1 and s2 are simple arithmetic expressions and o is a symbol
  405. (define-struct complexxx (s1 o s2))
  406.  
  407. (define complexxx1 (make-complexxx simplee1 '- simplee2))
  408. (define complexxx2 (make-complexxx 4 '* simplee3))
  409. (define complexxx3 (make-complexxx simplee3 '* -1))
  410. (define complexxx4 (make-complexxx simplee4 '/ 2))
  411.  
  412.  
  413. #;(define (eval-arith-temp a-arex)
  414.   (cond
  415.     [(simplee? a-arex)
  416.      ( ... (simplee-a a-arex) ...
  417.            (simplee-o a-arex) ...
  418.            (simplee-b a-arex) ... )]
  419.     [(complexxx? a-arex)
  420.      ( ... (complexxx-s1 a-arex) ...
  421.            (complexxx-o a-arex) ...
  422.            (complexxx-s2 a-arex) ... )]
  423.     [else ... ]))
  424.  
  425. ;; plug-in : arithmetic-expression variable number -> arithmetic expression
  426. ;; consumes an arithmetic-expression, a variable, and a number, and replaces
  427. ;; all occurrences of the variable with the number
  428. (define (plug-in a-arex variable number)
  429.   (cond [(simplee? a-arex)
  430.          (cond [(bothboth a-arex variable)
  431.                 (make-simplee number (simplee-o a-arex) number)]
  432.                [(and (and (symbol? (simplee-a a-arex))
  433.                      (not (symbol? (simplee-b a-arex))))
  434.                      (symbol=? (simplee-a a-arex) variable))
  435.                 (make-simplee number (simplee-o a-arex) (simplee-b a-arex))]
  436.                [(and (and (not (symbol? (simplee-a a-arex)))
  437.                      (symbol? (simplee-b a-arex)))
  438.                 (symbol=? (simplee-b a-arex) variable))
  439.                 (make-simplee (simplee-a a-arex) (simplee-o a-arex) number)]
  440.                [else a-arex])]
  441.         [(complexxx? a-arex)
  442.          (cond [(and (symbol? (complexxx-s1 a-arex))
  443.                      (symbol? (complexxx-s2 a-arex)))
  444.                 (make-complexxx number (complexxx-o a-arex) number)]
  445.                [(and (symbol? (complexxx-s1 a-arex))
  446.                      (not (symbol? (complexxx-s2 a-arex))))
  447.                 (make-complexxx number
  448.                                 (complexxx-o a-arex)
  449.                                 (plug-in (complexxx-s2 a-arex) variable number))]
  450.                [(and (not (symbol? (complexxx-s1 a-arex)))
  451.                      (symbol? (complexxx-s2 a-arex)))
  452.                 (make-complexxx
  453.                  (plug-in (complexxx-s1 a-arex) variable number)
  454.                  (complexxx-o a-arex)
  455.                  number)]
  456.                [(and (not (symbol? (complexxx-s1 a-arex)))
  457.                      (not (symbol? (complexxx-s2 a-arex))))
  458.                 (make-complexxx (plug-in (complexxx-s1 a-arex) variable number)
  459.                                 (complexxx-o a-arex)
  460.                                 (plug-in (complexxx-s2 a-arex) variable number))]
  461.                [else a-arex])]
  462.         [else a-arex]))
  463.  
  464.  
  465.  
  466. (check-expect (plug-in simplee1 'x 1) (make-simplee 1 '+ 2))
  467. (check-expect (plug-in simplee4 'y 3) simplee4)
  468. (check-expect (plug-in (make-complexxx (make-simplee 'x '+ 2) '-
  469.                                        (make-simplee 'y '* 4)) 'y 2)
  470.               (make-complexxx (make-simplee 'x '+ 2) '- (make-simplee 2 '* 4)))
  471. (check-expect (plug-in (make-complexxx (make-simplee 4 '+ 'x) '* 'x) 'x 6)
  472.               (make-complexxx (make-simplee 4 '+ 6) '* 6))
  473. (check-expect (plug-in (make-complexxx 2 '* (make-simplee 'x '* 'x)) 'x 5)
  474.               (make-complexxx 2 '* (make-simplee 5 '* 5)))
  475.  
  476. ;; bothboth : simple-arithmetic-expression variable number -> boolean
  477. ;; determines if the a and b of an arithmetic expression are both symbols and
  478. ;; are both equal to the given variable
  479. (define (bothboth a-arex variable)
  480.   (and
  481.    (and (symbol? (simplee-a a-arex))
  482.                     (symbol? (simplee-b a-arex)))
  483.    (and (symbol=? variable (simplee-a a-arex))
  484.         (symbol=? variable (simplee-b a-arex)))))
  485. (check-expect (bothboth simplee1 'x) false)
  486. (check-expect (bothboth (make-simplee 'x '- 'x) 'x) true)
Add Comment
Please, Sign In to add comment