t3x.org / sketchy / library / sum.html
SketchyLISP
Reference
  Copyright (C) 2007
Nils M Holm

sum

Conformance: SketchyLISP Extension

Purpose: Compute the sum of a sequence of positive numbers.

Arguments:
N - least value of sequence
M - maximum value of seqence

Model:

(define (sum n m)
  (cond ((= n m) m)
    (else (+ n (sum (+ n 1) m)))))

Implementation:

(define (sum n m)
  (let
    ((nn (natural n))
     (nm (natural m)))
    (letrec
      ((_sum
         (lambda (n m)
           (let ((x (+ 1 (- m n))))
             (+ (quotient (+ (* x x) x)
                          2)
                (* (- n 1) x))))))
    (cond ((> nn nm)
        (bottom '(sum: bad range)))
      (else (_sum nn nm))))))

Example:

(sum 2 5) 
=> 14

See also:
product, iota.