I am trying to write a simple program in Racket that prints 1 if the value of a
is > 1
, prints 0
if the value of a = 0
and -1
if a < 0
. I wrote the following but looks like it is not taking care of the third condition. Actually, I have not included the third condition so I don't know how to check for all three conditions using 'if' clause. A little guidance is appreciated.
I am new to Racket. My program is:
#lang racket
(define a 3);
(if (> a 0)
0
1)
-1
Thanks in advance.
The function you are looking for is already defined under the name sgn.
The reason your implementation doesn't work is that it is incomplete. You want:
(if (= a 0)
0
(if (< a 0)
-1
1))
Or just the better looking:
(cond
[(negative? n) -1]
[(positive? n) 1]
[else 0])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With