git @ Cat's Eye Technologies Robin / 0.2 stdlib / multiply.robin
0.2

Tree @0.2 (Download .tar.gz)

multiply.robin @0.2raw · history · blame

;'XXX'

    -> Tests for functionality "Interpret Robin Program (with Arith)"

`multiply` evaluates both of its arguments to numbers and evaluates to the product
of those two numbers.

    | (display
    |   (multiply 6 7))
    = 42

    | (display
    |   (multiply (subtract 0 6) 7))
    = -42

    | (display
    |   (multiply 6 (subtract 0 7)))
    = -42

    | (display
    |   (multiply (subtract 0 6) (subtract 0 7)))
    = -42

`multiply` expects exactly two arguments.

    | (display
    |   (multiply 14))
    ? uncaught exception: (illegal-arguments (14))

    | (display
    |   (multiply 6 7 7))
    ? uncaught exception: (illegal-arguments (6 7 7))

Both of the arguments to `multiply` must be numbers.

    | (display
    |   (multiply 14 #t))
    ? uncaught exception: (expected-number #t)

    | (display
    |   (multiply #t 51))
    ? uncaught exception: (expected-number #t)

'XXX'

(define multiply (macro (self args env)
  (bind multiply-r (fun (self a b) ;(b must be positive)
    (if (equal? b 1)
      a
      (add a (self self a (subtract b 1)))))
    (bind-args (a b) args env
      (if (equal? b 0) 0
        (if (< b 0)
          (if (< a 0)
            (multiply-r multiply-r a (subtract 0 b))
            (subtract 0 (multiply-r multiply-r a (subtract 0 b))))
          (multiply-r multiply-r a b)))))))