From 809af93e354e5ea53c668f37dd0f249f771c9700 Mon Sep 17 00:00:00 2001 From: Colin Jones Date: Mon, 9 Apr 2018 10:12:47 -0500 Subject: [PATCH] Focus the problem and clean up naming in macros Fixes #134 --- resources/koans.clj | 4 +--- src/koans/24_macros.clj | 18 +++++++++--------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/resources/koans.clj b/resources/koans.clj index a63ec62..faa46bc 100644 --- a/resources/koans.clj +++ b/resources/koans.clj @@ -264,8 +264,6 @@ (drop 2 form) "Hello, Macros!" 10 - '(+ 9 1) - '(* 10 2) - '(+ 10 (2 * 3))]}] + '(+ 9 1)]}] ] diff --git a/src/koans/24_macros.clj b/src/koans/24_macros.clj index d9017ea..2d8936c 100644 --- a/src/koans/24_macros.clj +++ b/src/koans/24_macros.clj @@ -7,23 +7,23 @@ (defmacro infix [form] (list (second form) (first form) (nth form 2))) -(defmacro infix-better [form] +(defmacro infix-concise [form] `(~(second form) ; Note the syntax-quote (`) and unquote (~) characters! __ - __ )) + __)) -(defmacro r-infix [form] +(defmacro recursive-infix [form] (cond (not (seq? form)) __ (= 1 (count form)) - `(r-infix ~(first form)) + `(recursive-infix ~(first form)) :else (let [operator (second form) first-arg (first form) others __] `(~operator - (r-infix ~first-arg) - (r-infix ~others))))) + (recursive-infix ~first-arg) + (recursive-infix ~others))))) (meditations "Macros are like functions created at compile time" @@ -36,10 +36,10 @@ (= __ (macroexpand '(infix (9 + 1)))) "You can do better than that - hand crafting FTW!" - (= __ (macroexpand '(infix-better (10 * 2)))) + (= '(* 10 2) (macroexpand '(infix-concise (10 * 2)))) "Things don't always work as you would like them to... " - (= __ (macroexpand '(infix-better ( 10 + (2 * 3))))) + (= '(+ 10 (2 * 3)) (macroexpand '(infix-concise (10 + (2 * 3))))) "Really, you don't understand recursion until you understand recursion" - (= 36 (r-infix (10 + (2 * 3) + (4 * 5))))) + (= 36 (recursive-infix (10 + (2 * 3) + (4 * 5)))))