Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help

2 views
Skip to first unread message

temp

unread,
Mar 16, 1998, 3:00:00 AM3/16/98
to

Hi I am trying to write an iterative lisp function that given a list A
returns a list with everything in A repeated twice consecutively. For
example A = [a,b,c] then the newlist = [a,a,b,b,c,c]. Any help would be
greatly appreciated. Thanks.

William Paul Vrotney

unread,
Mar 17, 1998, 3:00:00 AM3/17/98
to

Ok, you sucked me in. I'll do you a favor if you will do me one. I am
dying to find out if your source (you know what I mean) will accept

(defun double-em (list) (mapcan #'list list list))

so try with

(double-em '(a b c d)) => (A A B B C C D D)

Please report back.

--

William P. Vrotney - vro...@netcom.com

Eric S. Ingram

unread,
Mar 18, 1998, 3:00:00 AM3/18/98
to

As a novice myself, here is what I came up with...

(defun double-element (x)
(list x x))

(defun double-list (x)
(cond ((null x) nil)
(t (append (double-element (first x)) (double-list (rest x))))))

It seems to do the job when tested with CLISP and FreeLisp.

Adam Przybyla

unread,
Mar 19, 1998, 3:00:00 AM3/19/98
to

... try this:
(defun double-list (x)
(merge x x))
Adam Przybyla


Eric S. Ingram

unread,
Mar 19, 1998, 3:00:00 AM3/19/98
to

Adam Przybyla wrote:
> ... try this:
> (defun double-list (x)
> (merge x x))
> Adam Przybyla

I tried that using FreeLisp and I got...

(double-list '(a b c))

Error: Call (#<Function MERGE 00112242> (A B C) (A B C)) has the wrong
number of arguments.

Barry Margolin

unread,
Mar 19, 1998, 3:00:00 AM3/19/98
to

In article <35114759...@cableinet.co.uk>,

Eric S. Ingram <e.in...@cableinet.co.uk> wrote:

He forgot the result-type a predicate parameters. See CLtL or the
HyperSpec for the correct syntax of MERGE (you don't want us doing *all*
your homework, do you?).

--
Barry Margolin, bar...@bbnplanet.com
GTE Internetworking, Powered by BBN, Cambridge, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.

David D. Smith

unread,
Mar 20, 1998, 3:00:00 AM3/20/98
to

In article <1kcQ.3$fl.3...@cam-news-reader1.bbnplanet.com>, Barry
Margolin <bar...@bbnplanet.com> wrote:

> In article <35114759...@cableinet.co.uk>,
> Eric S. Ingram <e.in...@cableinet.co.uk> wrote:
> >Adam Przybyla wrote:
> >> ... try this:
> >> (defun double-list (x)
> >> (merge x x))
> >> Adam Przybyla
> >
> >I tried that using FreeLisp and I got...
> >
> >(double-list '(a b c))
> >
> >Error: Call (#<Function MERGE 00112242> (A B C) (A B C)) has the wrong
> >number of arguments.
>
> He forgot the result-type a predicate parameters. See CLtL or the
> HyperSpec for the correct syntax of MERGE (you don't want us doing *all*
> your homework, do you?).

He also forgot that MERGE is DESTRUCTIVE and MERGEing shared structure is
clearly bogus, and may never terminate.

d

? (let ((x '(1 2 3)))
(merge 'list x x #'<))
> Break:
> While executing: #<Anonymous Function #x3E824A6>
> Type Command-/ to continue, Command-. to abort.
> If continued: Return from BREAK.
See the RestartsŠ menu item for further choices.
1 > (local 8)
#1=(1 . #1#)
1 > >

Erik Naggum

unread,
Mar 20, 1998, 3:00:00 AM3/20/98
to

* temp <lal...@ibm.net>

| Hi I am trying to write an iterative lisp function that given a list A
| returns a list with everything in A repeated twice consecutively. For
| example A = [a,b,c] then the newlist = [a,a,b,b,c,c]. Any help would be
| greatly appreciated. Thanks.

first, you have a pretty weird syntax for lists, but I guess you still
mean lists like (A B C) and (A A B B C C) not something else entirely.

however, back to your homework assignment: as you may have found out,
there are many ways to do what you want to do, but I have not yet seen
the simple _iterative_ solutions:

(loop for e in <list> collect e collect e)

I'm sure you are not allowed to use LOOP, but rewriting this to use DO or
DOLIST shouldn't be too hard.

#:Erik
--
religious cult update in light of new scientific discoveries:
"when we cannot go to the comet, the comet must come to us."

Johannes Beck

unread,
Mar 25, 1998, 3:00:00 AM3/25/98
to

Erik Naggum wrote:
> * temp <lal...@ibm.net>
> | Hi I am trying to write an iterative lisp function that given a list A
> | returns a list with everything in A repeated twice consecutively. For
> | example A = [a,b,c] then the newlist = [a,a,b,b,c,c]. Any help would be
> | greatly appreciated. Thanks.
>
> first, you have a pretty weird syntax for lists, but I guess you still
> mean lists like (A B C) and (A A B B C C) not something else entirely.
>
> however, back to your homework assignment: as you may have found out,
> there are many ways to do what you want to do, but I have not yet seen
> the simple _iterative_ solutions:
>
> (loop for e in <list> collect e collect e)
>
> I'm sure you are not allowed to use LOOP, but rewriting this to use DO or
> DOLIST shouldn't be too hard.

What about a "comp.lang.lisp.homework" - newsgroup?

Bye
Joe

Adam Przybyla

unread,
Mar 25, 1998, 3:00:00 AM3/25/98
to

(merge 'list (copy-list <list>) (copy-list <list>) #'eql)
Adam Przybyla

0 new messages