I am doing something wrong in Scheme (7)

1 Name: #!/usr/bin/anonymous : 2007-03-22 16:28 ID:19uf5Tmm

(define (dumplist list)
(car list)
(cond (null? (cdr list))
(else
(dumplist (cdr list)))))

This is supposed to print out the values of the list, but instead it just prints (cdr list), or the list minus its first character. What am I doing wrong?

2 Name: #!/usr/bin/anonymous : 2007-03-23 01:48 ID:Heaven

Why would this print anything? It doesn't call the "print" procedure.

3 Name: #!/usr/bin/anonymous : 2007-03-23 15:55 ID:Heaven

I don't know. The MIT programming OCW doesn't actually introduce a lot of this stuff.

4 Name: #!/usr/bin/anonymous : 2007-03-26 16:18 ID:Heaven

Wait... As far as the interpreter is concerned, there is no 'print' procedure...

And that still doesn't explain why the output is (cdr list)

I don't see anything that would print it, unless I'm making some mistake with the cond statement.

5 Name: Cardcaptor : 2007-04-01 11:44 ID:4AWqNstI

The cond expression expects a list of test-value pairs. Like this:

(cond (<test1> <value1>) (<test2> <value2>) ... [(else <else-value>)])

So, the (null? (cdr list)) is interpreted as:
<test1> = null?
<value1> = (cdr list)

Since "null?" is a procedure, it is not equal to #f, so the Scheme interpreter interprets it as "true." That's why you get (cdr list) as the value of the expression.

Your function might be written like this:

(define (dumplist list)
(if (not (null? list))

  (begin
(display (car list))
(dumplist (cdr list)))))

6 Name: #!/usr/bin/anonymous : 2007-06-13 11:17 ID:sQeQ7nwq

Can Euler's equation be expressed by using Emacs calc? The writing
of "Iπ multiplication of e" is not understood though calc
corresponds to the complex number.

7 Name: #!/usr/bin/anonymous : 2007-06-20 17:54 ID:lP+4GCSI

the error is not just the cond.
evaluating an expression "prints" the value of the expression in the top loop. That's why evaluating

>(+ 3 4)

prints 7. But expression themselves aren't printed.

>(+ (+ 3 4) 1)

8

here, (+ 3 4) still evaluated to 7. But the top loop only prints the entire expression, not the sub expressions.

expressions are about values, and maybe side effects. So, what happens when I have an expression like (begin (+ 1 2) (+ 2 3)), which has two expressions? what happens to the value of (+ 1 2)? it is discarded. That's why expressions without side effects whose values aren't going to be returned... do nothing.

Look at your code.

(define (dumplist list)

 (car list)
(cond (null? (cdr list))
(else
(dumplist (cdr list)))))

you have two expressions, just like in the (begin ...) before. The first expression is (car list). I see that you thought the top loop would have printed the expression (car list), but the value is discarded instead. a function expressed like

(define (fun ...)

expression1
expression2
expression3...)

will only return the value of the last expression, so when you call it on the top loop, like

>(fun ...)

that's the value is going to be printed. That's why you have to call a function with side effects (ie. does something more than just returning a value) like (display).

This thread has been closed. You cannot post in this thread any longer.