[Haskell-cafe] Josephus problem and style

Ross Paterson ross at soi.city.ac.uk
Sun Apr 1 19:50:22 EDT 2007


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
          where (front, back) = Seq.splitAt (i `mod` Seq.length xs) xs



More information about the Haskell-Cafe mailing list