Sunday, June 05, 2022

SICP in Clojure: Chapter 2, Exercise 1

 This is my Clojure solution to Chapter 2, Exercise 1:

(ns sicp.ch2.ex01
 (:require [sicp.ch1.ex20 :as ch1-ex20]))

(defn- make-rat
  [n d]
  (let [g (Math/abs (long (ch1-ex20/gcd n d)))
        h (if (neg? d) (- g) g)]
    [(/ n h) (/ d h)]))

(comment
  (make-rat 10 -30) ; => [-1 3]
  #__)


No comments: