After we define a function, the name of the function can be used as a value without calling it. If you just evaluate the function name, then Plait will print something like #<procedure>.
> plus-five- (Number -> Number)
#<procedure:plus-five>
More usefully, you might pass the function to another function that calls it. For example, the map function takes a function and a list, and it applies the function to each element of the list to produce a new list.
'(123))- (Listof Number)
'(6 7 8)
Sometimes, and especially with map , you need a one-off function that doesn’t need to be defined for everyone else to see, and it doesn’t even need a name. You can make an anonymous function by using lambda :
- (Listof Number)
'(7 8 9)
The form (lambda (n)(+ n6)) means “the function that takes an argument n and returns (+ n6).” You can evaluate a lambda form without passing it anywhere, although that isn’t particularly useful:
- (Number -> Number)
#<procedure>
Notice that the result has a function type: it’s a function that takes a Number and returns a Number .
An anonymous function created with lambda doesn’t have to stay anonymous. Since you can use a lambda form anywhere that an expression is allowed, you can use in define :
This definition is completely equivalent to the function-definition shorthand:
Another interesting property of lambda functions is that, just like any local function, the body of a lambda can see any surrounding variable binding. For example, the lambda body in the following add-to-each function can see the m that is passed to add-to-each:
> (add-to-each7'(123))- (Listof Number)
'(8 9 10)
> (add-to-each70'(123))- (Listof Number)
'(71 72 73)
You can declare types for lambda arguments and results similar to declaring them with define in the function-definition shorthand:
- (String -> Boolean)
#<procedure>