Jump to content

Dr. scheme, (yet another person in need of help)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Soupdude

Soupdude

    Newbie

  • Members
  • Pip
  • 4 posts
so, i'm getting into scheme since its somehow going to speed the process of achieving AI

and having talked about binary code in physics, i wanted to see if i could create a program in scheme that converted from decimal to binary, (don't care about the other way around yet.)

now i know there's probably a pre-made definition for this in Dr. scheme somewhere, but i want to make it myself for the sake of self accomplishment (which is why i'm asking for help o.o)

here is what my code looks like so far:

 
(define (dec->bin x)
  (cond[(equal? 1 x) (print 1)]
       [(equal? 0 x) (print 0)]
       (else (print (remainder x 2)))       ;<-- either problem is here
       )
  (cond[(equal? (- x (remainder x 2)) 0) (printf " ")]
       [(> (- x (remainder x 2)) 0) (print (dec->bin (/ (- x (remainder x 2)) 2)))] ; <-- or here
       ))

and my problem: it keeps returning #<void> in one of the parts where there is a "print(" followed by " the value x" (shown in code)

edit: i am aware that the code is displayed backwards, but il fix that later, for now i want to get rid of those #<void> 's :P

i don't know what i'm doing wrong :S, plz help, thanks in advance

Edited by Soupdude, 09 November 2009 - 04:00 PM.
fixing it


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
My guess is it's the second, since dec->bin doesn't appear to return anything.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Soupdude

Soupdude

    Newbie

  • Members
  • Pip
  • 4 posts
thanks for the suggestion, i agree that it is somewhere in the second part, but i was wondering, is there a procedure or a certain code that causes the whole code to be looped a specific amount of times? :P

im assuming thats wat i need, since it seems to be looping an invalid value repeatedly,

il just try redoing the whole thing in a different way in the meantime xP

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You will eventually get down to 1 or 0, which should end the recursion.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog