laughingboy (11) [Avatar] Offline
#1
Listing 6.3 has the following loop form:

(loop [[part & parts :as work] work] ...

Does the :as part do anything? Are there any circumstances under which work will have a different value that what it is before the loop?
Chouser (21) [Avatar] Offline
#2
Re: What is the purpose of the :as in listing 6.3?
Each subsequent iteration through the loop will give 'work' a new value, as provided by the 'recur' form.
laughingboy (11) [Avatar] Offline
#3
Re: What is the purpose of the :as in listing 6.3?
Yes, but won't work be bound to the new value regardless of the presence of the ":as work" part? It seems to work (no pun intended) without it.
Chouser (21) [Avatar] Offline
#4
Re: What is the purpose of the :as in listing 6.3?
Without ":as work", each iteration through the loop will provide new values to 'part' and 'parts', but 'work' will remain bound to the value passed in to 'sort-pairs'. Now, the only place 'work' is used inside the loop is to test for the terminal condition, that is to see if there is any more work to be done. ...and since the only place where recur is called there will always be at least a 'pivot', there will always be more work to be done when the initial value of 'work' had contents. So, it does indeed function properly even if 'work' remains it's initial value, but it's almost a coincidence of the code that this is the case.

In trying to understand all of the above, I did stumble on a bug when qsort is passed an empty collection, so thank you very much for helping me find that! In fixing the bug, I simplified the code a bit, getting rid of 'cons-when', and ended up introducing a use of 'recur' that could pass to the loop an empty work seq. Without the ":as work" in this version you generally end up with an extra nil in the output seq:

http://gist.github.com/656138

Thanks again for encouraging me to reprocess that example one more time!
laughingboy (11) [Avatar] Offline
#5
Re: What is the purpose of the :as in listing 6.3?
Ah, thanks, I see now. I somehow got confused by the fact that work was a "mere local" -- it's actually in the spot where an arbitrary one-time initialization form goes.

I guess it would be safe to say that the form (let [[.... :as x] x] ...) is redundant, but that doesn't hold true for a similar loop form where rebinding can occur.

BTW, the book is great! I'm learning a whole lot smilie
michael.fogus (135) [Avatar] Offline
#6
Re: What is the purpose of the :as in listing 6.3?
Thank you for the kind words, and thank you for helping us to make it the best we can.
:f