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.

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