Thank you Jason, this is indeed a much nicer solution.

On Friday, June 24, 2016 at 8:51:26 PM UTC+2, Jason Felice wrote:
>
> Recursive descent parsers are much smaller for simple cases.  Basically, 
> you write one function per level of precedence, and each tries, greedily, 
> to consume as much as possible for that level of precedence.  e.g.
>
> (defn parse-level1 ;; + and -
>   [list]
>   ... ; calls parse-level2 repeatedly so long as next operator is + or -
>   [l1-expr remaining-tokens])
>
> (defn parse-level2 ;; * and /
>   ... ; calls parse-level3 repeatedly so long as next operator is * or /
>   [list]
>   ...
>   [l2-expr remaining-tokens])
>
> (defn parse-level3 ;; numbers
>   [list]
>   (if (number? list)
>     [(first list) (rest list)]))
>
>
>
> On Fri, Jun 24, 2016 at 2:28 PM, Botond Balázs <balazs...@gmail.com 
> <javascript:>> wrote:
>
>> Hi,
>>
>> I'm working through Clojure for the Brave and True and there's a little 
>> exercise at the end of Chapter 7 
>> <http://www.braveclojure.com/read-and-eval/>:
>>
>> Create an infix function that takes a list like (1 + 3 * 4 - 5) and 
>>> transforms it into the lists that Clojure needs in order to correctly 
>>> evaluate the expression using operator precedence rules.
>>
>>
>> I ended up implementing the shunting yard algorithm 
>> <https://en.wikipedia.org/wiki/Shunting-yard_algorithm> in Clojure to 
>> solve it (65 lines, many functions), but I have the suspicion that this 
>> exercise isn't meant to be this complicated and that I'm missing a very 
>> obvious and elegant solution. What do you think?
>>
>> Thanks,
>> Botond
>>
>> -- 
>> You received this message because you are subscribed to the Google
>> Groups "Clojure" group.
>> To post to this group, send email to clo...@googlegroups.com 
>> <javascript:>
>> Note that posts from new members are moderated - please be patient with 
>> your first post.
>> To unsubscribe from this group, send email to
>> clojure+u...@googlegroups.com <javascript:>
>> 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 unsubscribe from this group and stop receiving emails from it, send an 
>> email to clojure+u...@googlegroups.com <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to