I trying to write a function which gets an integer number , represented by string , and check if all his chars are digits and return #t \ #f accordingly . Thats the code –
(define (splitString str) (list->vector (string->list str)))
(define myVector 0)
(define flag #t)
(define (checkIfStringLegal str) (
(set! myVector (splitString str))
(do ( (i 0 (+ i 1)) ) ; init
((= i (vector-length myVector)) flag) ; stop condition
(cond ((>= 48 (char->integer (vector-ref myVector i)) ) (set! flag #f))
((<= 57 (char->integer (vector-ref myVector i)) )(set! flag #f))
)
)
)
)
Few explanations –
(list->vector (string->list str)) – convert string the char list .
(vector-ref myVector i) – char from the myVector at place i .
Its run OK , but when I try to use this func , like (checkIfStringLegal "444") I get –
application: not a procedure;
expected a procedure that can be applied to arguments
given: #<void>
arguments...:
#t
Try this:
This is how the procedure works:
string->listchar-numeric?to each one#t,andmapwill return#t. If a single validation failed,andmapwill return#fimmediatelyThat’s a functional-programming solution (and after all, this question is tagged as such), notice that your intended approach looks more like a solution in a C-like programming language – using vectors, explicit looping constructs (
do), mutation operations (set!), global mutable definitions … that’s fine and it might eventually work after some tweaking, but it’s not the idiomatic way to do things in Scheme, and it’s not even remotely a functional-programming solution.EDIT:
Oh heck, I give up. If you want to write the solution your way, this will work – you had a parenthesis problem, and please take good notice of the proper way to indent and close parenthesis in Scheme, it will make your code more readable for you and for others:
Even so, the code could be further improved, I’ll leave that as an exercise for the reader:
or