I’m trying to write a function that checks if the given input holds any characters other than digits . However , I get contract violation in DrRacket under Scheme .
Here’s the code :
A method that separating a string into characters :
(define (breaking str) (list->vector (string->list str)))
Creating a vector for the number :
(define myNumber (breaking "123498765"))
Here I check the given number :
(define (vectorFunc myVector)
(define i 0)
(do ()
((= i (vector-length myVector))) ; run until the end of the vector
(cond ((< (vector-ref myVector i) #\0) 'incorrect)
((> (vector-ref myVector i) #\9) 'also-incorrect))
(set! i (+ i 1)) ; inc "i+ by 1
); end of do
)
And the output of (vectorFunc myNumber) is :
. . >: contract violation
expected: real?
given: #\1
argument position: 1st
other arguments...:
#\0
>
What’s wrong with it ?
If you need to determine if a value is a character between
#\0and#\9, better use thechar-numeric?predicate:From the documentation:
In the case of your code, it looks like this:
I agree with @LudwigMeier’s comment – your code doesn’t look Scheme-like. You’re trying to use Scheme as if it were a C-like language, and it looks a bit weird. In Scheme things are done … different.