From: Weiguang Shi
Subject: newbie question -- append
Date: 
Message-ID: <slrn97utml.95j.wgshi@namao.cs.ualberta.ca>
Hi there,

I am learning CLISP on Linux.
When I want to append to a list, I did the following:

      [13]> (append '(b c d) '(a))
      (B C D A)

      [14]> (append '(b c d) 'a))
      (B C D . A)

Why is the difference in the results?

Thanks very much.
Weiguang

From: ········@hex.net
Subject: Re: newbie question -- append
Date: 
Message-ID: <0PLf6.321518$IP1.10599466@news1.giganews.com>
·····@namao.cs.ualberta.ca (Weiguang Shi) writes:
> I am learning CLISP on Linux.
> When I want to append to a list, I did the following:
> 
>       [13]> (append '(b c d) '(a))
>       (B C D A)
> 
>       [14]> (append '(b c d) 'a))
>       (B C D . A)
> 
> Why is the difference in the results?

Note that the result of this is the same with CMU/CL, as well as if
you pass the expression to a Scheme implementation.

Obviously if you try to append together things that are not all lists,
you may get a result that you didn't expect.

What happens here is that APPEND copies the members of all the lists
_save for the last argument_, to create a new list.  As for the last
argument, it becomes the CDR of the final dotted pair, which means
that if it's an atom, then you wind up with an improper list, or, more
particularly, a dotted list, that is, a list whose terminating atom is
not NIL.
-- 
(concatenate 'string "cbbrowne" ·@ntlug.org")
http://vip.hex.net/~cbbrowne/lisp.html
Q: How many Newtons does it take to change a light bulb?
A: Faux!  There to eat lemons, axe gravy soup!
From: Janis Dzerins
Subject: Re: newbie question -- append
Date: 
Message-ID: <87y9vktd0r.fsf@asaka.latnet.lv>
·····@namao.cs.ualberta.ca (Weiguang Shi) writes:

> Hi there,
>
> I am learning CLISP on Linux.
> When I want to append to a list, I did the following:
>
>       [13]> (append '(b c d) '(a))
>       (B C D A)
>
>       [14]> (append '(b c d) 'a))
>       (B C D . A)
>
> Why is the difference in the results?

I'd suggest drawing the box representations of the lists then try to
understand what the append function does (use the HyperSpec).

'(b c d) :

  +---+---+   +---+---+   +---+---+
  | b | *---->| c | *---->| d | *----> nil
  +---+---+   +---+---+   +---+---+

'(a) :

  +---+---+
  | a | *----> nil
  +---+---+

'a :

  a

In (append '(b c d) '(a)) case you get:

  +---+---+   +---+---+   +---+---+   +---+---+
  | b | *---->| c | *---->| d | *---->| a | *----> nil
  +---+---+   +---+---+   +---+---+   +---+---+

which is a proper list.

In (append '(b c d) 'a) case you get:

  +---+---+   +---+---+   +---+---+
  | b | *---->| c | *---->| d | a |
  +---+---+   +---+---+   +---+---+
 
which is not a proper list.

Note also that notation for

  +---+---+
  | a | b |
  +---+---+

is (a . b).

You should have read about this in any introductory material about
Lisp.

Janis Dzerins
--
  If million people say a stupid thing it's still a stupid thing.