From: David R. McIntyre
Subject: LISP Novice Dumb Question (I think) or could it actually be my implementation...
Date: 
Message-ID: <363E3C55.A935C736@cis.csuohio.edu>
All the following work as expected:

>(setf fn '+)
+
>(setf x 5)
5
>`(,fn x 4)
(+   X  4)
>(eval `(,fn  X  4))
9

BUT how do you explain the following (LISPWORKS) error?:

>(defun a (fn x)
       (eval `(,fn  x  4))
  )
A
>(a  '+  5)
ERROR: "Variable X is unbound"

From: michael hohn
Subject: Re: LISP Novice Dumb Question (I think) or could it actually be my implementation...
Date: 
Message-ID: <363E5738.3EC139C8@math.utah.edu>
David R. McIntyre wrote:

> All the following work as expected:
>
> >(setf fn '+)
> +
> >(setf x 5)
> 5
> >`(,fn x 4)
> (+   X  4)
> >(eval `(,fn  X  4))
> 9
>
> BUT how do you explain the following (LISPWORKS) error?:
>
> >(defun a (fn x)
>        (eval `(,fn  x  4))
>   )
> A
> >(a  '+  5)
> ERROR: "Variable X is unbound"

  I tried the following in clisp:

> (defun a (fn x)  (eval `(,fn  x  4)))
A
> (a '+ 5)

*** - EVAL: variable X has no value
1. Break>
> (defun a (fn x)  (eval `(,fn ,x 4)))
A
> (a '+ 5)
9

Although the use of eval is probably a bad idea...
Michael
From: Barry Margolin
Subject: Re: LISP Novice Dumb Question (I think) or could it actually be my implementation...
Date: 
Message-ID: <6is%1.18$gD6.944117@burlma1-snr1.gtei.net>
In article <·················@cis.csuohio.edu>,
David R. McIntyre <········@cis.csuohio.edu> wrote:
>BUT how do you explain the following (LISPWORKS) error?:
>
>>(defun a (fn x)
>       (eval `(,fn  x  4))
>  )
>A
>>(a  '+  5)
>ERROR: "Variable X is unbound"

EVAL evaluates the expression in the null lexical environment.  When you
call A, X's binding is in the local lexical environment.

What is it you're really trying do do?  Why not use:

(defun a (fn x)
  (funcall (symbol-function fn) x 4))

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Don't bother cc'ing followups to me.
From: ······@cit.org.by
Subject: Re: LISP Novice Dumb Question (I think) or could it actually be my implementation...
Date: 
Message-ID: <71mrn4$rfi$1@nnrp1.dejanews.com>
In article <·················@cis.csuohio.edu>,
  "David R. McIntyre" <········@cis.csuohio.edu> wrote:
> All the following work as expected:
>
> >(setf fn '+)
> +
> >(setf x 5)
> 5
> >`(,fn x 4)
> (+   X  4)
> >(eval `(,fn  X  4))
> 9
>
> BUT how do you explain the following (LISPWORKS) error?:
>
> >(defun a (fn x)
>        (eval `(,fn  x  4))
>   )
> A
> >(a  '+  5)
> ERROR: "Variable X is unbound"
>

Here's an excerpt from ACL help:

EVAL Description: evaluates the value of form in the current dynamic
environment and an empty lexical environment.  ^^^^^

As lambda-list arguments bound by lexical scope, variable X seems to be
unbound. There's no such problem with fn argument due to macroexpansion of
backquote, which proceeds in a current scope. Try this:

(defun a (fn x)
   (eval `(,fn  ,x  4)))

(a  '+  5)

Cheers,
   Eugene Zaikonnikov



-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    
From: Barry Margolin
Subject: Re: LISP Novice Dumb Question (I think) or could it actually be my implementation...
Date: 
Message-ID: <NEF%1.27$gD6.1252831@burlma1-snr1.gtei.net>
In article <············@nnrp1.dejanews.com>,  <······@cit.org.by> wrote:
>As lambda-list arguments bound by lexical scope, variable X seems to be
>unbound. There's no such problem with fn argument due to macroexpansion of
>backquote, which proceeds in a current scope. Try this:
>
>(defun a (fn x)
>   (eval `(,fn  ,x  4)))
>
>(a  '+  5)

If you're going to use that technique, you should quote the result of the
expansion, i.e.

(defun a (fn x)
  (eval (,fn ',x 4)))

Otherwise, you'll get double evaluation.  See the difference if you do:

(a 'cons 'y)

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Don't bother cc'ing followups to me.