schemer5rs

Scheme::R5RS. Trying to make the absolute value procedure


I'm trying to make the absolute value procedure and code got an error. I don't know why :/

(define (abs x)
   (cond ((> x 0) x)
       ((= x 0) 0)
       ((< x 0) (- x))))

Error message:

define-values: assignment disallowed;
 cannot change constant
  constant: abs

Solution

  • The abs procedure is already part of the language you're using and you can't create another procedure with the same name. Simply rename it (and the implementation can be simplified a bit, by the way):

    (define (myabs x)
      (cond ((>= x 0) x)
            (else (- x))))