[plt-scheme] Beginner Question

From: Carl Eastlund (carl.eastlund at gmail.com)
Date: Sun Oct 4 16:26:58 EDT 2009

On Sun, Oct 4, 2009 at 1:34 AM, vishy <vishalsodani at gmail.com> wrote:
> Why (cons (list 1 2) (list 5 3)), produces
> ((1,2) 5 3) and not ((1,2),(5, 3)) thats is a list of 2 elements .
>
> thanks

Vishy,

The cons procedure adds an element to a list.  If you run (cons 1
(list 2 3)), you add the element 1 to (list 2 3) and get (list 1 2 3).
 If you run (cons (list 1 2) (list 5 3)), you add (list 1 2) as an
element, and get (list (list 1 2) 5 3)).  If your intent is to join
the elements of two lists, the appropriate function is append.
Running (append (list 1 2) (list 5 3)) will give you (list 1 2 5 3).
Both methods of combination, cons and append, have their uses, so be
careful to always choose the one you mean.

--Carl


Posted on the users mailing list.