From: ·······@gmail.com
Subject: possibly silly question re quoting of function arguments...
Date: 
Message-ID: <1146235883.670788.203630@i40g2000cwc.googlegroups.com>
Hi,

i'm writing my first lisp programme and am trying to work out how to do
this:

i have a bunch of variables that look like:

(defvar myvar-x1 nil
  "1st x variable")

(defvar myvar-y1 nil
  "1st y variable")

i want to be able to push and pop items onto & out of myvar-x1 etc.

i want a predicate function that tells me whether a given list is of x
or y (e.g. myvar-x1 or myvar-y1)

(defun dest-is-xvar-p (alist)
  (if (eq alist myvar-x1)
       't
       'nil))

such that:

(dest-is-xvar-p myvar-x1) ;; this doesn't work...

returns true.

i can't work out how to do this without quoting the argument to
dest-is-xvar-p:

(defun dest-is-xvar-p (alist)
  (if (eq alist 'myvar-x1)
       't
       'nil))

(dest-is-xvar-p 'myvar-x1)

works, but i want to have a function which doesn't require me to quote
the argument.

can anyone suggest a simple lisp-ish way of doing this?

cheers,

B

From: Peter Seibel
Subject: Re: possibly silly question re quoting of function arguments...
Date: 
Message-ID: <m2fyjxbv9d.fsf@gigamonkeys.com>
········@gmail.com" <·······@gmail.com> writes:

> Hi,
>
> i'm writing my first lisp programme and am trying to work out how to do
> this:
>
> i have a bunch of variables that look like:
>
> (defvar myvar-x1 nil
>   "1st x variable")
>
> (defvar myvar-y1 nil
>   "1st y variable")
>
> i want to be able to push and pop items onto & out of myvar-x1 etc.
>
> i want a predicate function that tells me whether a given list is of x
> or y (e.g. myvar-x1 or myvar-y1)
>
> (defun dest-is-xvar-p (alist)
>   (if (eq alist myvar-x1)
>        't
>        'nil))

You can just write that:

  (defun dest-is-xvar-p (alist)
    (eq alist myvar-x1))

Though I subscribe to the school of thought that you should always use
EQL and never EQ. And a lisper is going to read alist as an
abbreviation for "association list" rather than "a list". But luckily
this isn't Scheme so we can use the 'list' as a variable name. So I'd
write it:

  (defun dest-is-xvar-p (list)
    (eql list myvar-x1))

> such that:
>
> (dest-is-xvar-p myvar-x1) ;; this doesn't work...
>
> returns true.

What did you expect it to return. You asked, (eq myvar-x1 myvar-x1);
of course it's true.

> i can't work out how to do this without quoting the argument to
> dest-is-xvar-p:
>
> (defun dest-is-xvar-p (alist)
>   (if (eq alist 'myvar-x1)
>        't
>        'nil))
>
> (dest-is-xvar-p 'myvar-x1)

Doesn't this also return true? Now you're asking (eq 'myvar-x1
'myvar-x1) which is also true but for a different reason.

> works, but i want to have a function which doesn't require me to
> quote the argument.
>
> can anyone suggest a simple lisp-ish way of doing this?

It's not clear what "this" is. Maybe you can step back and explain
what you're really trying to do.

-Peter

-- 
Peter Seibel           * ·····@gigamonkeys.com
Gigamonkeys Consulting * http://www.gigamonkeys.com/
Practical Common Lisp  * http://www.gigamonkeys.com/book/
From: ·······@gmail.com
Subject: Re: possibly silly question re quoting of function arguments...
Date: 
Message-ID: <1146237682.499064.17490@i40g2000cwc.googlegroups.com>
Hi,

i want a predicate function that tells me whether a given list is of x
or y (e.g. myvar-x1 or myvar-y1) without having to quote the argument i
pass to the function...

you misread my post (my fault for the way i wrote it probably!):

----
(defun dest-is-xvar-p (list)
 (eql list myvar-x1))

such that:

(dest-is-xvar-p myvar-x1)

returns true.
----

with the above, dest-is-xvar-p returns true for any empty list...

what i'm looking for is an sensible way to make it distinguish between
myvar-x1
and myvar-y1 without having to quote the function argument...

Hope this is clearer?

cheers,

B
From: Don Geddis
Subject: Re: possibly silly question re quoting of function arguments...
Date: 
Message-ID: <87mze5puza.fsf@geddis.org>
········@gmail.com" <·······@gmail.com> wrote on 28 Apr 2006 08:2:
> what i'm looking for is an sensible way to make it distinguish between
> myvar-x1 and myvar-y1 without having to quote the function argument...

Well, if you use a macro (DEFMACRO instead of DEFUN), that will have the
apparent effect of "automatically" quoting the arguments, so the user doesn't
have to.

But I agree with Peter.  You probably have some fundamental confusions about
how Lisp works at all, and are really trying to do something that doesn't
make any sense.  It would be better if you step back a level, and explain
what you're trying to achieve.  Forget about implementation for a second.
You're clearly building a module of some kind to perform some actions.  What's
the purpose of the module?  What the ideal API you'd like to implement?  If
you tell us these things, we can guide you to a good lisp implementation of
your concepts.

As it is, you appear confused about argument passing in lisp.  All that
call-by-value vs. call-by-reference stuff, vs. what lisp actually does
("call-by-identity"?).

        -- Don
_______________________________________________________________________________
Don Geddis                  http://don.geddis.org/               ···@geddis.org
Against stupidity, the gods themselves contend in vain.
("Mit der Dummheit k�mpfen G�tter selbst vergebens.")
	-- Friedrich von Schiller (1759-1805), "Maid of Orleans"
From: ·······@gmail.com
Subject: Re: possibly silly question re quoting of function arguments...
Date: 
Message-ID: <1146242446.546129.50640@y43g2000cwc.googlegroups.com>
i certainly have many fundamental confusions about how lisp works...!

but how can i actually learn it if i don't try to write programmes?

the problem wa sneatly expressed by Takehiko's post: i kind of just
expected there to be a simple lispish way of distinguishing 2 lists...

cheers,

B
From: ·······@gmail.com
Subject: Re: possibly silly question re quoting of function arguments...
Date: 
Message-ID: <1146242532.581456.200160@i39g2000cwa.googlegroups.com>
sorry: read 2 empty lists...

B
From: jayessay
Subject: Re: possibly silly question re quoting of function arguments...
Date: 
Message-ID: <m3k6994md9.fsf@rigel.goldenthreadtech.com>
········@gmail.com" <·······@gmail.com> writes:

> sorry: read 2 empty lists...

Actually, this is isn't what you wanted either - you wanted to tell
the difference between two "locations" or variables.


/Jon

-- 
'j' - a n t h o n y at romeo/charley/november com
From: jayessay
Subject: Re: possibly silly question re quoting of function arguments...
Date: 
Message-ID: <m3odyl4p2d.fsf@rigel.goldenthreadtech.com>
········@gmail.com" <·······@gmail.com> writes:


> i want a predicate function that tells me whether a given list is of x
> or y (e.g. myvar-x1 or myvar-y1) without having to quote the argument i
> pass to the function...

I don't know why you want this, but presumably what you want is some
way of identifying if some object is the actual location for X or Y
(in this context, that would be the actual symbols for X and Y).

First, what's the problem with quoting the argument?  You just don't
like that?  Or what?


> ----
> (defun dest-is-xvar-p (list)
>  (eql list myvar-x1))
> 
> such that:
> 
> (dest-is-xvar-p myvar-x1)
> 
> returns true.
> ----

(defmacro dest-is-xvar-p (list)
  `(eq ',list 'myvar-x1))

(dest-is-xvar-p myvar-x1)
=> t

(dest-is-xvar-p abc)
=> nil


> what i'm looking for is an sensible way to make it distinguish between
> myvar-x1
> and myvar-y1 without having to quote the function argument...

For the use case you give, you want a macro (since you want to compare
UNevaluated objects).  But in a more general setting, where the object
of interest is already in hand due to other processing (e.g., reading
something or generating something or whatever), then you _could_ simply
use a function:

(defun dest0is-xvar-p (list)
  (eq list 'myvar-x1))


> Hope this is clearer?

Not really.


/Jon

-- 
'j' - a n t h o n y at romeo/charley/november com
From: ·······@gmail.com
Subject: Re: possibly silly question re quoting of function arguments...
Date: 
Message-ID: <1146241678.510078.158780@j33g2000cwa.googlegroups.com>
Hi Jon,

(defmacro dest-is-xvar-p (list)
  `(eq ',list 'myvar-x1))

appears to do exactly what i want to do (i'm learning lisp and decided
to write a card game for emacs as an exercise)...

many thanks!

B
From: Barry Margolin
Subject: Re: possibly silly question re quoting of function arguments...
Date: 
Message-ID: <barmar-6B778A.21062528042006@comcast.dca.giganews.com>
In article <························@j33g2000cwa.googlegroups.com>,
 ········@gmail.com" <·······@gmail.com> wrote:

> Hi Jon,
> 
> (defmacro dest-is-xvar-p (list)
>   `(eq ',list 'myvar-x1))
> 
> appears to do exactly what i want to do (i'm learning lisp and decided
> to write a card game for emacs as an exercise)...
> 
> many thanks!
> 
> B

Suppose you do:

(setq otherlist-x1 mylist-x1)

(dest-is-xvar-p otherlist-x1)

Your macro will return false, is this what you want?

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Peter Seibel
Subject: Re: possibly silly question re quoting of function arguments...
Date: 
Message-ID: <m2aca5btv6.fsf@gigamonkeys.com>
········@gmail.com" <·······@gmail.com> writes:

> Hi,
>
> i want a predicate function that tells me whether a given list is of x
> or y (e.g. myvar-x1 or myvar-y1) without having to quote the argument i
> pass to the function...
>
> you misread my post (my fault for the way i wrote it probably!):
>
> ----
> (defun dest-is-xvar-p (list)
>  (eql list myvar-x1))
>
> such that:
>
> (dest-is-xvar-p myvar-x1)
>
> returns true.
> ----
>
> with the above, dest-is-xvar-p returns true for any empty list...

That's because there's only one empty list and because functions just
get values; they don't know where those values came from. So when you
say:

  (dest-is-xvar-p myvar-x1)

and the current value of myvar-x1 is NIL it's exactly the same as if you said:

  (dest-is-xvar-p NIL)

Basically you can't do what you think you want to do. The good news is
that when you step back and explain what you're really trying to do
(i.e. why do you think you need to do this) someone here will be able
to tell you the Lispy way to do *that*.

-Peter

-- 
Peter Seibel           * ·····@gigamonkeys.com
Gigamonkeys Consulting * http://www.gigamonkeys.com/
Practical Common Lisp  * http://www.gigamonkeys.com/book/
From: ·······@gmail.com
Subject: Re: possibly silly question re quoting of function arguments...
Date: 
Message-ID: <1146239100.300315.21530@j73g2000cwa.googlegroups.com>
I have two groups of lists: x & y

i need to push values onto these lists from a list z

i need to test whether user input specifies a group x list or a group y
list as a destination for items from z

any clearer?

B
From: Pascal Bourguignon
Subject: Re: possibly silly question re quoting of function arguments...
Date: 
Message-ID: <8764kt8im9.fsf@thalassa.informatimago.com>
········@gmail.com" <·······@gmail.com> writes:

> I have two groups of lists: x & y
>
> i need to push values onto these lists from a list z
>
> i need to test whether user input specifies a group x list or a group y
> list as a destination for items from z
>
> any clearer?

Yes. Then you can write it as you said it:

(defvar *myvar-x1* '()) 
(defvar *myvar-y1* '())

; note the convention to use: 
;  nil for the boolean false
; 'nil for the symbol nil
;  ()  for empty lists in programs
; '()  for empty lists in data

(defun push-onto-category (value category)
   (ecase category
     ((x)  (push value *myvar-x1*))
     ((y)  (push value *myvar-y1*))))

(let ((z '((x 1) (y 2) (x 3) (y 4) (x 5))))
  (dolist (category-value z)
     (push-onto-category (second category-value) (first category-value))))

(push-onto-category 0 'y)

*myvar-x1* --> (5 3 1)
*myvar-y1* --> (0 4 2)


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Small brave carnivores
Kill pine cones and mosquitoes
Fear vacuum cleaner
From: Takehiko Abe
Subject: Re: possibly silly question re quoting of function arguments...
Date: 
Message-ID: <keke-2904060125020001@192.168.1.2>
Peter Seibel wrote:

> Basically you can't do what you think you want to do. The good news is
> that when you step back and explain what you're really trying to do
> (i.e. why do you think you need to do this) someone here will be able
> to tell you the Lispy way to do *that*.

He wants to distinguish two lists -- possibly empty ones.
It is not obvious why two empty lists are EQ:

* (eq (list) (list))  --> T

While this works:

(defvar *place-1* (make-array 0 :adjustable t :fill-pointer 0))
(defvar *place-2* (make-array 0 :adjustable t :fill-pointer 0))

(defun place-1-p (place)
  (eq place *place-1*))

(place-1-p *place-2*) --> nil
(place-1-p *place-1*) --> t


I know that empty list == symbol NIL, and that has not
yet caused me any grief. But I think it can be confusing to
new comers.
From: ·······@gmail.com
Subject: Re: possibly silly question re quoting of function arguments...
Date: 
Message-ID: <1146242060.014339.37910@i40g2000cwc.googlegroups.com>
exactly!

since this is a learning exercise i have set myself, i was deliberately
trying to use only lists dand defuns to do this...

of course, that might have been a stupid objective to set myself...

indeed, apparently it was...

thx for the help!

B
From: Thomas A. Russ
Subject: Re: possibly silly question re quoting of function arguments...
Date: 
Message-ID: <ymi3bfxtshw.fsf@sevak.isi.edu>
····@gol.com (Takehiko Abe) writes:

> Peter Seibel wrote:
> 
> > Basically you can't do what you think you want to do. The good news is
> > that when you step back and explain what you're really trying to do
> > (i.e. why do you think you need to do this) someone here will be able
> > to tell you the Lispy way to do *that*.
> 
> He wants to distinguish two lists -- possibly empty ones.
> It is not obvious why two empty lists are EQ:
> 
> * (eq (list) (list))  --> T
> 
> While this works:
> 
> (defvar *place-1* (make-array 0 :adjustable t :fill-pointer 0))
> (defvar *place-2* (make-array 0 :adjustable t :fill-pointer 0))
> 
> (defun place-1-p (place)
>   (eq place *place-1*))
> 
> (place-1-p *place-2*) --> nil
> (place-1-p *place-1*) --> t

You can get a similar effect using lists, but you have to adopt a
convention where you, say, skip the first element to get at the actual
data.  This relies on the two lists being distinct objects, but that
isn't true of empty lists---so you have to use non-empty ones.

(defvar *place-1* (list nil))
(defvar *place-2* (list nil))
(defun get-p1 ()
   (cdr *place-1*))
(defun add-to-p1 (item)
   (push item (cdr *place-1*)))

Then you can actually do the tests.

As Peter Seibel notes, though, there is most likely a better way to do
this.  If you have user input that dictates which list to use, then you
can use that as the key to dispatch to the appropriate list, or use a
hash-table or an association list or some other data structure.  That
would be a better and more Lispy solution.




-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Takehiko Abe
Subject: Re: possibly silly question re quoting of function arguments...
Date: 
Message-ID: <keke-2904061249440001@192.168.1.2>
Thomas A. Russ wrote:

> As Peter Seibel notes, though, there is most likely a better way to do
> this.  

I agree. However, I think there are a few interesting points
to learn by analyzing more why his code didn't work --that's
more interesting than getting "a better way".