SketchyLISP Reference |
Copyright (C) 2007 Nils M Holm |
<<[fold-right] | [Index] | [head?]>> |
Conformance: R5RS Scheme
Purpose: Compute the greatest common divisor of a sequence of numbers. This function uses Euclid's Algorithm.
Arguments:
A... - numbers
Implementation:
(define (gcd . a) (letrec ((_gcd (lambda (a b) (cond ((zero? b) a) ((zero? a) b) ((n< a b) (_gcd a (nremainder b a))) (else (_gcd b (nremainder a b))))))) (fold-left _gcd 0 (map (lambda (x) (natural (abs x))) a))))
Example:
(gcd 99 -77) => 11
<<[fold-right] | [Index] | [head?]>> |