user1692517
user1692517

Reputation: 1152

Racket Scheme - if else statements

Currently using racket, can't find much help, was wondering if someone can help me..

I have

(define (reciprocal x) (/ 1 x))

however, having a 0 as an ex shouldn't work. So I tried modifying it to

(define (reciprocal x) (if (= x 0)((#f)(/ 1 x)))

I'm not sure what I'm doing wrong, I was hoping that it would return false if x = 0 but it does not do this. I can still get the reciprocal, just doesn't check for the x. Can someone point out what I'm doing wrong here? Thanks!

Upvotes: 1

Views: 1037

Answers (1)

crunch
crunch

Reputation: 156

You need to restructure the if-else clause. Typical Scheme syntax for this form is as follows:

(if (predicate expression) then else)

So you would rewrite your code as follows:

(define (reciprocal x) (if (= x 0) #f (/ 1 x)))

Upvotes: 3

Related Questions