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

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)
        (#t (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)
        (#t (tms (x10 a) (cdr b)
                 (addn a (list->integer (list (car b)))
                       r)))))))

    ; avoid leading zeroes in result
    (cond ((zero? a) 0)
      (#t (tms a (reverse (integer->list b))
            0)))))

Example:

(n* 4 5) 
=> 20

See also:
digits, nquotient, nremainder, n+, n-, *.