that looks interesting, except it is missing the part where you take
the original list and make it into a list of lists using list
comprehension.  That works in python and clojure for me ... "the so
far so good part".  If the recur were to do that repeatedly, the
algorithm would get messed up.  Still awesome to find out
about . . .and actually that's another way to do it because I can call
a helper function after the original divide (list comprehension)
step . . . remembering that mergesort is a divide and conquer problem.

On Jan 11, 1:32 am, "Nick Vogel" <voge...@gmail.com> wrote:
> Ok, first of all, here's how I translated that python code:
>
> (defn msort [myList]
>   (if (> (count myList) 1)
>     (let [l1 (first myList) l2 (second myList)]
>       (recur (concat (drop 2 myList) (my-merge l1 l2))))
>     (first myList)))
>
> The main thing that might need explaining is the recur, which basically goes
> back to either the last function definition or last loop and rebinds
> whatever parameters werre passed in.  In this case it goes back and is as if
> it's calling msort with (concat (drop 2 myList) (my-merge l1 l2)) as
> myList.  The let is just for clarity's sake, you could have just put them in
> place of l1 and l2 in the recur.
>
> On Sun, Jan 11, 2009 at 12:21 AM, e <evier...@gmail.com> wrote:
>
> > I'm just trying to understand basic stuff.
> > say I have a local list called "myList" (assigned using 'let' . . .
> > should I have used something else?)  Who cares what's in it.  Maybe I
> > set it up from a list comprehension from some input to my function.
>
> > I have no idea how to iteratively mess with it since everything is
> > persistent.  Ok, like, say it's a list of lists and I am going to be
> > merging the lists, like Tarjan's mergesort from some book from
> > college.
>
> > so I have myList with contents [[11] [2] [4] [1] [99]]
>
> > here's how I would do it in python:
>
> > def msort(myList):
> >  myList = [[x] for x in someList]
> >  while len(myList) > 1:
> >    l1 = myList.pop(0)
> >    l2 = myList.pop(0)
> >    listmerge = some_merge_function(l1, l2)
> >    myList.append(listmerge)          # important that newly merged go
> > to back of queue to get proper runtime
> >  return myList[0]
>
> > here's what I'm trying to do for clojure, and it's a mess:
>
> > (defn msort [toSort]
> >  (def sorted (let
> >   [myList (for [x toSort] [x])]      <----- so far so good (not a
> > real comment.  I don't know how, yet)
> >   [
> >    (while (> (count myList) 1)      <------- infinite loop the way
> > written?  I don't know how to overwrite myList
> >      (let [l1 (nth myList 0)][])
> >      (let [l2 (nth myList 1)][])
> >      (let [listmerge (some_merge_func l1 l2)][])
> >      (let [myList (concat (drop 2 myList) listmerge)][myList])  <---
> > probably a different local variable
> >      )
> >   ]))
> >  sorted)
>
> > doesn't compile anyway . . . I see that the let is causing the scope
> > to be all screwed up.  l1 and l2 can't be seen for the merge function.
>
> > should I be using let at all here?  Can things be redefined using
> > def?  see how much simpler it is not to say anything?  Which is
> > it .... def or let in python?  Answer: No . . .but I'm sure there's
> > value.  This seems like something that might be in the FAQ. . . .or
> > somewhere back in these discussions.  I'll look around.
>
> > Thanks.
--~--~---------~--~----~------------~-------~--~----~
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
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