Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
(define myFunc (lambda (x y)
   (...)))

The only reason we ever give associate a name with something is if we wish to reference it more than once!

...

For instance, lambdas are very useful when calling a function is needed as an input parameter, such as when calling higher-order functions such as filter, e.g. filter, map, foldr, etc.:

Code Block
(filter (lambda (x) (...)) aList)

(map (lambda (x) (...)) aList)

(foldr (lambda (aFirst aRecursiveResult)(...)) aList)

Lambdas are also useful when a function is needed for a return value:

Code Block

;; make-addX: num -> (lambda num -> num)
;; returns a function that adds its input to the original x input.
;; Examples:
(check-expect ((make-addX 3) 5) 8)
(check-expect ((make-addX 3) -2) 1)
(check-expect ((make-addX -5) 5) 0)
(check-expect ((make-addX -5) -2) -7)
(define (make-addX x)
   (lambda (y) (+ x y))) 

Problem: A "pure" lambda cannot be recursive because it cannot refer to itself!   This issue can be solved using more sophisticated lambda function techniques, which are mostly out of the scope of this course, and in Java, which always defines a name for object to use to reference itself.