(define l '(* - + 4))
(define (operator? x)
(or (equal? '+ x) (equal? '- x) (equal? '* x) (equal? '/ x)))
(define (tokes list)
(if (null? list)(write "empty")
(if (operator? (car list))
((write "operator")
(tokes (cdr list)))
(write "other"))))
The code works just fine til (tokes (cdr list))) reaches the end of file. Can someone give me a tip in how I can prevent that. I’m new at Scheme so I’m forgive me if the question is absurd.
You must make sure of advancing the recursion on each case (except the base case, when the list is
null). In your code you’re not making a recursive call for the(write "other")case. Also, you should usecondwhen there are several conditions to test, Let me explain with an example – instead of this:Better write this, is much more readable and has the added benefit that you can write more than one expression after each condition without the need to use a
beginform:… Which leads me to the next point, be aware that you can write only one expression for each branch of an
if, if more than one expression is needed for a given condition in anifform then you must surround them with abegin, for example:Going back to your question – here’s the general idea, fill-in the blanks: