From: Noah Green
Subject: Help: passing functions as arguments in Common Lisp
Date: 
Message-ID: <354014AC.C85BB983@interport.net>
Hi,
I've been searching thru some FAQs and stuff and am still stuck,
& would love some help. I know some Scheme and ML and
am just starting with Common Lisp. I'm trying to pass a function
as an argument, and then use it within a function. For example:

(defun is_member_of (equiv_rel element thelist)
	(cond ((equiv_rel element (car thelist)) t)
	      (t (is_member_of equiv_rel element (cdr thelist))))

This is written to see if "element" is a member of "thelist," using
"equiv_rel" as the boolean function to do the actual test. And to
call it:

(is_member_of equal 'a '(a b c))

That is, using the built-in function "equal" as the equivalence 
relation. (I realize that there is a built-in "member" function, this
is just an example.) This kind of thing interprets in Scheme but not
Common Lisp. I'm missing some kind of syntactic thing, I know, but
I can't dig it up. Can you tell me what it is?

Thanks gang!
-noah

·······@interport.net

From: Barry Margolin
Subject: Re: Help: passing functions as arguments in Common Lisp
Date: 
Message-ID: <GtV%.8$9Y4.32@cam-news-reader1.bbnplanet.com>
In article <·················@interport.net>,
Noah Green  <·······@interport.net> wrote:
>Hi,
>I've been searching thru some FAQs and stuff and am still stuck,
>& would love some help. I know some Scheme and ML and
>am just starting with Common Lisp. I'm trying to pass a function
>as an argument, and then use it within a function. For example:
>
>(defun is_member_of (equiv_rel element thelist)
>	(cond ((equiv_rel element (car thelist)) t)
>	      (t (is_member_of equiv_rel element (cdr thelist))))

In Common Lisp you have to use FUNCALL or APPLY to make use of a function
object in an expression:

    (funcall equiv_rel element (car thelist)) t)

CL has separate function and variable namespaces; when you just write
(equiv_rel ...) it looks for equiv_rel's function binding rather than its
variable binding.

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Cambridge, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
From: Hank Heijink
Subject: Re: Help: passing functions as arguments in Common Lisp
Date: 
Message-ID: <heijink-2704980944410001@fsw206155.socsci.kun.nl>
In article <·················@interport.net>, Noah Green
<·······@interport.net> wrote:

> Hi,
> I've been searching thru some FAQs and stuff and am still stuck,
> & would love some help. I know some Scheme and ML and
> am just starting with Common Lisp. I'm trying to pass a function
> as an argument, and then use it within a function. For example:
> 
> (defun is_member_of (equiv_rel element thelist)
>         (cond ((equiv_rel element (car thelist)) t)
>               (t (is_member_of equiv_rel element (cdr thelist))))

Use (funcall equiv_rel ...) instead of the call to equiv_rel, because LISP
uses separate namespaces for functions and variables, and it will look in
the wrong namespace.

> This is written to see if "element" is a member of "thelist," using
> "equiv_rel" as the boolean function to do the actual test. And to
> call it:
> 
> (is_member_of equal 'a '(a b c))

It should be (is_member_of #'equal 'a '(a b c)). #'equal is short for
(function equal) and now there's the reverse problem: equal is a function,
but LISP will look in the variable namespace if you don't tell LISP.

In general: in function applications, where a variable holds the function
that should be called, use funcall or apply. When a function has to be
passed as a variable, prefix the name with #'.

Hank Heijink