From: echo109
Subject: cloning elements in a list
Date: 
Message-ID: <vpUH9.2024$Rt1.32973@twister.southeast.rr.com>
Hello everyone,

Please somebody help me with the following question

How to write a lisp function which clones the top-level elements of a list L
(first arguement) n ( second argument) times. for eg. (clone 'A B C) 4 )
produces ( AAAABBBBCCCC)
Thanx
channa

From: John Gilson
Subject: Re: cloning elements in a list
Date: 
Message-ID: <kOUH9.223732$Up6.40085606@twister.nyc.rr.com>
"echo109" <·······@carolina.rr.com> wrote in message
·························@twister.southeast.rr.com...
> Hello everyone,
>
> Please somebody help me with the following question
>
> How to write a lisp function which clones the top-level elements of a list L
> (first arguement) n ( second argument) times. for eg. (clone 'A B C) 4 )
> produces ( AAAABBBBCCCC)
> Thanx
> channa

(defun clone (list n)
    (cond ((null list) list)
              ((<= n 0) ())
              (t (nconc (make-list n :initial-element (first list)) (clone (rest list) n)))))

Regards,
jag
From: Peter Seibel
Subject: Re: cloning elements in a list
Date: 
Message-ID: <m3vg276aux.fsf@localhost.localdomain>
>>>>> "echo" == echo  <echo109> writes:

    echo> Hello everyone,
    echo> Please somebody help me with the following question

    echo> How to write a lisp function which clones the top-level
    echo> elements of a list L (first arguement) n ( second argument)
    echo> times. for eg. (clone 'A B C) 4 ) produces ( AAAABBBBCCCC)

What have you got so far? (And I think you're going to want to invoke
that like: (clone '(A B C) 4). Note the extra opening paren.

-Peter

    echo> Thanx channa
From: Coby Beck
Subject: Re: cloning elements in a list
Date: 
Message-ID: <asp9al$21ff$1@otis.netspace.net.au>
"echo109" <·······@carolina.rr.com> wrote in message
·························@twister.southeast.rr.com...
> Hello everyone,
>
> Please somebody help me with the following question
>
> How to write a lisp function which clones the top-level elements of a list
L
> (first arguement) n ( second argument) times. for eg. (clone 'A B C) 4 )
> produces ( AAAABBBBCCCC)

To rewrite your question to what I *think* you mean:
(clone '(A B C) 4 )
=> (A A A A B B B B C C C C)

try this:
CL-USER 143 > (defun stretch (list factor)
               (loop for elt in list
                     nconc (make-list factor :initial-element elt)))
STRETCH

CL-USER 144 > (stretch '(a b c) 4)
(A A A A B B B B C C C C)

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Kent M Pitman
Subject: Re: cloning elements in a list
Date: 
Message-ID: <sfwbs3zt4e3.fsf@shell01.TheWorld.com>
"Coby Beck" <·····@mercury.bc.ca> writes:

> "echo109" <·······@carolina.rr.com> wrote in message
> ·························@twister.southeast.rr.com...
> > Hello everyone,
> >
> > Please somebody help me with the following question

If this is homework, it should be identified as such.

> > How to write a lisp function which clones the top-level elements of a list
> L
> > (first arguement) n ( second argument) times. for eg. (clone 'A B C) 4 )
> > produces ( AAAABBBBCCCC)
> 
> To rewrite your question to what I *think* you mean:
> (clone '(A B C) 4 )
> => (A A A A B B B B C C C C)

Right.  Except just as an aside, the original poster may wish to note that
this does not 'clone' the symbol, but rather creates multiple pointers to
the same symbol.

It is possible to make new pointers to a symbol just like an existing
one, which I assume is what cloning would seem to me to be, but it's surely
not what's intended by a homework assignment like this.
From: Joe Marshall
Subject: Re: cloning elements in a list
Date: 
Message-ID: <ptseor8p.fsf@ccs.neu.edu>
Kent M Pitman <······@world.std.com> writes:

> "Coby Beck" <·····@mercury.bc.ca> writes:
> 
> > "echo109" <·······@carolina.rr.com> wrote in message
> > ·························@twister.southeast.rr.com...
> > > Hello everyone,
> > >
> > > Please somebody help me with the following question
> 
> If this is homework, it should be identified as such.

It's gotta be homework.  Why would anyone want/need such a function?

I think people who come up with these problems (professors and
authors) are doing a disservice to their students by making them write
`nonsense code' such as `cloning the elements of a list', `removing
all the NILs from a tree structure', `replace all FOOs with BARs, but
don't use the built-in SUBST function'

My first exposure to Lisp was doing such idiotic busy work and my
first impression of Lisp was that it was ideal for idiotic busy work
and little else.  I don't think I'm the only one who thought this.

~jrm

p.s.  My second exposure was numeric differentiation:

(defvar *epsilon* .001)

(defun derivative (f)
  (lambda (x)
    (/ (- (funcall f (+ x *epsilon*))
          (funcall f (- x *epsilon*)))
       (* *epsilon* 2))))

This, at least, was something I could see the utility of.