git @ Cat's Eye Technologies Robin / master stdlib / itoa.robin
master

Tree @master (Download .tar.gz)

itoa.robin @masterraw · history · blame

;'<<SPEC'

    -> Tests for functionality "Evaluate Robin Expression (with Stdlib)"

`itoa` evaluates its sole argument to an integer, then evaluates to
a string representing that integer in decimal.

    | (itoa 100)
    = (49 48 48)

    | (itoa 99)
    = (57 57)

    | (itoa 0)
    = (48)

    | (itoa (subtract 0 1))
    = (45 49)

    | (itoa (subtract 0 765))
    = (45 55 54 53)

    | (itoa (literal m))
    ? abort (expected-number m)

    | (itoa)
    ? abort (illegal-arguments

    | (itoa 100 101)
    ? abort (illegal-arguments

'<<SPEC'

(define itoa (fun (val)
  (bind itoa-r (fun (self val)
    (if (equal? val 0)
      ()
      (let ((digit (remainder val 10))
            (rest  (divide val 10)))
        (prepend (add 48 digit) (self self rest)))))
    (if (equal? val 0)
      (list 48)
      (if (lt? val 0)
        (prepend 45 (reverse (itoa-r itoa-r (subtract 0 val))))
        (reverse (itoa-r itoa-r val)))))))