1

How to find the numerator and the denominator of a rational in Clojure?

How can I convert a rational to a pair of ints?

Thumbnail
  • 13,293
  • 2
  • 29
  • 37
Igor Chubin
  • 61,765
  • 13
  • 122
  • 144

2 Answers2

4

Just use the numerator and denominator functions.

> (numerator (/ 2 3))
2
> (denominator (/ 2 3))
3
>  
sloth
  • 99,095
  • 21
  • 171
  • 219
2

Use numerator and denominator functions to extract the pair of numbers like so

(defn ratio-to-vector [r]
 ((juxt numerator denominator) r))

For example

(ratio-to-vector 22/7) ;=> [22 7]

Note that in this form the function will break on other numerical types.

KobbyPemson
  • 2,519
  • 1
  • 18
  • 33