Skip to content

Instantly share code, notes, and snippets.

@ecounysis
Created February 25, 2012 05:23
Show Gist options
  • Select an option

  • Save ecounysis/1906623 to your computer and use it in GitHub Desktop.

Select an option

Save ecounysis/1906623 to your computer and use it in GitHub Desktop.
Numbers from "The Little Schemer" in Clojure
(defn add1 [x] (+ x 1))
(defn sub1 [x] (- x 1))
(defn o+ [x y]
(cond (= y 0) x
:else (o+ (add1 x) (sub1 y))))
(defn o- [x y]
(cond (= y 0) x
:else (o- (sub1 x) (sub1 y))))
(defn o* [x y]
(cond (= 0 y) 0
(= 1 y) x
:else (o+ x (o* x (sub1 y)))))
(defn o-div [x y]
(cond (= 0 y) nil
(= 1 y) x
(< x y) 0
:else (add1 (o-div (o- x y) y))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment