Question

The function I am looking for is something like:

(split 1/3) => (1 . 3)
(split 1093/209) => (1093 . 209)

Is it possible in RnRS, SRFI or any custom implementation?

Était-ce utile?

La solution

Yes. To get the numerator and denominator of a rational number you have two procedures you can use:

(numerator (/ 6 4))    ⇒  3
(denominator (/ 6 4))  ⇒  2

As it demonstrates the values returned are the lowest terms. The report specifies the denominator is always positive and the denominator of 0 is defined to be 1.

There has been no change in this behavior for a long time. I see the same description in R3RS until the latest R7RS so you can expect it to exist in all implementations of Scheme that exists today.

Just for fun:

(define (split number)
  (cons (numerator number)
        (denominator number)))
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top