From: Ap
Subject: hello, i want to clone a list
Date: 
Message-ID: <1109251468.638636.221150@o13g2000cwo.googlegroups.com>
Hello i'm trying to clone i list, so far i have this function

(defun mod-list (lista)
  (let ((lista-clone lista))
    (list (setf (nth 1 lista) 3) lista lista-clone)))

if i do
(mod-list '(a b c d))
i get
(3 (a 3 c d) (a 3 c d)) <- wrong
(3 (a 3 c d) (a b c d)) <- this is the answer that i want

i understand that if i pass a list as an argument to a function only
the pointer to the memory
location is actually pass. so my question:

1) Is there any way to clone a list inside a function?

One more question
inside a "cond" statement how do i call more than one funcion

for example if i try to do it like this, i get an error:
...(cond ((null list) ((function1) (function2) (function3)))....

what i usually do is
...(cond ((null list) (list (function1) (function2) (function3)))...

but this gives me a lot of unusefull output. my question is:

2) how can i call more than one function in a "cond" statement?


Thanks you very much!!!
please question 1 is very important :)

From: Thomas Stenhaug
Subject: Re: hello, i want to clone a list
Date: 
Message-ID: <1xb6dqoj.fsf@src.no>
"Ap" <·······@gmail.com> writes:

> 1) Is there any way to clone a list inside a function?

I think you want copy-list

   <http://www.lisp.org/HyperSpec/Body/fun_copy-list.html>

> 2) how can i call more than one function in a "cond" statement?

Try this:

(cond ((null list)
       (function1)
       (function2)
       ...)
      ...)

Thomas
From: Marco Antoniotti
Subject: Re: hello, i want to clone a list
Date: 
Message-ID: <0orTd.35$fp1.52970@typhoon.nyu.edu>
Thomas Stenhaug wrote:
> "Ap" <·······@gmail.com> writes:
> 
> 
>>1) Is there any way to clone a list inside a function?
> 
> 
> I think you want copy-list
> 
>    <http://www.lisp.org/HyperSpec/Body/fun_copy-list.html>

Nope.  He wants COPY-TREE.

http://www.lisp.org/HyperSpec/Body/fun_copy-tree.html

Cheers
--
Marco
From: Thomas Stenhaug
Subject: Re: hello, i want to clone a list
Date: 
Message-ID: <k6oweobc.fsf@src.no>
Marco Antoniotti <·······@cs.nyu.edu> writes:

> Nope.  He wants COPY-TREE.

Indeed he does.  Sorry, OP.


Thomas
From: Frode Vatvedt Fjeld
Subject: Re: hello, i want to clone a list
Date: 
Message-ID: <2hoeeam5xj.fsf@vserver.cs.uit.no>
"Ap" <·······@gmail.com> writes:

> (defun mod-list (lista)
>   (let ((lista-clone lista))
>     (list (setf (nth 1 lista) 3) lista lista-clone)))
>
> if i do
> (mod-list '(a b c d))
> i get
> (3 (a 3 c d) (a 3 c d)) <- wrong
> (3 (a 3 c d) (a b c d)) <- this is the answer that i want

  (defun mod-list (lista)
    (let ((lista-clone (copy-list lista)))
      (list (setf (nth 1 lista-clone) 3) lista-clone lista)))

> One more question
> inside a "cond" statement how do i call more than one funcion

You simply do it..

> for example if i try to do it like this, i get an error:
> ...(cond ((null list) ((function1) (function2) (function3)))....

..like this:

  (cond ((null list) (function1) (function2) (function3)))

I'd advise you to find an introductory book on Lisp and read it, and
then learn to use and read the CLHS.

>
> what i usually do is
> ...(cond ((null list) (list (function1) (function2) (function3)))...
>
> but this gives me a lot of unusefull output. my question is:

If you substitute "progn" for "list" you'd get what you wanted. But as
it happens, the progn is implicit at this particular position inside
cond, so you don't really need anything, as shown above.

-- 
Frode Vatvedt Fjeld
From: Ap
Subject: Re: hello, i want to clone a list
Date: 
Message-ID: <1109254117.140609.295450@l41g2000cwc.googlegroups.com>
Thanks you very much for the answer

i am reading "Common Lisp: A Gentle Introduction to Symbolic
Computation" very good book (pdf online)

not finish yet :)
From: Harald Hanche-Olsen
Subject: Re: hello, i want to clone a list
Date: 
Message-ID: <pcoekf5it4r.fsf@shuttle.math.ntnu.no>
+ "Ap" <·······@gmail.com>:

| Thanks you very much for the answer
| 
| i am reading "Common Lisp: A Gentle Introduction to Symbolic
| Computation" very good book (pdf online)
| 
| not finish yet :)

Don't forget to look at http://www.gigamonkeys.com/book/ as well.
Also to appear in flattened dead tree form soon.

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- Debating gives most of us much more psychological satisfaction
  than thinking does: but it deprives us of whatever chance there is
  of getting closer to the truth.  -- C.P. Snow
From: John Thingstad
Subject: Re: hello, i want to clone a list
Date: 
Message-ID: <opsmpyy7f8pqzri1@mjolner.upc.no>
On 24 Feb 2005 05:24:28 -0800, Ap <·······@gmail.com> wrote:

> Hello i'm trying to clone i list, so far i have this function
>
> (defun mod-list (lista)
>   (let ((lista-clone lista))
>     (list (setf (nth 1 lista) 3) lista lista-clone)))
>

Not sure I know what you mean.

(setf a '(1 2 (3 (4 5) 6))
(setf b (copy-tree a)
(setf (caaddr b) 7)
a => (1 2 (3 (4 5) 6))
b => (1 2 (7 (4 5) 6))


-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/