; Some notes on exam 2 ; This is bad: (if (test1) #t (test2)) ; This is better: (or (test1) (test2)) ; This is bad: (if (test1) (test2) #f) ; This is better: (and (test1) (test2)) ; This is bad: (if (test1) #t #f) ; This is better: (test1) ; This is bad: (if (test) #f #t) ; This is better: (not (test)) ; Some indentation really sucked. (Or was "suckier" than I would expect.) ; Here is an inefficient attempt to check if lst1 is less than lst2 9 ; (yes, it probably has other errors, too; focus on efficiency) (define list-string (lambda (val) (if (number? val) (number->string val) val)))) (cond ((and (number? x) (number? y)) (< x y)) (else (string-cistring x) (->string y))))))) ; Goal: Find a, b, and c ; > (less-than? a b) ; #t ; > (less-than? b c) ; #t ; > (less-than? a c) ; #f ; Idea: The conversion hurts you. (define a 10) (define b "2") (define c 3) (display '(less-than? a b)) (less-than? a b) (display '(less-than? b c)) (less-than? b c) (display '(less-than? a c)) (less-than? a c) ; Cloning: You need only check the cons cells or vector, not the elements (for memory) ; You need to check the elements (for equality) - equal? on the compound thing works. ; Challenge: Write sect so that it generates the "code" (as a list) for the new ; procedure ; For example ; > (sect + 1 2 '? 3 '?) ; (lambda (v1 v2) (# 1 2 v1 3 v2)) (define sect (lambda (proc . params) ; At the end (let kernel ((remaining-params params) (count 0) (newparams null) (body (list proc))) (cond ((null? remaining-params) (list 'lambda (reverse newparams) (reverse body))) ((eq? '? (car remaining-params)) (let ((newparam (genvar count))) (kernel (cdr remaining-params) (+ count 1) (cons newparam newparams) (cons newparam body)))) (else (kernel (cdr remaining-params) count newparams (cons (car remaining-params) body))))))) (define genvar (lambda (val) (string->symbol (string-append "v" (number->string val)))))