Hey everyone,

I've been trying all morning (more than 3 hours) to improve my macro but with little success...basically what I had before i started fiddling with it was a macro which would take an expression of the form (1 + 4 * 5 - 1 / 2) and would return the expression in prefix form: (/ (- (* (+ 1 4) 5) 1) 2)...it would not handle nested parens and no operator precedence...This is it:
---------------------------------------------------------------------------------------------------------------------------------------
(defmacro functionize [macro]
  `(fn [& args#] (eval (cons '~macro args#))))

(defmacro infix-to-prefix [expr]
(if-not (empty? (filter #(list? %))  '~expr))
(infix-to-prefix
   (map #(if (list? %)
         (apply (functionize infix-to-prefix) %) %) 'expr))
`(loop [
          ops#   (filter #(not (number? %)) '~expr)
          args#  (filter #(number? %)         '~expr)
         ]
 (if (empty? ops#) (first args#)
     (let [[a# b# & more#] args#]
     (recur
         (rest ops#)
         (conj more#
         (list (first ops#) a# b#))))))))

;;usage : (infix-to-prefix (1 + 4 * 6))
;;=> (* (1 + 4) 6)
--------------------------------------------------------------------------------------------------------------------------------------
I know for a fact that the bit after the back-quote works exactly as explained above...all the new code is above the back-quoted loop - recur form. all i'm trying to do is replace all the parens at each level with values calculated recursively using eval on the returned expression....However i can't seem to go any further...I am literally about to give up...I know this may not be necessarily the best approach but i 've worked hard on this and i want to see it succeed!

Any help is greatly appreciated...

Jim




--
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