From: Papastefanos Serafeim
Subject: Newbie question
Date: 
Message-ID: <bc07m0$1d8s$1@ulysses.noc.ntua.gr>
I want to pass a function as a parameter in another
function, and i am trying to do that with the following code in
Xlisp:
> (defun square(x) (* x x) )
SQUARE
> (defun koko(f a) (* 2 (f a) ) )
KOKO
Its all right 'till here. But when I am trying to test
that with:
> (koko square 3)
,i get the message: error: unbound variable - SQUARE
What am I doing wrong ? I have a book which says
that the above code is perfectly legal ! I had also tried
Clisp but the results were the same...

-- 
Papastefanos Serafeim
········@otenet.gr

From: Rmagere
Subject: Re: Newbie question
Date: 
Message-ID: <ve7a5gg3ugej6c@corp.supernews.com>
> I want to pass a function as a parameter in another
> function, and i am trying to do that with the following code in
> Xlisp:
> > (defun square(x) (* x x) )
> SQUARE
> > (defun koko(f a) (* 2 (f a) ) )
> KOKO
> Its all right 'till here. But when I am trying to test
> that with:
> > (koko square 3)
> ,i get the message: error: unbound variable - SQUARE
> What am I doing wrong ? I have a book which says
> that the above code is perfectly legal !
Not sure (though people will reply to you with more info) but probabably the
book you have is for Scheme

In CL you would write koko as follows:
(defun koko (f a) (* 2 (funcall f a)))

and call it:
(koko #'square 3) ==> 18

this is due to the fact that the function namespace is different from the
variables namespace
From: Jonatan Ring
Subject: Re: Newbie question
Date: 
Message-ID: <dXNEa.19353$8g5.318884@news2.e.nsc.no>
> Xlisp:
> > (defun square(x) (* x x) )
> SQUARE
> > (defun koko(f a) (* 2 (f a) ) )
> KOKO
> Its all right 'till here. But when I am trying to test
> that with:
> > (koko square 3)

I am very new to Lisp, but as far as I can see you tried to pass a symbol
which represent a variable to the koko function, whereas you actually want
to pass a function. Im not sure about this but perhaps you forgot some
parantheses or a quote. If you want to treat something as data you should
(according to my tutorial) use either ' or QUOTE. If you do not Lisp will
probably try to evaluate square, which is an unbound variable since you
didnt define it with setq. Im a newbe though so I may be wrong here.
From: Rainer Joswig
Subject: Re: Newbie question
Date: 
Message-ID: <joswig-AFEEC9.23565508062003@news.fu-berlin.de>
In article <······················@news2.e.nsc.no>,
 "Jonatan Ring" <········@online.no> wrote:

> > Xlisp:
> > > (defun square(x) (* x x) )
> > SQUARE
> > > (defun koko(f a) (* 2 (f a) ) )
> > KOKO
> > Its all right 'till here. But when I am trying to test
> > that with:
> > > (koko square 3)
> 
> I am very new to Lisp, but as far as I can see you tried to pass a symbol
> which represent a variable to the koko function,

No. He tries to pass the value of SQUARE.

CL-USER 1 > (defun square (x) (* x x))
SQUARE

CL-USER 2 > (defun koko (f a) (* 2 (funcall f a)))
KOKO


First version, we pass a symbol (see the quote):

CL-USER 3 > (koko 'square 3)
18


Second version, we pass the function object:

CL-USER 4 > (koko #'square 3)
18


Third version, same as second version, but differently written:

CL-USER 5 > (koko (function square) 3)
18
From: Michael Sullivan
Subject: Re: Newbie question
Date: 
Message-ID: <1fw9c6i.182ixby1uuoe29N%mes@panix.com>
Rainer Joswig <······@lispmachine.de> wrote:

> CL-USER 1 > (defun square (x) (* x x))
> SQUARE

> CL-USER 2 > (defun koko (f a) (* 2 (funcall f a)))
> KOKO

> First version, we pass a symbol (see the quote):
 
> CL-USER 3 > (koko 'square 3)
> 18

It never occured to me to try this...

What's the semantic difference here given that the function is going to
be funcalled.  Is there any good reason why this has not been adopted as
standard coding style.  I'm trying to come up with a way for this to go
wrong and failing.


Michael
From: Matthew Danish
Subject: Re: Newbie question
Date: 
Message-ID: <20030609070104.GA17568@lain.mapcar.org>
On Sun, Jun 08, 2003 at 11:43:47PM -0400, Michael Sullivan wrote:
> Rainer Joswig <······@lispmachine.de> wrote:
> > CL-USER 3 > (koko 'square 3)
> > 18
> 
> It never occured to me to try this...
> 
> What's the semantic difference here given that the function is going to
> be funcalled.  Is there any good reason why this has not been adopted as
> standard coding style.  I'm trying to come up with a way for this to go
> wrong and failing.

The semantic difference is that there is another level of indirection:
the function must be looked up from the symbol each time it is
funcalled.  

(defun bar () (print 1))

(defun foo (f)
  (let ((v (funcall f)))
    (defun bar () (print (1+ v)))
    (funcall f)))

See the difference between (foo 'bar) and (foo #'bar).

Of course, another consequence is that only functions bound to symbols
can be referred to with quote.  Lexically bound functions aren't
associated with symbols and therefore can only be referenced via
FUNCTION (and of course, with a normal function call).

-- 
; Matthew Danish <·······@andrew.cmu.edu>
; OpenPGP public key: C24B6010 on keyring.debian.org
; Signed or encrypted mail welcome.
; "There is no dark side of the moon really; matter of fact, it's all dark."
From: Rudi Schlatte
Subject: Re: Newbie question
Date: 
Message-ID: <8765nf66on.fsf@semmel.constantly.at>
···@panix.com (Michael Sullivan) writes:

> Rainer Joswig <······@lispmachine.de> wrote:
>
>> CL-USER 1 > (defun square (x) (* x x))
>> SQUARE
>
>> CL-USER 2 > (defun koko (f a) (* 2 (funcall f a)))
>> KOKO
>
>> First version, we pass a symbol (see the quote):
>  
>> CL-USER 3 > (koko 'square 3)
>> 18
>
> It never occured to me to try this...
>
> What's the semantic difference here given that the function is going to
> be funcalled.  Is there any good reason why this has not been adopted as
> standard coding style.  I'm trying to come up with a way for this to go
> wrong and failing.
>

funcall takes a function designator (look it up in the HyperSpec).
Symbols and function objects have different semantics w.r.t.
redefinition:

CL-USER(3): (defun foo (x) (* x x))
FOO
CL-USER(16): (defvar *a1* 'foo)
*A1*
CL-USER(19): (defvar *a2* #'foo)
*A2*
CL-USER(22): (defun foo (x) (* x x x))
STYLE-WARNING: redefining FOO in DEFUN

FOO
CL-USER(27): (funcall *a1* 2)
8
CL-USER(30): (funcall *a2* 2)
4

During prototyping, I tend to store function names rather than
function objects, so that all places pick up the redefined function
automatically.  It's just another level of indirection.

Rudi
-- 
whois DRS1020334-NICAT                    http://constantly.at/pubkey.gpg.asc
     Key fingerprint = C182 F738 6B9A 83AF 9C25  62D9 EFAE 45A6 9A69 0867
From: Rainer Joswig
Subject: Re: Newbie question
Date: 
Message-ID: <joswig-2125FC.10050309062003@news.fu-berlin.de>
In article <···························@panix.com>,
 ···@panix.com (Michael Sullivan) wrote:

> Rainer Joswig <······@lispmachine.de> wrote:
> 
> > CL-USER 1 > (defun square (x) (* x x))
> > SQUARE
> 
> > CL-USER 2 > (defun koko (f a) (* 2 (funcall f a)))
> > KOKO
> 
> > First version, we pass a symbol (see the quote):
>  
> > CL-USER 3 > (koko 'square 3)
> > 18
> 
> It never occured to me to try this...
> 
> What's the semantic difference here given that the function is going to
> be funcalled.  Is there any good reason why this has not been adopted as
> standard coding style.  I'm trying to come up with a way for this to go
> wrong and failing.
> 
> 
> Michael

The symbol is a global name for a function object. Whenever you
want to go via the lookup using the name you would
use the symbol.

For example with CL-HTTP I can use functions to respond
to web requests:

(http:export-url #u"/foo-page"
  :computed
  :response-function 'foo-function)

Above version allows you to redefine FOO-FUNCTION
later and the redefined version will be used.

The next version will use a function object.
Even after redefining FOO-FUNCTION, /foo-page will
be computed by the older version. To get the
new version of FOO-FUNCTION into place, you would
have to reexecute the EXPORT-URL form.

(http:export-url #u"/foo-page"
  :computed
  :response-function #'foo-function)
From: Adam Warner
Subject: Re: Newbie question
Date: 
Message-ID: <pan.2003.06.09.04.52.17.402458@consulting.net.nz>
Hi Michael Sullivan,

>> CL-USER 1 > (defun square (x) (* x x))
>> SQUARE
> 
>> CL-USER 2 > (defun koko (f a) (* 2 (funcall f a)))
>> KOKO
> 
>> First version, we pass a symbol (see the quote):
>  
>> CL-USER 3 > (koko 'square 3)
>> 18
> 
> It never occured to me to try this...
> 
> What's the semantic difference here given that the function is going to
> be funcalled.  Is there any good reason why this has not been adopted as
> standard coding style.  I'm trying to come up with a way for this to go
> wrong and failing.

Since there are common examples of this style in ANSI Common Lisp I'd say
it's an acceptable coding style so long as its use doesn't lead to undue
ambiguity (and one only intents to supply arguments from the function
namespace, although lambda forms could supply data from the variable
namespace).

When you last compiled a function did you type (compile 'function-name) or
(compile #'function-name)?

DESCRIBE is another example. One can obtain information about functions
without having to supply a call to the function namespace.

Looking at some of the CMUCL code, DESCRIBE calls DESCRIBE-AUX which performs
a typecase to call DESCRIBE-SYMBOL for symbols and DESCRIBE-FUNCTION for
functions. If we head off to DESCRIBE-SYMBOL we see that it performs
analysis to detect if the symbol defines a value in the function namespace:

  (cond ((macro-function x)
         (describe-function (macro-function x) :macro x))
        ((special-operator-p x)
         (desc-doc x 'function "Special form"))
        ((fboundp x)
         (describe-function (fdefinition x) :function x)))

Thus DESCRIBE-FUNCTION finally gets called if a function value is bound to
the symbol.

* (defun test ())

test
* (fboundp 'test)

t
* (describe 'test)

test is an internal symbol in the COMMON-LISP-USER package.
Function: #<Interpreted Function test {4806B4D9}>
Function arguments:
  There are no arguments.
Its defined argument types are:
  nil
Its result type is:
  *
Its definition is:
  (lambda () (block test))


I suppose LAMBDA is another great example of not having to specify the
function namespace. I love being able to type (lambda (...) ...) and have
it macroexpand into (funcall (lambda (...) ...)).

Regards,
Adam
From: Kalle Olavi Niemitalo
Subject: Re: Newbie question
Date: 
Message-ID: <87vfvbcjl0.fsf@Astalo.kon.iki.fi>
"Adam Warner" <······@consulting.net.nz> writes:

> When you last compiled a function did you type (compile 'function-name) or
> (compile #'function-name)?

That's not a style issue.  (compile #'function-name) is an error.
From: Edi Weitz
Subject: Re: Newbie question
Date: 
Message-ID: <87fzmk8cq2.fsf@bird.agharta.de>
"Papastefanos Serafeim" <········@otenet.gr> writes:

> I want to pass a function as a parameter in another
> function, and i am trying to do that with the following code in
> Xlisp:
> > (defun square(x) (* x x) )
> SQUARE
> > (defun koko(f a) (* 2 (f a) ) )
> KOKO
> Its all right 'till here. But when I am trying to test
> that with:
> > (koko square 3)
> ,i get the message: error: unbound variable - SQUARE
> What am I doing wrong ? I have a book which says
> that the above code is perfectly legal ! I had also tried
> Clisp but the results were the same...

There are two languages of the Lisp family which are still in wide use
today (except for languages embedded in other applications like Emacs
Lisp or Autolisp) - these are Scheme and Common Lisp. They differ in
various aspects and one of them is the number of "namespaces" they
have. This difference makes the code above legal in Scheme and illegal
in Common Lisp so most likely your book is about Scheme (or some other
Lisp variant which in this respect is similar to Scheme).

You seem to have tested your code with CLISP (<http://clisp.sf.net/>)
which is an implementation of Common Lisp. In Common Lisp your code
would look like this:

  (defun square (x)
    (* x x))

  (defun koko (f a)
    (* 2 (funcall f a)))

  (koko #'square 3)

For more information about Common Lisp check out

  <http://www.lisp.org/>,
  <http://www.cliki.net/>,

or one of the online tutorials mentioned at the bottom of

  <http://cl-cookbook.sf.net/>.

For more information about Scheme you might want to ask in the
newsgroup comp.lang.scheme.

You mentioned XLisp which is yet another Lisp variant. I don't know
much about XLisp but If you don't have any specific reasons to use it
I'd suggest switching to something which is more widely used.

Good luck,
Edi.
From: Kenny Tilton
Subject: Re: Newbie question
Date: 
Message-ID: <3EE3BF34.7060008@nyc.rr.com>
Edi Weitz wrote:
> There are two languages of the Lisp family which are still in wide use
> today (except for languages embedded in other applications like Emacs
> Lisp or Autolisp) - these are Scheme and Common Lisp. 

And Logo?

-- 

  kenny tilton
  clinisys, inc
  http://www.tilton-technology.com/
  ---------------------------------------------------------------
"Everything is a cell." -- Alan Kay
From: Papastefanos Serafeim
Subject: Re: Newbie question
Date: 
Message-ID: <bc0d1g$1m1p$1@ulysses.noc.ntua.gr>
Thanks for all the answers, you were most helpful !

"Papastefanos Serafeim" <········@otenet.gr> wrote in message
··················@ulysses.noc.ntua.gr...
> I want to pass a function as a parameter in another
> function, and i am trying to do that with the following code in
> Xlisp:
> > (defun square(x) (* x x) )
> SQUARE
> > (defun koko(f a) (* 2 (f a) ) )
> KOKO
> Its all right 'till here. But when I am trying to test
> that with:
> > (koko square 3)
> ,i get the message: error: unbound variable - SQUARE
> What am I doing wrong ? I have a book which says
> that the above code is perfectly legal ! I had also tried
> Clisp but the results were the same...
>
> -- 
> Papastefanos Serafeim
> ········@otenet.gr
>
>
From: Kumar Balachandran
Subject: Re: Newbie question
Date: 
Message-ID: <IEREa.2384$qd3.1978@tornadotest1.news.pas.earthlink.net>
Papastefanos Serafeim wrote:
> I want to pass a function as a parameter in another
> function, and i am trying to do that with the following code in
> Xlisp:
> 
>>(defun square(x) (* x x) )
> 
> SQUARE

Think math. y = F(x) and z = G(y) implies z = (F o G) (x)

(defun twice (x) (* 2 x))

(defun compose (f g)
	#'(lambda (x) (funcall f (funcall g x))))

(defun koko(x) (funcall (compose #'square #'twice) x))
From: LIN8080
Subject: Re: Newbie question
Date: 
Message-ID: <3EEA2D26.2DDFD995@freenet.de>
Papastefanos Serafeim schrieb:

> and i am trying to do that with the following code in
> Xlisp:

Hallo

Can you tell me what version of x-lisp you are using? Is it dos or win?

stefan