[Haskell-cafe] Josephus problem and style

Duncan Coutts duncan.coutts at worc.ox.ac.uk
Sun Apr 1 19:12:17 EDT 2007


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 ... urge to fuse ...

Actually the interesting thing that makes this example tricky to fuse
using automagic techniques is that while loop looks like it should be a
good producer and consumer, it's also recursive. Hmm, interesting.

Duncan



More information about the Haskell-Cafe mailing list