Last active
December 22, 2015 02:49
-
-
Save gfredericks/6406008 to your computer and use it in GitHub Desktop.
abstract algebra helpers
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
(defmacro defring | |
[additive-identity | |
multiplicative-identity | |
add | |
negate | |
multiply] | |
`(let [add# ~add | |
negate# ~negate | |
multiply# ~multiply] | |
(defn ~'+ | |
([] ~additive-identity) | |
([x#] x#) | |
([x# y#] (add# x# y#)) | |
([x# y# & zs#] (reduce add# (add# x# y#) zs#))) | |
(defn ~'- | |
([x#] (negate# x#)) | |
([x# y#] (~'+ x# (negate# y#))) | |
([x# y# & zs#] (~'+ x# (negate# (apply add# zs#))))) | |
(defn ~'* | |
([] ~multiplicative-identity) | |
([x#] x#) | |
([x# y#] (multiply# x# y#)) | |
([x# y# & zs#] (reduce multiply# (multiply# x# y#) zs#))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your additive identity should be
0
, not(range)
, and the multiplicative identity should be1
, not(range)
.