From: Steve Long
Subject: Re: Newbie - 2 Small problems
Date: 
Message-ID: <3C8A2E76.477284F9@hotmail.com>
TejimaNoHimitsu wrote:

> I'm not sure if this is the right place to ask, but since it is a lisp
> forum I thought it couldn't hurt ;)
>
> 1)  Is there any way to test for option arguments in a function?  For
> example, if I do:
>
> (defun test (list1 list2 &key k)
>
> is there a way to test in the body of that function whether or not k
> exists?  If k doesn't exist, the value is NIL, but I don't know how to
> compare if something's value is NIL.  I know I should know, but I
> don't.  I would use an if statement, but k is either NIL or the key
> value....
>

By exists, you mean (boundp k) is non-nil? I believe an arg always has to
have something bound to it.

>
> 2)  is there an easy way to swap items in a list?  For example, I want
> to swap the 2nd and 4th items in the list '(1 4 3 2 5)....  is there
> an easy way to do it?  I can't explicitly set the value of an element
> in a list, can I?
>

Yes.  One non-destructive technique (function) might be

(defun swap-elt (seq n1 n2)
   (let ((new-seq (copy-seq seq)))
     (setf (elt new-seq n1) (elt seq n2))
     (setf (elt new-seq n2) (elt seq n1))
     new-seq))
swap-elt

(setf x '(1 2 3 4 5))
(1 2 3 4 5)

(swap-elt x 1 2)
(1 3 2 4 5)
x
(1 2 3 4 5)

(swap-elt x 0 4)
(5 2 3 4 1)
x
(1 2 3 4 5)

(setf x "god")
god
(swap-elt x 0 2)
dog


Many ways to skin this cat.

>
> Sorry for the wierd questions.  I've been trying to figure them out
> for hours but can't =(
>
> Thanks!

From: Thomas A. Russ
Subject: Re: Newbie - 2 Small problems
Date: 
Message-ID: <ymiofhueve5.fsf@sevak.isi.edu>
> TejimaNoHimitsu wrote:
> > 1)  Is there any way to test for option arguments in a function?  For
> > example, if I do:
> >
> > (defun test (list1 list2 &key k)
> >
> > is there a way to test in the body of that function whether or not k
> > exists?  If k doesn't exist, the value is NIL, but I don't know how to
> > compare if something's value is NIL.  I know I should know, but I
> > don't.  I would use an if statement, but k is either NIL or the key
> > value....
> >

Hmm, I seem to be missing some articles in this thread from my
newsreader, but I didn't see this particular one answered.

The answer is that one may specify more complicated patterns for keyword
(and optional) parameters.  In particular, the pattern 
    (<name> <default-value> <value-supplied-p>) 
solves exactly your problem:

(defun test (list1 list2 &key (k nil k-supplied-p))
  ...)

will have k-supplied-p bound to T if a value was supplied in the call
and it will be bound to NIL if no value was supplied (and the default
value was used instead).



-- 
Thomas A. Russ,  USC/Information Sciences Institute          ···@isi.edu    
From: Coby Beck
Subject: Re: Newbie - 2 Small problems
Date: 
Message-ID: <Fucj8.272809$A44.15109825@news2.calgary.shaw.ca>
"Thomas A. Russ" <···@sevak.isi.edu> wrote in message
····················@sevak.isi.edu...
> > TejimaNoHimitsu wrote:
> > > 1)  Is there any way to test for option arguments in a function?  For
> > > example, if I do:
> > >
> > > (defun test (list1 list2 &key k)
> > >
> > > is there a way to test in the body of that function whether or not k
> > > exists?  If k doesn't exist, the value is NIL, but I don't know how to
> > > compare if something's value is NIL.  I know I should know, but I
> > > don't.  I would use an if statement, but k is either NIL or the key
> > > value....
> > >
>
> Hmm, I seem to be missing some articles in this thread from my
> newsreader, but I didn't see this particular one answered.
>
> The answer is that one may specify more complicated patterns for keyword
> (and optional) parameters.  In particular, the pattern
>     (<name> <default-value> <value-supplied-p>)
> solves exactly your problem:
>
> (defun test (list1 list2 &key (k nil k-supplied-p))
>   ...)
>
> will have k-supplied-p bound to T if a value was supplied in the call
> and it will be bound to NIL if no value was supplied (and the default
> value was used instead).

I think every answer to this used the same k-supplied-p variable name.  Just
in case the OP reads too much into that coincidence I will point out you can
call it whatever you wish.

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Thomas A. Russ
Subject: Re: Newbie - 2 Small problems
Date: 
Message-ID: <ymiit81e64x.fsf@sevak.isi.edu>
"Coby Beck" <·····@mercury.bc.ca> writes:


> I think every answer to this used the same k-supplied-p variable name. 

Boy, talk about a pervasive naming convention!


-- 
Thomas A. Russ,  USC/Information Sciences Institute          ···@isi.edu