1; SketchyLISP Library
2; Copyright (C) 2005,2006,2007 Nils M Holm. All rights reserved.
3; See the file LICENSE of the SketchyLISP distribution
4; for conditions of use.
5
6; ---name---
7; negate
8
9; ---conformance---
10; SketchyLISP Core
11
12; ---purpose---
13; Compute the negative value of a number.
14
15; ---args---
16; X - number
17
18; ---keywords---
19; NEGATE function, negative value, arithmetics
20
21; ---see-also---
22; digits, -, negative?
23
24; ---example---
25; (negate 125) => -125
26
27(define negate #t)
28
29(require "digits.scm")
30(require "zerop.scm") ; zero?
31
32; ---code---
33(define (negate x)
34  (letrec
35    ((_negate
36       (lambda (x)
37         (cond ((eq? (car x) '-) (cdr x))
38           ((eq? (car x) '+) (cons '- (cdr x)))
39           (else (cons '- x))))))
40    (cond ((zero? x) x)
41      (else (list->integer
42              (_negate (integer->list x)) #t)))))
43