Reputation: 33
Does Scheme have a sign function? I could not find any information about that.
I define a sign function as a function which produces -1 when x<0, 0 when x=0 and 1 when x>0.
Upvotes: 3
Views: 1550
Reputation: 236034
Just use the built-in sgn
procedure:
(sgn -10)
=> -1
(sgn 10)
=> 1
(sgn 0)
=> 0
In case you're wondering how to implement it...
(define (sign n)
(cond ((negative? n) -1)
((positive? n) 1)
(else 0)))
Upvotes: 12