Hi you'al,

Since I am in a "self-study" course in Scheme, (using the book "Scheme: The Art Of Programming") and using DrRacket as my interpreter, is it permissible to use this forum to double-check to see if my answers to the exercises are correct? Since the answers are not in the book, I have no way of knowing if I am making errors.

If it is permissible, I will be grateful for all your help...and, at the end of the book maybe I can put all my notes online as an aid to all future want-to-be-scheme-programers?

Exercise 1.6

Lists

Using the symbols one, two, three, four, and the procedure cons, construct the following lists without using quoted lists (You may use quoted symbols and the empty list).

a. (one two three four)

(cons 'one (cons 'two (cons 'three (cons 'four '()))))----> OK

b. (one (two three four))

If (cons 'one '())----> '(one), and if (cons 'two (cons 'three (cons 'four '())))----> '(two three four), why do the following give errors when I combine them in DrRacket?

> (cons 'one '() (cons 'two (cons 'three (cons 'four '()))))

. . cons: expects 2 arguments, given 3: 'one '() '(two three four)

or

> (cons 'one '() (cons (cons 'two (cons 'three (cons 'four '())))))

. . cons: expects 2 arguments, given 1: '(two three four)

I understand that the procedure cons is used to create lists, and that cons is made up of two parts---> car and cdr, and that each are but names for memory spaces that may or may not contain different types of data. In the expression (one (two three four)), I think that there are two lists nested... the first list has only two element in it---> 'one, and the empty list '().

The second list has four elements---> 'two, 'three, 'four, and '(). I do not wish to go further until I understand problem 'b'. I think what I am missing here will prevent me from understanding problems 'c' 'd' and 'e' as well. I seem to be missing a key point in this section. Please, what am I doing wrong?....therry

c. (one (two three) four)

d. (((one two) (three four))

e. (((one)))

# Understanding Lists in Scheme

Page 1 of 1## 5 Replies - 9198 Views - Last Post: 12 August 2011 - 12:50 AM

##
**Replies To:** Understanding Lists in Scheme

### #2

## Re: Understanding Lists in Scheme

Posted 10 August 2011 - 05:39 PM

In response to the question about b.

The REPL told you exactly what's wrong.

You are correct that a pair consists of a car and a cdr. However, you've given cons a car, a cdr, and a third argument it didn't want.

The structure for the cons statement you probably want is this:

The REPL told you exactly what's wrong.

Quote

cons: expects 2 arguments, given 3: 'one '() '(two three four)

You are correct that a pair consists of a car and a cdr. However, you've given cons a car, a cdr, and a third argument it didn't want.

The structure for the cons statement you probably want is this:

(cons (cons `one `()) (cons `two (cons `three (cons `four `())))) ; Or more concisely `((one . ()) . (two . (three . (four . ()))))

### #3

## Re: Understanding Lists in Scheme

Posted 10 August 2011 - 08:28 PM

Cons can actually be a somewhat tricky concept to grasp if you are trying to build lists out of it. Lucky for you, building lists out of cons will only truly be found in one spot, in a book. No one uses cons to build lists. However, with that being said, cons does come in handy when you want to make a pair of one thing or another. For example,

Do you see that this could be used as coordinates, a key/value or anything. In a way, cons is a data structure in it's own right. A data structure used for pairs.

This doesn't help you in answering your problem though. So, here's what you need to know: Cons only takes two arguments. (cons 1 2 3) is

Each call to the cons I have in the code above is only receiving two arguments. That's why it can be so confusing to make lists from cons of the nature (one (two three) four five). You have to somehow put it together so that cons will only get two arguments in each call. So my advice is, just know cons has a car/cdr and can be used for pairs. If you are going to make lists use quote notation or the 'list' function.

list is built out of cons and is a lot easier to understand and write.

Here's your question

Though, if you really must use cons, here's your question

I think that should all help you get the rest!

CG-USER(21): (cons 1 2) (1 . 2)

Do you see that this could be used as coordinates, a key/value or anything. In a way, cons is a data structure in it's own right. A data structure used for pairs.

This doesn't help you in answering your problem though. So, here's what you need to know: Cons only takes two arguments. (cons 1 2 3) is

**NOT**valid.CG-USER(22): (cons 1 (cons 2 (cons 3 nil))) (1 2 3)

Each call to the cons I have in the code above is only receiving two arguments. That's why it can be so confusing to make lists from cons of the nature (one (two three) four five). You have to somehow put it together so that cons will only get two arguments in each call. So my advice is, just know cons has a car/cdr and can be used for pairs. If you are going to make lists use quote notation or the 'list' function.

CG-USER(24): (list 'one (list 'two 'three) 'four) (ONE (TWO THREE) FOUR)

list is built out of cons and is a lot easier to understand and write.

Here's your question

**e**using list:CG-USER(25): (list (list (list 'one))) (((ONE)))

Though, if you really must use cons, here's your question

**e**using cons:CG-USER(35): (cons (cons (cons 'one nil) nil) nil) (((ONE)))

I think that should all help you get the rest!

This post has been edited by **I X Code X 1**: 11 August 2011 - 06:54 AM

### #4

## Re: Understanding Lists in Scheme

Posted 11 August 2011 - 08:03 AM

Thanks IX code XI for your reply.

I noticed that you used the symbol nil to represent the empty list '(). If I am coding, can I use nil instead of '() in my code?

1 CG-USER(22): (cons 1 (cons 2 (cons 3 nil)))

2 (1 2 3),

Am I correct to understand that cons can be used for an X/Y coordinates... Like, if I am coding instructions (for drawing a line that starts at point X and ends at point Y), I can use cons to represent points X/Y by defining the list that contains points X/Y? That's going to make my job much easier....therry

I noticed that you used the symbol nil to represent the empty list '(). If I am coding, can I use nil instead of '() in my code?

1 CG-USER(22): (cons 1 (cons 2 (cons 3 nil)))

2 (1 2 3),

Am I correct to understand that cons can be used for an X/Y coordinates... Like, if I am coding instructions (for drawing a line that starts at point X and ends at point Y), I can use cons to represent points X/Y by defining the list that contains points X/Y? That's going to make my job much easier....therry

### #5

## Re: Understanding Lists in Scheme

Posted 11 August 2011 - 09:18 AM

Thanks GWatt for your reply

I see, in coding cons, every time there is a '(' it requires a '(cons' that has two arguments .... a car and a cdr....and, any car or cdr can contain another list, such as a (cons x '()),

or a (cons 1 (cons 2 (cons n '()))). In reverse, every time there is more than one list in a statement, such as in problem b, each list becomes either a car or a cdr for a (cons....so, in the expression ((1)), does that require a

or a

or is the extra '() with the closing )) implied?

Do I understand correctly that your first '(cons' of line #1, in the coding below, represents the first '(' of line 3, and that the rest of line #1 are the car and the cdr of the first (cons? That line #3 is a pseudo graphic representation of the memory spaces in line #1?

1 (cons (cons `one `()) (cons `two (cons `three (cons `four `()))))

2 ; Or more concisely

3 `((one . ()) . (two . (three . (four . ()))))

Is this correct?....therry

I see, in coding cons, every time there is a '(' it requires a '(cons' that has two arguments .... a car and a cdr....and, any car or cdr can contain another list, such as a (cons x '()),

or a (cons 1 (cons 2 (cons n '()))). In reverse, every time there is more than one list in a statement, such as in problem b, each list becomes either a car or a cdr for a (cons....so, in the expression ((1)), does that require a

(cons (cons 1 '()))

or a

(cons (cons 1 '()) '())

or is the extra '() with the closing )) implied?

Do I understand correctly that your first '(cons' of line #1, in the coding below, represents the first '(' of line 3, and that the rest of line #1 are the car and the cdr of the first (cons? That line #3 is a pseudo graphic representation of the memory spaces in line #1?

1 (cons (cons `one `()) (cons `two (cons `three (cons `four `()))))

2 ; Or more concisely

3 `((one . ()) . (two . (three . (four . ()))))

Is this correct?....therry

### #6

## Re: Understanding Lists in Scheme

Posted 12 August 2011 - 12:50 AM

In Common Lisp (and about 98% sure this applies to Scheme too)

nil == 'nil == () == '()

In other words, they all mean the same thing.

The cons can be used for coordinates if you wanted to, but I was just saying it can be thought of as a mini data structure that hold pairs.

nil == 'nil == () == '()

In other words, they all mean the same thing.

The cons can be used for coordinates if you wanted to, but I was just saying it can be thought of as a mini data structure that hold pairs.

Page 1 of 1