From: ryba4
Subject: How to make a list of values, not symbols?
Date: 
Message-ID: <8f6928ed-6477-46b3-b661-4c80e37ebc86@c65g2000hsa.googlegroups.com>
Hello all
A newbie question.
I have a function that looks like

(defun myfunc (arg1 arg2)
(< (nth 1 arg1) (nth 1 arg2)))

If I call it this way, it works (it gets lists of numbers)
(myfunc '(1 2 3) '(4 5 6))

but if I call it with lists of variables, the arguments become lists
of symbols and < throws an error:

(setq a 1 b 2 c 3)
(myfunc '(a b c) '(a b c))

How do I form a list of values instead of symbols?

Best regards

From: Rainer Joswig
Subject: Re: How to make a list of values, not symbols?
Date: 
Message-ID: <joswig-39CAFA.22160604082008@news-europe.giganews.com>
In article 
<····································@c65g2000hsa.googlegroups.com>,
 ryba4 <···············@gmail.com> wrote:

> Hello all
> A newbie question.
> I have a function that looks like
> 
> (defun myfunc (arg1 arg2)
> (< (nth 1 arg1) (nth 1 arg2)))
> 
> If I call it this way, it works (it gets lists of numbers)
> (myfunc '(1 2 3) '(4 5 6))
> 
> but if I call it with lists of variables, the arguments become lists
> of symbols and < throws an error:
> 
> (setq a 1 b 2 c 3)
> (myfunc '(a b c) '(a b c))
> 
> How do I form a list of values instead of symbols?
> 
> Best regards

(list a b c)  returns a list of the values of a, b and c.

-- 
http://lispm.dyndns.org/
From: ryba4
Subject: Re: How to make a list of values, not symbols?
Date: 
Message-ID: <2d1e9157-43e2-4547-bb77-c2f0f21a9247@f63g2000hsf.googlegroups.com>
On Aug 5, 3:16 am, Rainer Joswig <······@lisp.de> wrote:
> (list a b c)  returns a list of the values of a, b and c.

Thank you!
From: Pascal J. Bourguignon
Subject: Re: How to make a list of values, not symbols?
Date: 
Message-ID: <87sktkse8c.fsf@hubble.informatimago.com>
ryba4 <···············@gmail.com> writes:

> Hello all
> A newbie question.
> I have a function that looks like
>
> (defun myfunc (arg1 arg2)
> (< (nth 1 arg1) (nth 1 arg2)))
>
> If I call it this way, it works (it gets lists of numbers)
> (myfunc '(1 2 3) '(4 5 6))
>
> but if I call it with lists of variables, the arguments become lists
> of symbols and < throws an error:
>
> (setq a 1 b 2 c 3)
> (myfunc '(a b c) '(a b c))

'(a b c) doesn't evaluate to a list of variable, it evaluates to a
list of symbols, as you noticed.

Actually, there is no way to get a list of variable.


Notice how the CLHS Glossary defines:

   variable n. a binding in the ``variable'' namespace. See Section
   3.1.2.1.1 (Symbols as Forms).

and unfortunately, the Common Lisp doesn't define portably any
operation of environment allowing us to extract these bindings in the
"variable" namespace.


> How do I form a list of values instead of symbols?

Ah, so it is not a list of variables that you want. It's a list of values!

Then say it: (list a b c)

(let ((a 1) (b 2) (c 3))
  (myfunc (list a b c) (list a b c)))


Notice how it is important to use the right word?  Not at all the same answer you get.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

"Logiciels libres : nourris au code source sans farine animale."
From: ryba4
Subject: Re: How to make a list of values, not symbols?
Date: 
Message-ID: <eb29cb9c-e0c1-4cd5-960a-24051abb6173@p25g2000hsf.googlegroups.com>
Thanks for a notice,
BTW, is there a way to filter the arguments inside the function? So
that it were possible to pass lists of symbols without problems. I've
tried (eval arg1), but this way lisp attempts to execute it like (a b
c).
From: Pascal J. Bourguignon
Subject: Re: How to make a list of values, not symbols?
Date: 
Message-ID: <87k5ewrnju.fsf@hubble.informatimago.com>
ryba4 <···············@gmail.com> writes:

> Thanks for a notice,
> BTW, is there a way to filter the arguments inside the function? So
> that it were possible to pass lists of symbols without problems. I've
> tried (eval arg1), but this way lisp attempts to execute it like (a b
> c).

What do you want to do?

As I explained in my first response, there is no way to access to the
environment at run-time.  You won't be able to access to the lexical
variable values, because the compiler lose the association between the
variable name and the binding:


C/USER[170]> (disassemble (compile nil (lambda (x) (let ((a 1) (b 2))
                                       (incf a x)
                                       (decf b x)
                                       (if (zerop x) (+ x a) (* x b))))))

Disassembly of function NIL
(CONST 0) = 1
(CONST 1) = 2
1 required argument
0 optional arguments
No rest parameter
No keyword parameters
19 byte-code instructions:
0     (CONST&PUSH 0)                      ; 1
1     (CONST&PUSH 1)                      ; 2
2     (LOAD&PUSH 1)
3     (LOAD&PUSH 4)
4     (CALLSR&STORE 2 53 1)               ; +
8     (LOAD&PUSH 0)
9     (LOAD&PUSH 4)
10    (CALLSR&STORE 1 54 0)               ; -
14    (LOAD&PUSH 3)
15    (CALLS2&JMPIF 146 L25)              ; ZEROP
18    (LOAD&PUSH 3)
19    (LOAD&PUSH 1)
20    (CALLSR 2 55)                       ; *
23    (SKIP&RET 4)
25    L25
25    (LOAD&PUSH 3)
26    (LOAD&PUSH 2)
27    (CALLSR 2 53)                       ; +
30    (SKIP&RET 4)
NIL
C/USER[171]> 

See?  There is absolutely NO reference to the symbols A or B or X,
naming the lexical variables of that function.


So what you seem to be asking, is still as impossible as the first
time you asked it.








There is one way you could embed a reference to a variable, it's with
a closure.

(defmacro make-getter (variable) `(lambda () ,variable))
(defun    get-value   (getter)   (funcall getter))

(defun myfunc (arg1 arg2)
   (< (get-value (nth 1 arg1)) (get-value (nth 1 arg2))))

(let ((a 1) (b 2) (c 3))
  (myfunc (list (make-getter a) (make-getter b) (make-getter c))
          (list (make-getter a) (make-getter b) (make-getter c))))

For more information see:
http://groups.google.com/group/comp.lang.lisp/msg/2183b41e2a37c258
http://groups.google.com/group/comp.lang.lisp/msg/1799d5db9267c523
or search comp.lang.lisp for 'locative'.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

"A TRUE Klingon warrior does not comment his code!"
From: Pascal J. Bourguignon
Subject: Re: How to make a list of values, not symbols?
Date: 
Message-ID: <7ciqufj3y9.fsf@pbourguignon.anevia.com>
ryba4 <···············@gmail.com> writes:

> Thanks for a notice,
> BTW, is there a way to filter the arguments inside the function? So
> that it were possible to pass lists of symbols without problems. I've
> tried (eval arg1), but this way lisp attempts to execute it like (a b
> c).

Ok, if  you insist, there is one special kind of variables you could
access.  The SPECIAL variables.

Those are the variables declared with DEFVAR or DEFPARAMETER.

Since they are SPECIAL, we have the convention of naming them with a
name starting and ending with a star.  If you don't do that, you could
get nasty surprises, because these variables have a SPECIAL behavior.

We can get the value of these special variables with SYMBOL-VALUE.

So, let's say:

(defparameter *a* 1)
(defparameter *b* 2)
(defparameter *c* 3)

(defun myfunc (arg1 arg2)
  (< (symbol-value (nth 1 arg1)) (symbol-value (nth 1 arg2))))

(myfunc '(*a* *b* *c*)  '(*a* *b* *c*))


And now, if you want to do it for both symbols naming SPECIAL
variables and other values, you could define:

(defgeneric value-of (thing)
  (:documentation "Returns the value of THING, which is 
either a symbol naming a special variable, or a THING by itself.")
  (:method ((thing symbol)) (symbol-value thing))
  (:method ((thing t))      thing))


(defun myfunc (arg1 arg2)
  (< (value-of (nth 1 arg1)) (value-of (nth 1 arg2))))

(myfunc '(*a* *b* *c*) '(1 2 3))



But it won't work for:

(myfunc '(*a* *b* *c*) '(1 (+ 1 1) 3))

(the best way to make it work in all cases, is to use closures as
indicated in my other answer).

-- 
__Pascal Bourguignon__
From: verec
Subject: Re: How to make a list of values, not symbols?
Date: 
Message-ID: <4898e980$0$722$5a6aecb4@news.aaisp.net.uk>
On 2008-08-04 21:10:27 +0100, ryba4 <···············@gmail.com> said:

> Hello all
> A newbie question.
> I have a function that looks like
> 
> (defun myfunc (arg1 arg2)
> (< (nth 1 arg1) (nth 1 arg2)))
> 
> If I call it this way, it works (it gets lists of numbers)
> (myfunc '(1 2 3) '(4 5 6))
> 
> but if I call it with lists of variables, the arguments become lists
> of symbols and < throws an error:
> 
> (setq a 1 b 2 c 3)
> (myfunc '(a b c) '(a b c))
> 
> How do I form a list of values instead of symbols?
> 
> Best regards

Just for the fun of it :-)

(defun myfunc (arg1 arg2)
  (< (nth 1 arg1) (nth 1 arg2)))

CL-USER 1 > (myfunc '(1 2 3) '(4 5 6))
T

CL-USER 2 > (setf a 1 b 2 c 3)
3

CL-USER 3 > a
1

CL-USER 4 > b
2

CL-USER 5 > c
3

CL-USER 6 > (myfunc `(,a ,b ,c) `(,b ,c ,a))
T

--
JFB