When we used map and reduce, we had to define extra functions like fact and square to pass in as parameters. This is a nuisance if we don't need them anywhere else in the program and especially if they're trivial, like sum or addone. For on-the-spot use in cases like this, we can use an anonymous function called a lambda-expression. Here's a lambda-expression corresponding to sum:
lambda ( x, y ) => x + yThe symbol lambda introduces the function and x and y are its formal parameters. The expression x + y is the function definition. The part after lambda is just a recursion equation without the -- and with => instead of <=. Here's another lambda-expression used as the actual parameter of reduce:
reduce ( [ "toe","tac","tic" ], lambda ( a, b ) => b <> a, nil ) ; "tictactoe" : list ( char )There can be more than one recursion equation in the lambda- expression. They're separated from each other by the symbol | and pattern-matching is used to select the appropriate one. Here's an example that uses pattern-matching in a lambda-expression to avoid division by zero when the function it defines is executed:
map ( [ 1,0,2,0,3 ], lambda ( 0 ) => 0 | ( succ ( n ) ) => 100 div succ ( n ) ) ; [ 100,0,50,0,33 ] : list ( num )