Reputation: 341
I'm new to scheme and I'm trying to do a small very straight forward program. But I keep getting this error message "Syntactic keyword may not be used as an expression: if".
Can anyone tell me if I'm missing something in my program or if I'm doing some sort of mistake?
Here is my program.
(define (foo lis k)
(COND
((NULL? lis) '())
(IF (< (CAR lis) k)
(display (CAR lis))
ELSE (display (CDR lis)))
))
(foo '(1 5 3 2 4) 3)
Thanks
-Gunnlaugur
Upvotes: 1
Views: 3532
Reputation: 45657
cond
takes expressions in the form*
(condition expr1 expr2 ... result)
Since you gave
(if (< (car lis) k) (display (car lis)) else (display (cdr lis)))
Scheme will try to see if if
is true, and then run the next four expressions (< (car lis) k)
, (display (car lis))
, else
, and (display (cdr lis))
. It runs into a couple problems, though. if
cannot be evaluated as true or false, since it is syntax. Even if it could be, you would get a misplaced aux keyword else
error. And if that didn't happen, it would do both (display (car lis))
and (display (cdr lis))
(which is not what you want.
I'm guessing that you don't actually want to use display
. If you're running this interactively in a terminal, Scheme will evaluate the result of your function and print it for you.
*
a slight simplification. Look in TSPL4 for details.
Upvotes: 0
Reputation: 20591
I'm not sure what you are trying to do, but it seems like if is not needed there:
(define (foo lis k)
(cond
((null? lis) '())
((< (car lis) k)
(display (car lis)))
(else (display (cdr lis)))))
(foo '(1 5 3 2 4) 3)
Upvotes: 2