Re: [Haskell-cafe] Josephus problem and style

2007-04-03 Thread Anthony Chaumas-Pellet
Thanks for your comments everyone! There is one point that has left me puzzled, though. From: Ross Paterson [EMAIL PROTECTED] You show a bias towards tail recursion. It would be neater (and lazier) to return the executed ones incrementally. This is easier if you don't distinguish the

Re: [Haskell-cafe] Josephus problem and style

2007-04-03 Thread Malcolm Wallace
Anthony Chaumas-Pellet [EMAIL PROTECTED] wrote: From: Ross Paterson [EMAIL PROTECTED] You show a bias towards tail recursion. It would be neater (and lazier) to return the executed ones incrementally. Why is tail recursion a bad thing for a finite function? Tail recursion tends to

[Haskell-cafe] Josephus problem and style

2007-04-01 Thread Anthony Chaumas-Pellet
Hello, I've written a function to compute the general Josephus problem, giving both the number of the survivor and the order in which the others are killed. However, I am not overly fond of my actual Haskell implementation, so I would like some comments on elegance. My current function is as

Re: [Haskell-cafe] Josephus problem and style

2007-04-01 Thread Ross Paterson
On Sun, Apr 01, 2007 at 06:24:23PM +0200, Anthony Chaumas-Pellet wrote: I've written a function to compute the general Josephus problem, giving both the number of the survivor and the order in which the others are killed. However, I am not overly fond of my actual Haskell implementation, so I

Re: [Haskell-cafe] Josephus problem and style

2007-04-01 Thread Paul Hudak
Here's a solution that I think is a bit more elegant. -Paul josephus n k = let loop xs = let d:r = drop (k-1) xs in d : loop (filter (/= d) r) in take n (loop (cycle [1..n])) Anthony Chaumas-Pellet wrote: Hello, I've written a function to compute the general

Re: [Haskell-cafe] Josephus problem and style

2007-04-01 Thread Duncan Coutts
On Sun, 2007-04-01 at 16:46 -0400, Paul Hudak wrote: Here's a solution that I think is a bit more elegant. -Paul josephus n k = let loop xs = let d:r = drop (k-1) xs in d : loop (filter (/= d) r) in take n (loop (cycle [1..n])) Lovely. .. must.. resist

Re: [Haskell-cafe] Josephus problem and style

2007-04-01 Thread Ross Paterson
On Mon, Apr 02, 2007 at 09:12:17AM +1000, Duncan Coutts wrote: On Sun, 2007-04-01 at 16:46 -0400, Paul Hudak wrote: Here's a solution that I think is a bit more elegant. -Paul josephus n k = let loop xs = let d:r = drop (k-1) xs in d : loop (filter (/=

Re: [Haskell-cafe] Josephus problem and style

2007-04-01 Thread Ross Paterson
Here's the sequence version: import Data.Sequence as Seq josephus k n = reduce (fromList [1..n]) where reduce xs | Seq.null xs = [] | otherwise = case viewl (rotate (k-1) xs) of x : xs' - x : reduce xs' rotate i xs = back front