refs seem silly in this context! Now I REALLY have to get my head wrapped around clojure. sooooooo I only have one thread, but I have to do a dosync? Why can this whole function just work in a thread agnostic way? It's a local variable! If a thread calls it, then a thread should own the memory that's created. Each thread should have its own asynchronous stack to push local variables onto that no one else is allowed to see. Seems like all that transaction, dosync stuff ought to be optional.
I made my own, local list . . . that no one else in the world can see. I should be able to call pop_front on it to get back the first item while also mutating the the list to only contain the rest. Oh, and my other choice is to use recursion. I'll look into that recur thing. On Jan 11, 1:27 am, "Eric Lavigne" <lavigne.e...@gmail.com> wrote: > > 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. > > Sorting is done much more easily with recursion than with iteration. > However, it looks like you are focused on learning language features rather > than programming strategy, so I will just answer your questions. > > I have not actually done any iterative programming in Clojure, as I prefer > the functional approach, so my answers are based on my limited understanding > of the Clojure documentation. > > 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 > > Be careful with def. I think that it creates a global, and it looks like you > want something with a scope limited to this function. > > http://clojure.org/Vars > > > [myList (for [x toSort] [x])] <----- so far so good (not a > > real comment. I don't know how, yet) > > Use a semicolon to create a comment: > > some code ; a comment > > > [ > > (while (> (count myList) 1) <------- infinite loop the way > > written? I don't know how to overwrite myList > > (let [l1 (nth myList 0)][]) > > The line above does nothing. When you write "(let [x 1] expr1) expr2" the > new value of x has a narrow scope so that it only affects expr1. > > You are wanting to create a local variable that you can change. Refs can do > that: > > (let [x (ref 3)] > (dosync > (while (> @x 0) > (ref-set x (- @x 1))) > @x)) > > http://clojure.org/Refs > > > (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) > > After all that work you just return the original list? I must have missed > the part where you tried to change sorted. Any such attempt would fail, > though, because Clojure collections are immutable. Maybe sorted should be a > ref to a list instead of just a list. > > Here is a discussion of sorting implementations in Clojure. I hope that you > find it useful. > > http://www.fatvat.co.uk/2008/12/bubbling-clojure.html > > -- > Education is what survives when what has been learned has been forgotten. > > - B. F. Skinner --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---