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; + 8 9; ---conformance--- 10; R5RS 11 12; ---purpose--- 13; Add numbers. 14 15; ---args--- 16; A... - numbers 17 18; ---keywords--- 19; + function, plus, sum, addition, arithmetics 20 21; ---see-also--- 22; digits, n+, -, quotient, remainder, * 23 24; ---example--- 25; (+ 5 7 9) => 21 26 27(define plus #t) 28 29(require "digits.scm") 30(require "abs.scm") 31(require "ngreater.scm") ; n> 32(require "minus.scm") ; - 33(require "integer.scm") 34(require "fold-left.scm") 35(require "negativep.scm") ; negative? 36(require "non-negativep.scm") ; non-negative? 37(require "negate.scm") 38 39; ---code--- 40(define (+ . a) 41 (letrec 42 ((_iplus 43 (lambda (a b) 44 (cond ((and (non-negative? a) 45 (non-negative? b)) 46 (n+ (abs a) (abs b))) 47 ((and (non-negative? a) 48 (negative? b)) 49 (cond ((n> (abs a) (abs b)) 50 (- a (abs b))) 51 (else (negate (- (abs b) a))))) 52 ((and (negative? a) 53 (non-negative? b)) 54 (cond ((n> (abs a) (abs b)) 55 (negate (- (abs a) b))) 56 (else (- b (abs a))))) 57 ; both negative 58 (else (negate (n+ (abs a) (abs b))))))) 59 (i+ 60 (lambda (a b) 61 (_iplus (integer a) (integer b))))) 62 (fold-left i+ 0 a))) 63