Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

HELP! Scheme!

Name: Anonymous 2010-03-21 7:39

So, I recently read about the collatz conjecture on xkcd.
Of course I had read my SICP that day (like every day), so I was
certain, I could easily implement it in Scheme. Like this:

(define (collatz n)
  ((display n)
   (newline)
   (if (> n 1)
       (if (odd? n) (collatz (+ (* 3 n) 1))
           (collatz (/ n 2)))
       1)))


When i call (collatz 10), for instance, PLT Scheme gives
me all the numbers, until it throws an error when it reaches 1:
procedure application: expected procedure, given: #<void>; arguments were: #<void> 1

How do I do it right?

Name: Anonymous 2010-03-21 7:48

>>1
My Scheme is a bit rusty, butI think you need to wrap your  main body forms in a begin, like this:
(begin (display n) ...), or even simpler, if you were using paredit, go over the first paren and press M-s to split it:

(define (collatz n)
  (display n) (newline)
  (if (> n 1)
      (if (odd? n) (collatz (+ (* 3 n) 1))
          (collatz (/ n 2)))
      1))

I tested it and it works just fine.

Name: Anonymous 2010-03-21 7:50

>>1
You haven't read your SICP everyday.
display always evaluates to undefined and then you're applying it as a function.

Name: Anonymous 2010-03-21 7:52

>>3
...to yet another undefined and then the last argument won't evaluate anyway because you're throwing up issues with application to an undefined value everywhere.

Name: Anonymous 2010-03-21 7:56

>>4
I compared my solution with >>2 and it looks like I put the body of collatz in parentheses, and that was all.
Just a typo. Now it works.

Name: Anonymous 2010-03-21 7:57

...do you even know what parentheses denote in SICP Scheme outside from special forms?

Name: Anonymous 2010-03-21 11:20

>>6
Ocular trauma.

Name: Anonymous 2010-03-21 23:23

>>7
Colour them gray, then

Name: Anonymous 2010-12-17 1:18

Are you GAY?
Are you a NIGGER?
Are you a GAY NIGGER?

If you answered "Yes" to all of the above questions, then GNAA (GAY NIGGER ASSOCIATION OF AMERICA) might be exactly what you've been looking for!

Name: Anonymous 2010-12-17 1:38

This post brought to you by the Gay Nigger Association of America

Name: Anonymous 2011-01-31 19:50

<-- check em dubz

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List