next up previous
Next: Conditionals Up: Expressions Previous: Record Operations

Lambda Expressions, Applications and Currying

A lambda expression is a way to write a function without really giving it a name. For example, here is a function that takes a number and negates it:

lambda x . -x;
and here is another that takes two elements are compares them:
lambda x . lambda y . x = y;
You can apply these functions to arguments by juxtaposing them as in:
( lambda x . -x ) 10;
which results in the value -10.

Note that lambda x . lambda y . x = y is a function that takes an argument x and returns another lambda expression. Thus

( lambda x . lambda y . x = y ) 10;
returns another lambda expression as a value, namely
lambda y . 10 = y;
You can apply the whole expression to two numbers as in:
( lambda x . lambda y . x = y ) 10 11;
which evaluates to false.

The function lambda x . lambda y . x = y is polymorphic, meaning that it can be applied to any two arguments of the same type. In fact, ccli considers its type to be

'a -> 'a -> boolean
where 'a is a variable type (i.e. it stands for any type). If you apply the function to a string as in
( lambda x . lambda y . x = y ) "ccl";
you get a function of type
string -> bool
That is, putting a string as the first argument adds the constraint to the ccli type checker that the second argument should also be a string. Thus,
( lambda x . lambda y . x = y ) "ccl" 5;
produces the type error
Type error in 'hello.ccl' on line 23:
  could not apply function to argument
  ( ( lambda x . ( lambda y . (x=y) ) ) "ccl" ) 
    has type string -> boolean, while
  5 has type integer
noting the problem. You also have to be careful about parentheses. For example, the following produces an error
( lambda x . lambda y . x + y ) 10 - 11;
because it parses as
( ( lambda x . lambda y . x + y ) 10 ) - 11;
which makes no sense (you can't subtract an integer from a function). Thus, use
( lambda x . lambda y . x + y ) ( 10 - 11 ); // or
( lambda x . lambda y . x + y ) 10  ( - 11 );
depending on your intentions.


next up previous
Next: Conditionals Up: Expressions Previous: Record Operations
Eric Klavins 2003-12-03