Created
February 25, 2012 05:23
-
-
Save ecounysis/1906623 to your computer and use it in GitHub Desktop.
Numbers from "The Little Schemer" in Clojure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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