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
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
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
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
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
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
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 (/=
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