Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; 10.1.5
- ;; a lotp is one of:
- ;; - empty
- ;; - (cons tp lotp) where tp is a toy-price
- ;; - and lotp is a list-of-toy-prices
- #;(define (lotp-temp lotp)
- (cond [(empty? lotp) ... ]
- [else ... (first lotp) ... (lotp-temp (rest lotp)) ... ]))
- ;; eliminate-exp : number lotp -> lotp
- ;; consumes a number and a list of toy prices and
- ;; produces a list of all prices in lotp <= the number
- (define (eliminate-exp ua lotp)
- (cond [(empty? lotp) empty]
- [(> (first lotp) ua) (eliminate-exp ua (rest lotp))]
- [else (cons (first lotp) (eliminate-exp ua (rest lotp)))]))
- (check-expect (eliminate-exp 1.0
- (cons 2.95 (cons .95 (cons 1.0 (cons 5 empty)))))
- (cons .95 (cons 1.0 empty)))
- (check-expect (eliminate-exp 1.0 (cons .95 (cons 1.0 empty)))
- (cons .95 (cons 1.0 empty)))
- (check-expect (eliminate-exp 1.0 empty) empty)
- ;; 10.1.6
- ;; a lotd is one of:
- ;; - empty
- ;; - (cons t lot) where td is a toy-description
- ;; and lot is a list-of-toy-descriptions
- #;(define (lotd-temp lotd)
- (cond [(empty? lotd) ...]
- [else ... (first lotd) ... (lotd-temp (rest lotd)) ... ]))
- ;; name-robot : lotd -> lotd
- ;; replaces all occurrences of 'robot in a list with 'r2d2
- (define (name-robot lotd)
- (cond [(empty? lotd) empty]
- [else (cond [(symbol=? 'robot (first lotd))
- (cons 'r2d2 (name-robot (rest lotd)))]
- [else (cons (first lotd) (name-robot (rest lotd)))])]))
- (check-expect (name-robot empty) empty)
- (check-expect (name-robot (cons 'doll empty)) (cons 'doll empty))
- (check-expect (name-robot (cons 'doll (cons 'robot empty)))
- (cons 'doll (cons 'r2d2 empty)))
- (check-expect (name-robot (cons 'robot empty)) (cons 'r2d2 empty))
- ;; a los is one of:
- ;; - empty
- ;; - (cons s los) where s is a symbol and los is a list-of-symbols
- #;(define (los-temp los)
- (cond [(empty? los) ... ]
- [else ... (first los) ... (los-temp (rest los)) ... ]))
- ;; substitute : symbol symbol los -> los
- ;; consumes two symbols, old and new, and a los, and produces a new los
- ;; where all occurrences of old are replaced by new
- (define (substitute new old los)
- (cond [(empty? los) empty]
- [else (cond [(symbol=? old (first los))
- (cons new (substitute new old (rest los)))]
- [else (cons (first los)
- (substitute new old (rest los)))])]))
- (check-expect (substitute 'Barbie 'doll
- (cons 'robot (cons 'doll (cons 'dress empty))))
- (cons 'robot (cons 'Barbie (cons 'dress empty))))
- (check-expect (substitute 'Barbie 'doll empty) empty)
- (check-expect (substitute 'Barbie 'doll
- (cons 'robot empty)) (cons 'robot empty))
- ;; 10.1.7
- ;; a lon is one of:
- ;; - empty
- ;; - (cons n lon) where n is a name and lon is a list of names
- #;(define (lon-temp lon)
- (cond [(empty? lon) ... ]
- [else ... (first lon) .. (rest lon) ... ]))
- ;; recall : symbol lon -> lon
- ;; consumes a symbol and lon and produces a
- ;; lon with all instances of the symbol removed
- (define (recall ty lon)
- (cond [(empty? lon) empty]
- [else (cond [(symbol=? ty (first lon)) (recall ty (rest lon))]
- [else (cons (first lon) (recall ty (rest lon)))])]))
- (check-expect (recall 'robot empty) empty)
- (check-expect (recall 'robot (cons 'robot (cons 'doll (cons 'dress empty))))
- (cons 'doll (cons 'dress empty)))
- ;; 10.1.8
- ;; a lon is one of:
- ;; - empty
- ;; - (cons n lon) where n is a number and lon is a list of numbers
- #;(define (lon-temp lon)
- (cond [(empty? lon) ... ]
- [else ... (first lon) .. (rest lon) ... ]))
- ;; quadratic-roots : a b c -> one of:
- ;; - symbol ('degenerate or 'none)
- ;; - number
- ;; - lon
- ;; solves quadratic equations
- (define (quadratic-roots a b c)
- (cond [(= a 0) 'degenerate]
- [(< (sqr b) (* 4 a c)) 'none]
- [(= (sqr b) (* 4 a c)) (/ (* -1 b) (* 2 a))]
- [(> (sqr b) (* 4 a c))
- (cons (/
- (+ (* -1 b)
- (sqrt (- (sqr b) (* 4 a c))))
- (* 2 a))
- (cons (/
- (- (* -1 b)
- (sqrt (- (sqr b) (* 4 a c))))
- (* 2 a)) empty))]))
- (check-expect (quadratic-roots 1 0 -1) (cons 1 (cons -1 empty)))
- (check-expect (quadratic-roots 2 4 2) -1)
- (check-expect (quadratic-roots 0 4 2) 'degenerate)
- (check-expect (quadratic-roots 2 1 2) 'none)
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- (require 2htdp/image)
- (require 2htdp/universe)
- (define SCENE-WIDTH 200)
- (define SCENE-HEIGHT 33)
- (define cursor (rectangle 1 25 "solid" "red"))
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; a 1string is a string of length 1
- ;; a lo1string is one of:
- ;; - empty
- ;; - (cons 1string lo1string)
- #;(define (lo1string-temp lo1string)
- (cond [(empty? lo1string) ... ]
- [else ... (first lo1string) ...
- (lo1string-temp (rest lo1string)) ... ]))
- ;; An Editor is a (make-editor lo1string lo1string)
- ;; An editor is interpreted as (append pre post)
- ;; with a cursor displayed between pre and post
- (define-struct editor (pre post))
- #;(define (processor a-editor)
- ... (editor-pre a-editor)...
- ... (editor-post a-editor)... )
- ;; render : editor -> image
- ;; produces the image of the text stored in the editor
- (define (render an-editor)
- (beside (lo1s->image (editor-pre an-editor))
- cursor
- (lo1s->image (editor-post an-editor))))
- (check-expect (render (make-editor (cons "s" (cons "t" empty))
- (cons "h" (cons "i" (cons "l" empty))))) .)
- ;; lo1s->image : lo1string -> image
- ;; consumes a lo1string and produces an image
- (define (lo1s->image lo1string)
- (cond [(empty? lo1string) empty-image]
- [else (beside (1s->image (first lo1string))
- (lo1s->image (rest lo1string)))]))
- (check-expect (lo1s->image empty) empty-image)
- (check-expect (lo1s->image (cons "s" (cons "t" empty))) .)
- ;; 1s->image : string -> image
- ;; consumes a 1string and produces an image
- (define (1s->image 1s)
- (text 1s 20 "black"))
- (check-expect (1s->image "s") .)
- ;; editor->scene : editor -> scene
- ;; produces a scene based on the contents of the editor
- (define (editor->scene editor)
- (place-image (render editor)
- (text-x editor) (/ SCENE-HEIGHT 2)
- (empty-scene SCENE-WIDTH SCENE-HEIGHT)))
- (check-expect (editor->scene (make-editor
- (cons "s" (cons "t" empty))
- (cons "h" (cons "i" (cons "l" empty)))))
- .)
- ;; text-x : String -> Number
- ;; determines the x coordinate of the center of the text
- (define (text-x editor)
- (+ 4
- (/ (image-width (render editor))
- 2)))
- ;; edit : Editor Key -> Editor
- ;; alters the editor's string contents based on key input
- (define (edit e k)
- (cond
- [(key=? "\b" k) (backspace e)]
- [(key=? "right" k) (move-right e)]
- [(key=? "left" k) (move-left e)]
- [(or (key=? "down" k) (key=? "up" k) (key=? "\u007F" k)) e]
- [(<= (- SCENE-WIDTH 5)
- (image-width (render (make-editor
- (cons-append (editor-pre e) k)
- (editor-post e))))) e]
- [else (make-editor
- (cons-append (editor-pre e) k)
- (editor-post e))]))
- ;; backspace : e -> e
- ;; takes in an editor e and outputs an edited e
- (define (backspace e)
- (cond [(equal? "" (editor-pre e)) e]
- [else (make-editor (remove-last-cons (editor-pre e))
- (editor-post e))]))
- (check-expect (backspace
- (make-editor empty
- (cons "5" empty)))
- (make-editor empty (cons "5" empty)))
- (check-expect (backspace
- (make-editor (cons "s" (cons "t" empty)) (cons "t" empty)))
- (make-editor (cons "s" empty) (cons "t" empty)))
- ;; remove-last-cons : lo1string -> lo1string
- ;; takes in a lo1string and outputs a lo1string with the last cons removed
- (define (remove-last-cons lo1string)
- (cond [(empty? lo1string) empty]
- [(empty? (rest lo1string)) empty]
- [else (cons (first lo1string) (remove-last-cons (rest lo1string)))]))
- (check-expect (remove-last-cons empty) empty)
- (check-expect (remove-last-cons (cons 4 empty)) empty)
- (check-expect (remove-last-cons (cons 5 (cons 4 empty))) (cons 5 empty))
- ;; cons-append : lo1string 1string -> lo1string
- ;; appends a 1string to a lo1string and outputs the edited lo1string
- (define (cons-append lo1string 1string)
- (cond [(empty? lo1string) (cons 1string empty)]
- [else (cons (first lo1string) (cons-append (rest lo1string) 1string))]))
- (check-expect (cons-append empty "s") (cons "s" empty))
- (check-expect (cons-append (cons "t" (cons "x" empty)) "s")
- (cons "t" (cons "x" (cons "s" empty))))
- ;; move-right : e -> e
- ;; takes in an editor e and outputs an editor e
- (define (move-right e)
- (cond [(equal? "" (editor-pre e)) e]
- [else (make-editor
- (cons-append (editor-pre e)
- (first (editor-post e)))
- (rest (editor-post e)))]))
- (check-expect (move-right (make-editor (cons "s" (cons "t" empty))
- (cons "t" empty)))
- (make-editor (cons "s" (cons "t" (cons "t" empty)))
- empty))
- (check-expect (move-right (make-editor empty (cons "s" empty)))
- (make-editor (cons "s" empty) empty))
- ;; move-left : e -> e
- ;; takes in an editor e and outputs an editor e
- (define (move-left e)
- (cond [(equal? "" (editor-pre e)) e]
- [else (make-editor
- (remove-last-cons (editor-pre e))
- (append (last-cons (editor-pre e)) (editor-post e)))]))
- (check-expect (move-left (make-editor (cons "s" (cons "t" empty))
- (cons "t" empty)))
- (make-editor (cons "s" empty)
- (cons "t" (cons "t" empty))))
- ;; last-cons : lo1string -> lo1string
- ;; produce a 1-element list containing the last string in a lo1string
- (define (last-cons lo1string)
- (cond [(empty? lo1string) empty]
- [(empty? (rest lo1string)) (cons (first lo1string) empty)]
- [else (last-cons (rest lo1string))]))
- (check-expect (last-cons (cons "s" (cons "t" empty))) (cons "t" empty))
- (check-expect (last-cons empty) empty)
- (check-expect (last-cons (cons "s" empty)) (cons "s" empty))
- (check-expect (last-cons (cons "s" (cons "t" (cons "x" empty))))
- (cons "x" empty))
- (big-bang (make-editor (cons "" empty) (cons "" empty))
- (on-key edit)
- (to-draw editor->scene))
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; pocket calculator
- ;; An arithmetic expression is one of:
- ;; - a simple arithmetic expression
- ;; - a complex arithmetic expression
- (define-struct simple (a o b))
- ;; A simple arithmetic expression is a
- ;; a number
- ;; (make-simple a o b)
- ;; where a is a number, o one of ('+ '- '* '/) , and b is a number
- (define simple1 (make-simple 1 '+ 2))
- (define simple2 (make-simple 2 '* 4))
- (define simple3 (make-simple 6 '/ 3))
- (define simple4 (make-simple 7 '- 2))
- (define-struct complexx (s1 o s2))
- ;; A complex arithmetic expression is a
- ;; (make-complex s1 o s2)
- ;; where s1 and s2 are simple arithmetic expressions and o is a symbol
- (define complex1 (make-complexx simple1 '- simple2))
- (define complex2 (make-complexx 4 '* simple3))
- (define complex3 (make-complexx simple3 '* -1))
- (define complex4 (make-complexx simple4 '/ 2))
- #;(define (eval-arith-temp a-arex)
- (cond
- [(simple? a-arex)
- ( ... (simple-a a-arex) ...
- (simple-o a-arex) ...
- (simple-b a-arex) ... )]
- [(complexx? a-arex)
- ( ... (complexx-s1 a-arex) ...
- (complexx-o a-arex) ...
- (complexx-s2 a-arex) ... )]
- [else ... ]))
- ;; eval-arith: arithmetic expression -> number
- ;; Evaluates an arithmetic expression
- (define (eval-arith a-arex)
- (cond
- [(simple? a-arex)
- (cond
- [(symbol=? (simple-o a-arex) '+)
- (+ (simple-a a-arex) (simple-b a-arex))]
- [(symbol=? (simple-o a-arex) '-)
- (- (simple-a a-arex) (simple-b a-arex))]
- [(symbol=? (simple-o a-arex) '*)
- (* (simple-a a-arex) (simple-b a-arex))]
- [(symbol=? (simple-o a-arex) '/)
- (/ (simple-a a-arex) (simple-b a-arex))])]
- [(complexx? a-arex)
- (cond
- [(symbol=? (complexx-o a-arex) '+)
- (+ (eval-arith (complexx-s1 a-arex)) (eval-arith (complexx-s2 a-arex)))]
- [(symbol=? (complexx-o a-arex) '-)
- (- (eval-arith (complexx-s1 a-arex)) (eval-arith (complexx-s2 a-arex)))]
- [(symbol=? (complexx-o a-arex) '*)
- (* (eval-arith (complexx-s1 a-arex)) (eval-arith (complexx-s2 a-arex)))]
- [(symbol=? (complexx-o a-arex) '/)
- (/ (eval-arith (complexx-s1 a-arex))
- (eval-arith (complexx-s2 a-arex)))])]
- [else a-arex]))
- (check-expect (eval-arith 1) 1)
- (check-expect (eval-arith simple1) 3)
- (check-expect (eval-arith simple2) 8)
- (check-expect (eval-arith complex1) -5)
- (check-expect (eval-arith complex2) 8)
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; An arithmetic-expression is one of:
- ;; - a simple arithmetic expression
- ;; - a complex arithmetic expression
- ;; A simple arithmetic expression is one of:
- ;; - a number
- ;; - a variable
- ;; - (make-simplee a o b)
- ;; - where a is a number or a symbol, o one of ('+ '- '* '/) ,
- ;; and b is a number or a symbol
- (define-struct simplee (a o b))
- (define simplee1 (make-simplee 'x '+ 2))
- (define simplee2 (make-simplee 'y '* 4))
- (define simplee3 (make-simplee 'z '/ 3))
- (define simplee4 (make-simplee 7 '- 2))
- ;; A complex arithmetic expression is a
- ;; (make-complexxx s1 o s2)
- ;; where s1 and s2 are simple arithmetic expressions and o is a symbol
- (define-struct complexxx (s1 o s2))
- (define complexxx1 (make-complexxx simplee1 '- simplee2))
- (define complexxx2 (make-complexxx 4 '* simplee3))
- (define complexxx3 (make-complexxx simplee3 '* -1))
- (define complexxx4 (make-complexxx simplee4 '/ 2))
- #;(define (eval-arith-temp a-arex)
- (cond
- [(simplee? a-arex)
- ( ... (simplee-a a-arex) ...
- (simplee-o a-arex) ...
- (simplee-b a-arex) ... )]
- [(complexxx? a-arex)
- ( ... (complexxx-s1 a-arex) ...
- (complexxx-o a-arex) ...
- (complexxx-s2 a-arex) ... )]
- [else ... ]))
- ;; plug-in : arithmetic-expression variable number -> arithmetic expression
- ;; consumes an arithmetic-expression, a variable, and a number, and replaces
- ;; all occurrences of the variable with the number
- (define (plug-in a-arex variable number)
- (cond [(simplee? a-arex)
- (cond [(bothboth a-arex variable)
- (make-simplee number (simplee-o a-arex) number)]
- [(and (and (symbol? (simplee-a a-arex))
- (not (symbol? (simplee-b a-arex))))
- (symbol=? (simplee-a a-arex) variable))
- (make-simplee number (simplee-o a-arex) (simplee-b a-arex))]
- [(and (and (not (symbol? (simplee-a a-arex)))
- (symbol? (simplee-b a-arex)))
- (symbol=? (simplee-b a-arex) variable))
- (make-simplee (simplee-a a-arex) (simplee-o a-arex) number)]
- [else a-arex])]
- [(complexxx? a-arex)
- (cond [(and (symbol? (complexxx-s1 a-arex))
- (symbol? (complexxx-s2 a-arex)))
- (make-complexxx number (complexxx-o a-arex) number)]
- [(and (symbol? (complexxx-s1 a-arex))
- (not (symbol? (complexxx-s2 a-arex))))
- (make-complexxx number
- (complexxx-o a-arex)
- (plug-in (complexxx-s2 a-arex) variable number))]
- [(and (not (symbol? (complexxx-s1 a-arex)))
- (symbol? (complexxx-s2 a-arex)))
- (make-complexxx
- (plug-in (complexxx-s1 a-arex) variable number)
- (complexxx-o a-arex)
- number)]
- [(and (not (symbol? (complexxx-s1 a-arex)))
- (not (symbol? (complexxx-s2 a-arex))))
- (make-complexxx (plug-in (complexxx-s1 a-arex) variable number)
- (complexxx-o a-arex)
- (plug-in (complexxx-s2 a-arex) variable number))]
- [else a-arex])]
- [else a-arex]))
- (check-expect (plug-in simplee1 'x 1) (make-simplee 1 '+ 2))
- (check-expect (plug-in simplee4 'y 3) simplee4)
- (check-expect (plug-in (make-complexxx (make-simplee 'x '+ 2) '-
- (make-simplee 'y '* 4)) 'y 2)
- (make-complexxx (make-simplee 'x '+ 2) '- (make-simplee 2 '* 4)))
- (check-expect (plug-in (make-complexxx (make-simplee 4 '+ 'x) '* 'x) 'x 6)
- (make-complexxx (make-simplee 4 '+ 6) '* 6))
- (check-expect (plug-in (make-complexxx 2 '* (make-simplee 'x '* 'x)) 'x 5)
- (make-complexxx 2 '* (make-simplee 5 '* 5)))
- ;; bothboth : simple-arithmetic-expression variable number -> boolean
- ;; determines if the a and b of an arithmetic expression are both symbols and
- ;; are both equal to the given variable
- (define (bothboth a-arex variable)
- (and
- (and (symbol? (simplee-a a-arex))
- (symbol? (simplee-b a-arex)))
- (and (symbol=? variable (simplee-a a-arex))
- (symbol=? variable (simplee-b a-arex)))))
- (check-expect (bothboth simplee1 'x) false)
- (check-expect (bothboth (make-simplee 'x '- 'x) 'x) true)
Add Comment
Please, Sign In to add comment