cons

You have problems, and we're glad to hear them. Explain the problem, what you have tried, and where you got stuck.
Feel free to share a little info on yourself and the course.
Forum rules
Please respect your teacher's guidelines. Homework is a learning tool. If we just post answers, we aren't actually helping. When you post questions, be sure to show what you have tried or what you don't understand.
Post Reply
Ehsan
Posts: 8
Joined: Thu Jul 12, 2012 7:39 am

cons

Post by Ehsan » Sun Jul 22, 2012 10:53 am

Hi every . i have question about cons in lisp . how can i make this list
(1 (4 9 (12) ))

i used this rule (cons 1 (cons (cons 4 (cons 9 (cons (cons 12()))) ()) ())) but interpreter return error :(

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: cons

Post by nuntius » Sun Jul 22, 2012 6:07 pm

I think the main issue is that the empty list must be written as '(). If you write (), then it will try to evaluate the function with no name.

In Common Lisp, '() can also be written as nil.

Ehsan
Posts: 8
Joined: Thu Jul 12, 2012 7:39 am

Re: cons

Post by Ehsan » Mon Jul 23, 2012 2:26 am

tnx . i found how to build a nested list

saulgoode
Posts: 45
Joined: Tue Dec 14, 2010 1:39 am

Re: cons

Post by saulgoode » Mon Jul 23, 2012 12:40 pm

Even if unquoted empty lists were recognized, the following part of your expression is not a pair:

Code: Select all

(cons (cons 12 ()) )

Kompottkin
Posts: 94
Joined: Mon Jul 21, 2008 7:26 am
Location: München, Germany
Contact:

Re: cons

Post by Kompottkin » Mon Jul 23, 2012 1:29 pm

Actually, in Common Lisp, () is equivalent to (reads as) NIL, which is self-evaluating, so it's a completely legitimate way of writing the empty list. (Which does not mean it isn't stylistically questionable.)

sylwester
Posts: 133
Joined: Mon Jul 11, 2011 2:53 pm

Re: cons

Post by sylwester » Tue Jul 24, 2012 5:00 pm

Ehsan wrote:Hi every . i have question about cons in lisp . how can i make this list
(1 (4 9 (12) ))

i used this rule (cons 1 (cons (cons 4 (cons 9 (cons (cons 12()))) ()) ())) but interpreter return error :(
Maybe it's easier to compose it with list first eg.

Code: Select all

(list 1 (list 4 9 (list 1 2)))
Then (list a b c) is an abbrivation for (cons a (cons b (cons c NIL)))
So translating the first list would get you:

Code: Select all

(cons 1(cons (list 4 9 (list 1 2)) NIL))
Then do the same with the rest. Note that I'm using NIL here to keep away the the extra parenthesis. If you're using Scheme you need to use '()
It's quite difficult to get it right so before list and quasiquoting it must have been a source of many errors :)

nuntius wrote:I think the main issue is that the empty list must be written as '(). If you write (), then it will try to evaluate the function with no name.
This is true for Scheme. The empty list needs to be quoted since it cannot be evaluated.
nuntius wrote: In Common Lisp, '() can also be written as nil.
For Common Lisp quoting is optional. (), NIL, '() and 'NIL evaluates to NIL.
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p

Ehsan
Posts: 8
Joined: Thu Jul 12, 2012 7:39 am

Re: cons

Post by Ehsan » Wed Jul 25, 2012 11:46 am

tnx everyone . I created this instruction , but this is very confusing :D

(cons 1 (cons (cons 2 (cons 6 (cons 7 (cons 8 ()))))(cons 3 ( cons (cons 4 (cons 9 (cons (cons 12())())))(cons(cons 5(cons 10 (cons 11()))) ())))))


(1 (2 6 7 8) 3 (4 9 (12)) (5 10 11))

Post Reply