On 9 Wrz, 14:30, Stefan Rohlfing <[email protected]> wrote:
> In order to get some more insight into recursion I wrote the following
> function but ended up in confusion:
>
> (defn prefix->postfix [expr]
> (if (coll? expr)
> (let [ [op arg1 arg2] expr]
> [ (prefix->postfix arg1) (prefix->postfix arg2) op]))
> expr)
>
> I expected the result to be a vector, such as
> (prefix->postfix '(+ 1 2))
> ;; [1 2 +]
>
> However, the expression passed as an argument is just returned, at
> least this is what is looks like:
>
> (prefix->postfix '(* 2 3))
> ;; --> (* 2 3)
>
> (prefix->postfix '(+ 1 (* 2 3)))
> ;; --> (+ 1 (* 2 3))
>
> I seems there is still some way to go until I can really 'think in
> recursion'.
>
> Therefore I would appreciate if someone could tell me why the above
> function is not returning the expected result.
>
> Stefan
It return the last expression in the funciton body, which is in this
example: expr - which is unchanged input parameter.
You probably meant
(defn prefix->postfix [expr]
(if (coll? expr)
(let [ [op arg1 arg2] expr]
[ (prefix->postfix arg1) (prefix->postfix arg2) op])
expr))
--
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