True / false evaluation doesn't work as expected in Scheme
- by ron
I'm trying to compare two booleans :
(if (equal? #f (string->number "123b"))
"not a number"
"indeed a number")
When I run this in the command line of DrRacket I get "not a number" , however , when I
put that piece of code in my larger code , the function doesn't return that string ("not a number") , here's the code :
(define (testing x y z)
(define badInput "ERROR")
(if (equal? #f (string->number "123b"))
"not a number"
"indeed a number")
(display x))
And from command line : (testing "123" 1 2)
displays : 123
Why ?
Furthermore , how can I return a value , whenever I choose ?
Here is my "real" problem :
I want to do some input check to the input of the user , but the thing is , that I want to
return the error message if I need , before the code is executed , because if won't - then I would run the algorithm of my code for some incorrect input :
(define (convert originalNumber s_oldBase s_newBase)
(define badInput "ERROR")
; Input check - if one of the inputs is not a number then return ERROR
(if (equal? #f (string->number originalNumber))
badInput)
(if (equal? #f (string->number s_oldBase))
badInput)
(if (equal? #f (string->number s_newBase))
badInput)
(define oldBase (string->number s_oldBase))
(define newBase (string->number s_newBase))
(define outDecimal (convertIntoDecimal originalNumber oldBase))
(define result "") ; holds the new number
(define remainder 0) ; remainder for each iteration
(define whole 0) ; the whole number after dividing
(define temp 0)
(do()
((= outDecimal 0)) ; stop when the decimal value reaches 0
(set! whole (quotient outDecimal newBase)) ; calc the whole number
(set! temp (* whole newBase))
(set! remainder (- outDecimal temp)) ; calc the remainder
(set! result (appending result remainder)) ; append the result
(set! outDecimal (+ whole 0)) ; set outDecimal = whole
) ; end of do
(if (> 1 0)
(string->number (list->string(reverse (string->list result)))))
) ;end of method
This code won't work since it uses another method that I didn't attach to the post (but it's irrelevant to the problem . Please take a look at those three IF-s ... I want to return "ERROR" if the user put some incorrect value , for example (convert "23asb4" "b5" "9")
Thanks