[Haskell-cafe] Linear Shuffle

2005-01-15 Thread Okasaki, C. DR EECS
Clearly, you can do a perfect shuffle in O(N) time using mutable arrays, using the standard imperative algorithm. You can also do it in O(N) expected time using *immutable* arrays, using Haskell's bulk array operations. 1. Start with a list of length N. If N 2, stop. 2. Pair each element with

Re: [Haskell-cafe] Linear shuffle

2005-01-15 Thread Ben Rudiak-Gould
Marcin 'Qrczak' Kowalczyk wrote: Henning Thielemann [EMAIL PROTECTED] writes: I did some shuffling based on mergesort [...] I think it doesn't guarantee equal probabilities of all permutations. It doesn't (proof: it has a bounded runtime, which can't be true of a perfect shuffling algorithm

Re: [Haskell-cafe] Linear shuffle

2005-01-15 Thread Ben Rudiak-Gould
Scott Turner wrote: Analogous to quicksort's bad behavior in the worst case, an invocation of 'partition' is not guaranteed to make any progress with the shuffling, because one of the output lists might receive all of the input items. It's worse than quicksort, because there's no guarantee that

[Haskell-cafe] Linear shuffle

2005-01-14 Thread Gracjan Polak
Hi, I want to implement linear list shuffle in Haskell (http://c2.com/cgi/wiki?LinearShuffle) and I invented code: shuffle :: [a] - IO [a] shuffle [] = return [] shuffle x = do r - randomRIO (0::Int,length x - 1) s - shuffle (take r x ++ drop (r+1) x) return ((x!!r) : s) This

Re: [Haskell-cafe] Linear shuffle

2005-01-14 Thread Ketil Malde
Gracjan Polak [EMAIL PROTECTED] writes: shuffle :: [a] - IO [a] shuffle [] = return [] shuffle x = do r - randomRIO (0::Int,length x - 1) s - shuffle (take r x ++ drop (r+1) x) return ((x!!r) : s) This algorithm seems not effective, length, take, drop and (!!) are costly.

Re: [Haskell-cafe] Linear shuffle

2005-01-14 Thread Keean Schupke
Gracjan Polak wrote: This algorithm seems not effective, length, take, drop and (!!) are costly. Is there any better way to implement shuffle? Here is an algorithm known as a perfect-shuffle... I am not too sure of the efficiency compared to the example you gave, but it builds a tree to enable

Re: [Haskell-cafe] Linear shuffle

2005-01-14 Thread Ketil Malde
Tomasz Zielonka [EMAIL PROTECTED] writes: On Fri, Jan 14, 2005 at 09:17:41AM +0100, Gracjan Polak wrote: This algorithm seems not effective, length, take, drop and (!!) are costly. Is there any better way to implement shuffle? You can use mutable arrays (modules Data.Array.MArray,

Re: [Haskell-cafe] Linear shuffle

2005-01-14 Thread Tomasz Zielonka
On Fri, Jan 14, 2005 at 10:17:27AM +0100, Ketil Malde wrote: Tomasz Zielonka [EMAIL PROTECTED] writes: On Fri, Jan 14, 2005 at 09:17:41AM +0100, Gracjan Polak wrote: This algorithm seems not effective, length, take, drop and (!!) are costly. Is there any better way to implement shuffle?

Re: [Haskell-cafe] Linear shuffle

2005-01-14 Thread Henning Thielemann
On Fri, 14 Jan 2005, Gracjan Polak wrote: I want to implement linear list shuffle in Haskell (http://c2.com/cgi/wiki?LinearShuffle) and I invented code: shuffle :: [a] - IO [a] shuffle [] = return [] shuffle x = do r - randomRIO (0::Int,length x - 1) s - shuffle (take r x ++

Re: [Haskell-cafe] Linear shuffle

2005-01-14 Thread Keean Schupke
Please see: http://okmij.org/ftp/Haskell/perfect-shuffle.txt For an explanation of the algorithm. Keean. Ketil Malde wrote: Tomasz Zielonka [EMAIL PROTECTED] writes: On Fri, Jan 14, 2005 at 09:17:41AM +0100, Gracjan Polak wrote: This algorithm seems not effective, length, take, drop and

Re: [Haskell-cafe] Linear shuffle

2005-01-14 Thread John Meacham
On Fri, Jan 14, 2005 at 09:17:41AM +0100, Gracjan Polak wrote: This algorithm seems not effective, length, take, drop and (!!) are costly. Is there any better way to implement shuffle? Oleg wrote a great article on implementing the perfect shuffle. with some sample code.

Re: [Haskell-cafe] Linear shuffle

2005-01-14 Thread Ketil Malde
Tomasz Zielonka [EMAIL PROTECTED] writes: But is that better, really? IIUC, you will now need to shift the first part of the string to the right, so it's still a linear operation for each shuffle. Perhaps I don't know this particular algorithm, but you can shuffle the array with linear

Re: [Haskell-cafe] Linear shuffle

2005-01-14 Thread Ketil Malde
Keean Schupke [EMAIL PROTECTED] writes: Please see: http://okmij.org/ftp/Haskell/perfect-shuffle.txt For an explanation of the algorithm. Right. I was commenting based on the source posted by Gracjan. (And http://c2.com/cgi/wiki?LinearShuffle contains a variety of shuffling algorithms).

Re: [Haskell-cafe] Linear shuffle

2005-01-14 Thread Gracjan Polak
Henning Thielemann wrote: Is it a good idea to use IO monad for this plain computation? It is needed as random number supply. -- Gracjan ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Re: [Haskell-cafe] Linear shuffle

2005-01-14 Thread Gracjan Polak
John Meacham wrote: Oleg wrote a great article on implementing the perfect shuffle. with some sample code. http://okmij.org/ftp/Haskell/misc.html#perfect-shuffle Thats the kind of answer I was hoping to get :) Thanks. shuffle could be useful in standard library. At least Python has it. I

Re: [Haskell-cafe] Linear shuffle

2005-01-14 Thread Daniel Fischer
Am Freitag, 14. Januar 2005 09:50 schrieb Ketil Malde: Gracjan Polak [EMAIL PROTECTED] writes: shuffle :: [a] - IO [a] shuffle [] = return [] shuffle x = do r - randomRIO (0::Int,length x - 1) s - shuffle (take r x ++ drop (r+1) x) return ((x!!r) : s) This algorithm

Re: [Haskell-cafe] Linear shuffle

2005-01-14 Thread Henning Thielemann
On Fri, 14 Jan 2005, Scott Turner wrote: The shuffling algorithms mentioned so far are comparable to insertion/selection sort. I had come up with a shuffler that relates to quicksort, in that it partitions the input randomly into lists and works recursively from there. It looks efficient

Re: [Haskell-cafe] Linear shuffle

2005-01-14 Thread Marcin 'Qrczak' Kowalczyk
Henning Thielemann [EMAIL PROTECTED] writes: I did some shuffling based on mergesort, that is a list is randomly split (unzipped) into two lists and the parts are concatenated afterwards. You must repeat this some times. It even works for infinite lists. I think it doesn't guarantee equal

Re: [Haskell-cafe] Linear shuffle

2005-01-14 Thread Atwood, John Wesley
Gracjan Polak [EMAIL PROTECTED] wrote: John Meacham wrote: Oleg wrote a great article on implementing the perfect shuffle. with some sample code. http://okmij.org/ftp/Haskell/misc.html#perfect-shuffle Thats the kind of answer I was hoping to get :) Thanks. shuffle could