Chapter 6.5. lambda: A Function So Important It Deserves Its Own Chapter

It’s impossible to overstate the importance of the lambda command in Lisp. In fact, this command is pretty much the entire reason that Lisp exists in the first place.

What lambda Does

In short, lambda lets you create a function without giving it a name. For example, let’s say we create a half function that takes a number and divides it in half. Until now, we’ve written such a function this way:

(defun half (n)
    (/ n 2))
image with no caption

It turns out that, in Lisp, functions are actually values that we can view and pass around just as if they were numbers or lists. An experienced Lisp programmer would say that functions are first-class values in Lisp. As you saw in Chapter 5, you can actually get at the function represented by the word half by using the function operator:

> #'half
#<FUNCTION HALF ...>

The lambda command just lets you do these same two things in a single step. You can define a function and then get it, without giving your function a name:

> (lambda (n) (/ n 2))
#<FUNCTION :LAMBDA ...>

The first parameter to the lambda command is a parameter list, no different from the parameter list used in defun. The rest of the parameters are just the commands for the body of the unnamed function.

Once you have a value representing your unnamed halving function, you can pass it directly to other Common Lisp commands, such as the mapcar or apply commands. For instance, we could do the following to elegantly halve all the values in a list:

> (mapcar (lambda (n) (/ n 2)) '(2 4 6))
(1 2 3)

Because not all parameters of the lambda command are evaluated, lambda itself is not actually a true function. It is something called a macro. Remember from Chapter 2 that all parameters to a Lisp function are evaluated before the function itself is evaluated. Macros, on the other hand, have special powers and are allowed to break those rules. You’ll learn more about macros in Chapter 16.

Also, to confuse matters a bit, the actual value that lambda returns is a regular Lisp function—in this case, a function that cuts a number in half. When Lispers talk about lambda functions—which they pretty much do for breakfast, lunch, and dinner—they’re talking about functions created using lambda. They’re not talking about the lambda macro itself, which is not a function. Got that?

lambda lets your programs do very complicated things.

The lambda form allows your programming code to take a conceptual leap.

While most programming languages try to keep the worlds of functions and values separate, Lisp lets you bridge these worlds as desired. For instance, if you want to package up a little ad hoc function and pass it off to another part of your program, lambda does exactly what you need.

You will see that most Lisp programs use this command very heavily. The same holds true for the remaining examples in this book.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.50.222