Though I'm opposed to posting solutions to school assignments --in
principle-- here's this little something in the hope that it might get you
interested in lisp:
(defn value [e]
(cond (number? e) e
(= (second e) '+) (+ (value (first e)) (value (nth e 2)))
(= (second e) '-) (- (value (first e)) (value (nth e 2)))
(= (second e) '*) (* (value (first e)) (value (nth e 2)))
(= (second e) '/) (quot (value (first e)) (value (nth e 2)))))
(defn e [s] (value (read-string s)))
Then you use (e) like so:
user=> (e "(1 + (2 * (4 * 5)))")
41
Function e converts the string into a list and calls function value with
it. Function value accepts wither a number or a binary operation in a list
and applies the corresponding operator to the operands. Since operands can
themselves be operations, it calls itself recursively on the first and
third elements of the list, which are the operands.
On Tuesday, May 1, 2012 8:19:04 PM UTC-7, Asranz wrote:
>
> oh please if u can teach me!
>
>
> On 1 mayo, 21:29, Armando Blancas <[email protected]> wrote:
> > > i just need to evaluate in infix a string "(1 +2 (4 * 5)"
> >
> > Check out The Little Schemer. It'll teach you the techniques for doing
> this.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en