user1137775
user1137775

Reputation: 33

Sign function in Scheme?

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

Answers (1)

&#211;scar L&#243;pez
&#211;scar L&#243;pez

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

Related Questions