Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

Higher Order Functions

By Jonathan Bartlett
2005-05-13


Creating anonymous functions

In Scheme, functions are created by default without names. The lambda special form creates a nameless function and then returns the value to the enclosing form or function call. The enclosing form may:

• Set a symbol to refer to the value (thus giving it a name).
• Store the value in a data structure for later use.
• Pass the value as a parameter to a function.

In most programming languages, the act of defining functions and naming them occur simultaneously. That these operations are separate actions in Scheme leads to a lot of confusion to new Scheme programmers. The Scheme method is actually very simple though, because Scheme treats functions just as it would other values.

Functions are created with lambda. Just like other values, these functions can be:

• passed as arguments,
• stored into variables,
• stored as a part of a larger data structure.

To make a Scheme function created with lambda act like a function from other languages, you need only to store it in a global variable -- this makes the function visible to other functions and accessible by name. Listing 1 shows an example of an anonymous function in Scheme that squares the number given to it.

Listing 1. A nameless function


(lambda (x)

(* x x)
)
This code defines a function with one formal parameter, x; the function squares the parameter and returns the value. Remember, Scheme doesn't need explicit return values. Scheme simply returns the result of the last form evaluated in the function.

Let's continue by giving the function a name as in Listing 2.

Listing 2. Naming a function

(define square

(lambda (x)
(* x x)))

(display (square 3))
(newline)
(display (square 4))
(newline)
This is the simplest and most common way of dealing with functions -- giving them a name and then using them in computation later on. However, there is no rule in Scheme that forces you to give a function a name before using it.

In Scheme, the head of a list in a program must result in a function or a special form, but that doesn't mean it has to be the name of a function. Because the lambda special form returns functions, you can actually use a lambda function definition directly in a function call.

For example, instead of writing (square 4), you can also write ( (lambda (x) (* x x)) 4). This is a two-element list in which the first element is the definition of the function and the second element is a number. The head of this list is the function definition itself. When it runs, it first evaluates everything within the list. The lambda form evaluates to a function definition which, because it is the head of the list, is called on the second element. The return value of this form is the square of the number 4.

Defining and calling functions immediately rather than naming them or passing them as values is an interesting concept, but not extremely useful. However, it does illustrate the idea of functions as values and shows how anonymous functions work.

Now let's look at functions as function arguments.

Tutorial Pages:
» Using functions for such higher order purposes as arguments, function-generating functions, and anonymous functions
» Creating anonymous functions
» Functions as function arguments
» Using functions as arguments
» Building functions at runtime
» Functions and object-oriented programming
» Of the highest order
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» How to Install PHP 5 on Linux
» How to Install Apache 2 on Linux
» How to Install MySQL 5.0 on Linux
» SMB Caching
» Mound --Bind
» Tar Wild Card Interpretation

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources