;; Be sure to set the Language Level to Beginner ;; Run this file using "Step", not "Execute". ;; sum: lon -> num ;; Sums the numbers in a lon. (define (sum a-lon) (cond [(empty? a-lon) 0] [(cons? a-lon) (+ (first a-lon) (sum (rest a-lon)))])) "sum tests:" (define myLON (cons 10 (cons 15 empty))) ;; this line is so you can see the stepper evaluate the function: (sum myLON) "Hand evaluation check: " ;; Note the inclusion of which Law of Scheme is at work. (= ;; initial function invocation (sum myLON) ;; substitute in for placeholder - Law of Scheme for Placeholders: (sum (cons 10 (cons 15 empty))) ;; substitute definition of function for function call -- Law of Scheme for functions: (cond ((empty? (cons 10 (cons 15 empty))) 0) ((cons? (cons 10 (cons 15 empty))) (+ (first(cons 10 (cons 15 empty))) (sum (rest (cons 10 (cons 15 empty))))))) ;; The stepper puts a few extra steps in here to show that it is evaluating the first statement in the cond. ;; These steps that may be omitted. ;; Evaluate conditional expression first -- Law of Scheme for cond: (cond (false 0) ((cons? (cons 10 (cons 15 empty))) (+ (first(cons 10 (cons 15 empty))) (sum (rest (cons 10 (cons 15 empty))))))) ;; If false, go to next conditional expression -- Law of Scheme for cond: (cond ((cons? (cons 10 (cons 15 empty))) (+ (first(cons 10 (cons 15 empty))) (sum (rest (cons 10 (cons 15 empty))))))) ;; Evaluate conditional expression first -- Law of Scheme for cond: (cond (true (+ (first(cons 10 (cons 15 empty))) (sum (rest (cons 10 (cons 15 empty))))))) ;; Subsitute return value of function (cond here)-- Law of Scheme for functions: (+ (first(cons 10 (cons 15 empty))) (sum (rest (cons 10 (cons 15 empty))))) ;; Evaluate inner expressions first - Law of Scheme for functions: (+ 10 (sum (rest (cons 10 (cons 15 empty))))) ;; Evaluate inner expressions first - Law of Scheme for functions: (+ 10 (sum (cons 15 empty))) ;; Substitute definition of function for function call -- Law of Scheme for functions: (+ 10 (cond ((empty? (cons 15 empty)) 0) ((cons? (cons 15 empty)) (+ (first (cons 15 empty)) (sum (rest (cons 15 empty))))))) ;; Stepper steps left out again. ;; Evaluate conditional expression first -- Law of Scheme for cond: (+ 10 (cond (false 0) ((cons? (cons 15 empty)) (+ (first (cons 15 empty)) (sum (rest (cons 15 empty))))))) ;; If false, go to next conditional expression -- Law of Scheme for cond: (+ 10 (cond ((cons? (cons 15 empty)) (+ (first (cons 15 empty)) (sum (rest (cons 15 empty))))))) ;; Evaluate conditional expression first -- Law of Scheme for cond: (+ 10 (cond (true (+ (first (cons 15 empty)) (sum (rest (cons 15 empty))))))) ;; Subsitute return value of function (cond here)-- Law of Scheme for functions: (+ 10 (+ (first (cons 15 empty)) (sum (rest (cons 15 empty))))) ;; Evaluate inner expressions first - Law of Scheme for functions: (+ 10 (+ 15 (sum (rest (cons 15 empty))))) ;; Evaluate inner expressions first - Law of Scheme for functions: (+ 10 (+ 15 (sum empty))) ;; Substitute definition of function for function call -- Law of Scheme for functions: (+ 10 (+ 15 (cond ((empty? empty) 0) ((cons? empty) (+ (first empty) (sum (rest empty))))))) ;; Evaluate conditional expression first -- Law of Scheme for cond: (+ 10 (+ 15 (cond (true 0) ((cons? empty) (+ (first empty) (sum (rest empty))))))) ;; Subsitute return value of function (cond here)-- Law of Scheme for functions: (+ 10 (+ 15 0)) ;; Evaluate inner expressions first - Law of Scheme for functions: (+ 10 15) ;; Apply function -- Law of Scheme for functions-- one more time to get the answer -- finally! whew! 25 ) ;; BE NICE TO YOUR GRADER: UNDERLINE THE EXPRESSION THAT IS GOING TO BE EVALUATED NEXT (Just as the stepper does). --- Your graders thank you!