Problem 2 Write the step-by-step computation that would be taken if you ran this 5 POINTS program in the DrRacket Stepper. Besides showing the intermediate terms of the computation, label each step as either: arith: Primitive “arithmetic” (of any form, not just numeric operations) plug: Function application—“plugging in” conditional: A conditional step. |# (require 2htdp/image) (define (absval n) (cond [(< n 0) (- n)] [else n])) (circle (absval (- 3 10)) "solid" "green") ; arith (circle (absval -7) "solid" "green") ; plug (circle (cond [(< -7 0) (- -7)] [else -7]) "solid" "green") ; arith (circle (cond [true (- -7)] [else -7]) "solid" "green") ; conditional (circle (- -7) "solid" "green") ; arith (circle 7 "solid" "green") ; draw circle step -> FIN ;;; A Comment is a (make-comment String String) (define-struct comment (name text)) ;;; A LOC (list of comments) is one of: ;;; - empty ;;; - (cons Comment LOC) (define c1 (list (make-comment "Olin" "Yahoo! You go, girl!") (make-comment "Amal" "I failed -- try again in the Spring. :-(") (make-comment "Marty" "Big deal. I made a 96.") (make-comment "Marty" "I'm better than all of you!"))) ;; LOC String -> LOC ;; Makes a new list of comments without any comments ;; with the given name in from the given list of comments. (define (defriend aloc name) (cond [(empty? aloc) empty] [else (if (string=? (comment-name (first aloc)) name) (defriend (rest aloc) name) (cons (first aloc) (defriend (rest aloc) name)))])) (check-expect (defriend empty "foo") empty) (check-expect (defriend c1 "Marty") (list (make-comment "Olin" "Yahoo! You go, girl!") (make-comment "Amal" "I failed -- try again in the Spring. :-("))) (check-expect (defriend c1 "Amal") (list (make-comment "Olin" "Yahoo! You go, girl!") (make-comment "Marty" "Big deal. I made a 96.") (make-comment "Marty" "I'm better than all of you!"))) ;; a Interval is one of: ;; - HalfOpen ;; - Closed ;; - Composite ;; a HalfOpen is a (make-half-open Number Number) ;; Interpretation is the begining point inclusive, ;; second number is end point exclsive. (define-struct half-open (left right)) ;; a Closed is a (make-closed Number Number) ;; interpret as inclusive numbers. (define-struct closed (left right)) ;; a Composite is a (make-composite Interval Interval) (define-struct composite (left right)) ;; Alterna...askadlftave ;; A Half-Open is one of: ;; - Closed-Open ;; - Open-Closed ;; A Closed-open is a (make-closed-open Number Number) ;; A Open-Closed is a (make-open-closed Number Number) ;; HalfOpen -> ???? (define (half-open-temp ah) (... (half-open-left ah) ... (half-open-right ah) ...)) ;; Interval Number -> Boolean ;; consumes a half-open interval and a ;; number and determines if the number is in the interval. (define (contains? ah n) (cond [(half-open? ah) (and (>= n (half-open-left ah)) (< n (half-open-right ah)))] [(closed? ah) (and (>= n (closed-left ah)) (<= n (closed-right ah)))] [else (or (contains? (composite-left ah) n) (contains? (composite-right ah) n))])) (check-expect (contains? (make-half-open 0 3) 4) false) (check-expect (contains? (make-half-open 0 3) 2) true) (check-expect (contains? (make-half-open 0 3) 0) true) (check-expect (contains? (make-half-open 0 3) 3) false) (check-expect (contains? (make-closed 0 3) 3) true) (check-expect (contains? (make-composite (make-closed 0 3) (make-half-open 5 7)) 4) false) (check-expect (contains? (make-composite (make-closed 0 3) (make-half-open 5 7)) 6) true) ;;; A Horcrux is one of: ;;; - (make-qwoggle Horcrux Number) ;;; - String (define-struct qwoggle (x blazt)) ;;; A LON is one of: ;;; - empty ;;; - (cons Number LON) make-qwoggle qwoggle-x qwoggle-blazt qwoggle? #;(define (temp h) (cond [(string? h) ...] [(qwoggle? h) ... (temp (qwoggle-x h)) ... ... (qwoggle-blazt h) ...])) ;;; A Position is a (make-position String Number Number) (define-struct position (name numshares share-price)) ;;; Interpretation: a Position gives the name of a stock, the ;;; number of shares we own, and the current price of one share ;;; of the stock. ;;; A Portfolio is one of ;;; - empty ;;; - (cons Position Portfolio) ;;; Interpretation: a Portfolio is a collection of stock positions. (define p1 (cons (make-position "IBM" 10 190) ; 10 shares IBM $190/share (cons (make-position "AAPL" 5 422) ; 5 shares Apple $422/share. empty))) ;; Portfolio -> Number ;; returns the total value of all the positions. (define (portfolio-worth p) (cond [(empty? p) 0] [else (+ (* (position-numshares (first p)) (position-share-price (first p))) (portfolio-worth (rest p)))])) (check-expect (portfolio-worth empty) 0) (check-expect (portfolio-worth p1) 4010)