Scheme Programming/Procedures< Scheme Programming
To make Scheme complete, it needs a method for declaring a procedure. The keyword
lambda is used to declare a procedure:
(lambda (x) (* x x))
This reads as "The function which takes parameters x, and returns that value, multiplied by itself".
There is an issue here, and that is this function is created, and destroyed in that one line. In its form above, it does not even evaluate with anything. This is known as an 'anonymous closure'.
> (lambda (x) (* x x)) #<CLOSURE <anon> (x) (* x x)>
However, we can make this accessible at a later date, by making use of the earlier
> (define square (lambda (x) (* x x))) #<unspecified> > (square 2) 4
Now, the square function is bound to the name square. It can now be accessed as a normal function, much like + or -, except that we created it.
lambda keyword is the very formal way to define a function, however. We can implicitly define a function, using
define as follows:
(define (square x) (* x x))
This is much more concise and easy to use, however, it is sometimes necessary to use a lambda function as we shall see later on.
The general form of a lambda expression is as follows:
(lambda (<Formal parameters>) <body-expression-1> ... <body-expression-n>)
And the general form for creating a non-anonymous closure is as follows:
(define (<name> <formal parameters>) <body-expression-1> ... <body-expression-n>)
Now we have at our disposal a very powerful language. It is Turing Complete and can compute almost any conceivable algorithm, however, since we have not introduced some features of the Scheme language, we shall avoid many algorithms.