Skip to content

Instantly share code, notes, and snippets.

@syou6162
Created August 20, 2012 10:40
Show Gist options
  • Save syou6162/3403043 to your computer and use it in GitHub Desktop.
Save syou6162/3403043 to your computer and use it in GitHub Desktop.
何重和みたいなものをexpressionを取って簡単に書けるようにするマクロ
(defmacro sum [bindings expr]
`(reduce + (for ~bindings ~expr)))
(macroexpand-1 '(sum [x '(1 2 3)
y '(4 5 6)]
(+ x y)))
(clojure.core/reduce
clojure.core/+
(clojure.core/for [x (quote (1 2 3))
y (quote (4 5 6))]
(+ x y)))
;; \sum_{x \in {1 2 3} \sum_{y \in {4 5 6} (x + y)
(sum [x '(1 2 3)
y '(4 5 6)]
(+ x y)) ; 63
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment