Hi all,

I'm running into a problem where I need to define several functions
and/or vars inside another function, but the function has multiple
definitions and they all need to have access to the inner things.
This could be solved by making private functions and using partial,
but this sounds like a good problem to tackle with a macro.  I'm not
sure how to do it, though, being so new to the language.  Here's a
useful example, where defwithinner is the macro name:


(defwithinner primitives
  "Generates pythagorean primitives.  Will filter them against pred if
given."
  (let [U (gen-matrix [1 2 2 -2 -1 -2 2 2 3] 3 3)
        A (gen-matrix [1 2 2 2 1 2 2 2 3] 3 3)
        D (gen-matrix [-1 -2 -2 2 1 2 2 2 3] 3 3)
        S (gen-vector [3 4 5])]
    (letfn [(prims-from [m] [(mult m U) (mult m A) (mult m D)])
            (next-prims [l] (flatten (map prims-from l)))]))
  ([] (iterate next-prims [S]))
  ([pred] (iterate #(filter pred (next-prims %)) [S])))

--->

(defn primitives
  "Generates pythagorean primitives.  Will filter them against pred if
given."
  ([]
  (let [U (gen-matrix [1 2 2 -2 -1 -2 2 2 3] 3 3)
        A (gen-matrix [1 2 2 2 1 2 2 2 3] 3 3)
        D (gen-matrix [-1 -2 -2 2 1 2 2 2 3] 3 3)
        S (gen-vector [3 4 5])]
    (letfn [(prims-from [m] [(mult m U) (mult m A) (mult m D)])
            (next-prims [l] (flatten (map prims-from l)))]
      (iterate next-prims [S]))))
  ([pred]
  (let [U (gen-matrix [1 2 2 -2 -1 -2 2 2 3] 3 3)
        A (gen-matrix [1 2 2 2 1 2 2 2 3] 3 3)
        D (gen-matrix [-1 -2 -2 2 1 2 2 2 3] 3 3)
        S (gen-vector [3 4 5])]
    (letfn [(prims-from [m] [(mult m U) (mult m A) (mult m D)])
            (next-prims [l] (flatten (map prims-from l)))]
      (iterate #(filter pred (next-prims %)) [S])))))


OTOH, being so new to clojure I've probably missed some mechanism
which already takes care of this problem.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to