|
Practical Common LispAnonymous Functions Once you start writing, or even simply using, functions that accept other functions as arguments, you're bound to discover that sometimes it's annoying to have to define and name a whole separate function that's used in only one place, especially when you never call it by name. When it seems like overkill to define a new function with DEFUN, you can create an "anonymous" function using a LAMBDA expression. As discussed in Chapter 3, a LAMBDA expression looks like this: (lambda (parameters) body) One way to think of LAMBDA expressions is as a special kind of function name where the name itself directly describes what the function does. This explains why you can use a LAMBDA expression in the place of a function name with #'. (funcall #'(lambda (x y) (+ x y)) 2 3) ==> 5 You can even use a LAMBDA expression as the "name" of a function in a function call expression. If you wanted, you could write the previous FUNCALL expression more concisely. ((lambda (x y) (+ x y)) 2 3) ==> 5 But this is almost never done; it's merely worth noting that it's legal in order to emphasize that LAMBDA expressions can be used anywhere a normal function name can be.[66] Anonymous functions can be useful when you need to pass a function as an argument to another function and the function you need to pass is simple enough to express inline ...» |
Код для вставки книги в блог HTML
phpBB
текст
|
|