On 9 Wrz, 15:27, Stefan Rohlfing <stefan.rohlf...@gmail.com> wrote:
> The indentation was correct by got messed up when I copying the code.
>
> This is how I interpret the function:
> 'expr' is only returned if (coll? expr) returns 'false', with is not
> the case with an argument such as '(+ 2 4).
> Next, this expression is destructured in the 'let' and 'prefix-
>
> >postfix called again:
>
> [ (prefix->postfix 2) (prefix->postfix 4) +]
>
> As 2 and 4 are both not a collection, their values are just returned.
> This should lead to this final result:
>
> [2 4 +]
>
> However, no vector is returned, just the initial argument to the
> function.
>
> I really want to understand what I did wrong here :-)
>
> Stefan

 Hello.

 You understand recursion correctly, the issue is with lisp syntax :)

 Your code:

 (defn prefix->postfix [expr]
   (if (coll? expr)
     (let [ [op arg1 arg2] expr]
       [ (prefix->postfix arg1) (prefix->postfix arg2) op]))
   expr)

Translated to pseudocode it does:

def f(expr):
  if expr is collection:
    calculate vector and ignore the result
  else:
    calculate nil and ignore it
  return expr

It's because code should look like this:

 (defn prefix->postfix [expr]
   (if (coll? expr)
     (let [ [op arg1 arg2] expr]
       [ (prefix->postfix arg1) (prefix->postfix arg2) op])
     expr))

Notice the difference with the placing of the penultimate ")" - this
difference would be easier to notice, if you indent your code
automaticaly - by emacs for example.

Becouse of that paren if in your function is version with only one
branch - this if would evaluate to nil if expr will be not colleciton.

But no matter what the expr is, after the if there is expr and (as a
last expression in the function body) it is what the function returns.

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