;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-beginner-reader.ss" "lang")((modname lect17-cond-semantics-stepper) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) (require "student-extras.rkt") ; - review: using local variables (or not) ; - syntax/semantics of `cond`, w/o '...' ; - stepping through my-max (w/ and w/o `let`) ;; We re-write the following to use 0,1,2 or 3 local variables, via `let*`: ; downcased-initial : string -> string ; Given a *non-empty* string `w`, ; return its first character, downcased. ; #;(define (downcased-initial w) (string (char-downcase (string-ref w 0)))) ;; Step through this: ;(downcased-initial "hello") #| Syntax,semantics of `cond`: A is: (cond ) A ("question/answers") is either: - ε (that's our notation for 'nothing at all') - [ ] Semantics of `cond`: eval of (cond ) returns: - if is ε, then: semantics say trigger a run-time error. - if is [ ] , then: eval ; If true: return result of eval'ing . If false: return result of eval'ing (cond ). |# ;;;;;;;;;;;;;;; my-max ; my-max : list-of-number -> number ; Return the largest number in `a-lon`. ; (define (my-max a-lon) (cond [(empty? a-lon) -inf.0] [(cons? a-lon) (let* {[max-of-rest (my-max (rest a-lon))]} (if (> max-of-rest (first a-lon)) max-of-rest (first a-lon)))])) ; The point of `let`: ; - avoid repeated calculation! ; - name a sub-part of the overall problem. (define (root a b c) (/ (+ (- b) (sqrt (- (sqr b) (* 4 a c)))) (* 2 a))) ; vs: (define (root2 a b c) (let* {[discriminant (- (sqr b) (* 4 a c))] [numerator (+ (- b) (sqrt discriminant))] [denominator (* 2 a)]} (/ numerator denominator))) (my-max (cons 1 (cons 2 empty))) (my-max (cons -18 (cons -17 (cons -16 (cons -15 (cons -14 (cons -13 (cons -12 (cons -11 (cons -10 (cons -9 (cons -8 (cons -7 (cons -6 (cons -5 (cons -4 (cons -3 (cons -2 (cons -1 empty))))))))))))))))))) (my-max (cons -23 (cons -22 (cons -21 (cons -20 (cons -19 (cons -18 (cons -17 (cons -16 (cons -15 (cons -14 (cons -13 (cons -12 (cons -11 (cons -10 (cons -9 (cons -8 (cons -7 (cons -6 (cons -5 (cons -4 (cons -3 (cons -2 (cons -1 empty))))))))))))))))))))))))