|
SketchyLISP Reference |
Copyright (C) 2007 Nils M Holm |
| <<[modulo] | [Index] | [n+]>> |
Conformance: SketchyLISP Core
Purpose: Compute the product of two natural numbers.
Arguments:
A - natural number
B - natural number
Implementation:
(define (n* a b)
(letrec
; X*10 where X=/=0
((x10
(lambda (x)
(list->integer
(append (integer->list x) '(0d))
#t)))
; Add A to R B times.
; A,R are numbers, B is a decimal digit.
(addn
(lambda (a b r)
(cond ((zero? b) r)
(else (addn a (n- b 1) (n+ a r))))))
; R=A*B
; B is in reverse order.
(tms
(lambda (a b r)
(cond ((null? b) r)
(else (tms (x10 a) (cdr b)
(addn a (list->integer (list (car b)))
r)))))))
; avoid leading zeroes in result
(cond ((zero? a) 0)
(else (tms a (reverse (integer->list b))
0)))))
Example:
(n* 4 5) => 20
See also:
digits,
nquotient,
nremainder,
n+,
n-,
*.
| <<[modulo] | [Index] | [n+]>> |