Nice one Sam.  I think you could add order of operations by transforming
the form in multiple passes. First /, then *, then + and -.  Not the most
efficient solution but a start.

Alex

On Wed, May 2, 2012 at 6:14 PM, Sam Ritchie <sritchi...@gmail.com> wrote:

> Hey Jim, what do you think of something like this?
>
> (defmacro infix->prefix [form]
>   (loop [[a op b & more :as form] form]
>     (if (not op)
>       a
>       (recur (cons (list op a b) more)))))
>
> This transforms the list by plucking off three items at a time,
> rearranging them into prefix notation then popping them back onto the list
> in the recursion. When only one form's left inside the list, the macro
> returns that form, passing it into the compiler. Here's an example
> expansion:
>
> user> (infix->prefix (1 * 2 + 3 - 6))
> (1 * 2 + 3 - 6)
> ((* 1 2) + 3 - 6)
> ((+ (* 1 2) 3) - 6)
> ((- (+ (* 1 2) 3) 6))
> -1
>
> On Wed, May 2, 2012 at 1:24 PM, Jim - FooBar(); <jimpil1...@gmail.com>wrote:
>
>> 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+unsubscribe@**googlegroups.com<clojure%2bunsubscr...@googlegroups.com>
>> For more options, visit this group at
>> http://groups.google.com/**group/clojure?hl=en<http://groups.google.com/group/clojure?hl=en>
>
>
>
>
> --
> Sam Ritchie, Twitter Inc
> 703.662.1337
> @sritchie09
>
> (Too brief? Here's why! http://emailcharter.org)
>
>  --
> 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
>

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