Hi,
Are LISP functions first order,i.e. I need a chunk of code where I can pass a
function as a parameter to another and use that reference to invoke the
function with some other parameters?. How can I do that
Thanks,
Fahd
Lots of ways. Maybe a macro is what you're looking for, but otherwise
you can use the #' reader macro (same as (function ...). This will
package up the function, but you have to explicitly call this with
funcall, or some other function like one of the map functions.
CL-USER 3 > (defun f (x) (1+ x))
F
CL-USER 4 > (funcall #'f 2)
3
You can do the same with lambda as in (funcall (lambda (x) (1+ x)) 2),
or (funcall #'(lambda (x) (1+ x)) 2). The first one just invokes the
lambda macro which produces the second.
ALBINALI4 wrote:
>
> Hi,
> Are LISP functions first order,i.e. I need a chunk of code where I can pass a
> function as a parameter to another and use that reference to invoke the
> function with some other parameters?. How can I do that
>
> Thanks,
> Fahd
HTH
--
Alexander Schofield
"ALBINALI4" <·········@aol.com> wrote in message
··································@mb-cd.aol.com...
> Hi,
> Are LISP functions first order,i.e. I need a chunk of code where I can
pass a
> function as a parameter to another and use that reference to invoke the
> function with some other parameters?. How can I do that
>
here's a couple of simple examples:
CL-USER 1 > (defun operate (func arg1 arg2)
(funcall func arg1 arg2))
OPERATE
CL-USER 2 > (operate #'* 2 4)
8
CL-USER 3 > (defun operate (func &rest args)
(apply func args))
OPERATE
CL-USER 4 > (operate #'* 2 4 6)
48
--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
>>>>> On 10 Feb 2002 23:54:24 GMT, ALBINALI4 ("ALBINALI4") writes:
ALBINALI4> Are LISP functions first order,i.e. I need a chunk of
ALBINALI4> code where I can pass a function as a parameter to another
ALBINALI4> and use that reference to invoke the function with some
ALBINALI4> other parameters?. How can I do that
Functions are first-class objects, and the most common way that
you'll see them referenced is with the FUNCTION special form.
This can be written out (FUNCTION FOO), or just: #'FOO.
The way to call such a reference with arguments is to use either
FUNCALL or APPLY (depending on how you want to supply the arguments).
You can pass around functions just as you can pass around any other
kind of object (number, symbol, list, array, instance, etc.)
A plain old function call in Lisp (without passing the
function to be called) looks like this:
a> (expt 3 2) ;EXPT requires exactly two arguments
==> 9
The following three expressions all produce the same answer:
b> (+ 1 2)
c> (funcall #'+ 1 2)
d> (apply #'+ (list 1 2))
==> 3
Here's an example that shows that your own functions are just as
first-class as the built-in Lisp functions; it also shows off some
other Lisp features, and another built-in function-calling function:
e> (defun whatever (fun1 fun2 x y z offsets)
(+ (funcall fun1 x y z)
(apply fun2 offsets)))
(defun foo (&rest numbers)
(reduce #'expt numbers))
(whatever #'* #'foo 4 5 6 (list 3 2 3))
==> 849
There are many other interesting ways that you can call functions
and compose functions in Lisp; the examples above are just the
most basic thing you can do.
Chris
·········@aol.com (ALBINALI4) writes:
> Are LISP functions first order,i.e. I need a chunk of code where I
> can pass a function as a parameter to another and use that reference
> to invoke the function with some other parameters?. How can I do
> that
<http://agharta.de/cookbook/functions.html>
Edi.
--
Dr. Edmund Weitz
Hamburg
Germany
The Common Lisp Cookbook
<http://agharta.de/cookbook/>