#lang racket (require rackunit) ;; ----------------------------------------------------------------------- ;; Racket functions are bound to names in the same way as any other value! ;; In this expression, + is a free variable: ;; ;; (lambda (x y) ;; (+ (* x x) (* y y))))) ;; ;; You can verify that + is free for yourself by evaluating: (define (capture-+ +) ; Receives a function as an argument, ; ** binds it to + **. (lambda (x y) ; Returns a new-two-argument function (+ (* x x) (* y y))))) ; that uses the captured +. (define sum-of-squares (capture-+ *)) ; pass in * ;; (check-equal? (sum-of-squares 3 4) 25) ; 9 + 16 = 25, right? ;; ------------------------------------------------------------------------- ;; Try these in the REPL. Dr. Racket provides strong support for software ;; engineering by not letting us do this sort of mental gymnastics in our ;; programs. ;; (if (zero? 0) 1 foo) ; foo cannot be reached ;; (and #f foo) ; foo cannot be reached ;; ( ;; (lambda (x) ; this function ignores its argument... ;; (lambda (z) z)) ;; (lambda (x) foo) ; ... so foo cannot be reached ;; ) ;; See: ;; ;; (((lambda (x) (lambda (z) z)) ;; (lambda (x) foo)) ;; 'x)