tag:blogger.com,1999:blog-22587889.post2681874066349580371..comments2024-02-11T13:21:47.930+05:30Comments on Ruminations of a Programmer: Learning Haskell: Solving the Josephus Flavius gameAnonymoushttp://www.blogger.com/profile/01613713587074301135noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-22587889.post-85152381955288204492009-02-10T22:30:00.000+05:302009-02-10T22:30:00.000+05:30Some interesting variations on Haskell reddit .. h...Some interesting variations on Haskell reddit .. http://www.reddit.com/r/haskell/comments/7w04j/learning_haskell_solving_the_josephus_flavius_game/Anonymoushttps://www.blogger.com/profile/01613713587074301135noreply@blogger.comtag:blogger.com,1999:blog-22587889.post-11095157842868011872009-02-10T22:15:00.000+05:302009-02-10T22:15:00.000+05:30My solution:josephus :: Int -> Int -> ([Int]...My solution:<BR/><BR/>josephus :: Int -> Int -> ([Int], Int)<BR/>josephus groupSize nth = (order, last order)<BR/> where order = josephusOrdering [1..groupSize] nth<BR/><BR/>josephusOrdering :: [Int] -> Int -> [Int]<BR/>josephusOrdering [] _ = []<BR/>josephusOrdering xs n = victim : josephusOrdering survivors n<BR/> where (victim, survivors) = next xs n<BR/><BR/>next :: [Int] -> Int -> (Int, [Int])<BR/>next (x:xs) 1 = (x, xs)<BR/>next (x:xs) n = next (xs ++ [x]) (n - 1)Unknownhttps://www.blogger.com/profile/09458819790768430576noreply@blogger.comtag:blogger.com,1999:blog-22587889.post-79411345249497397962009-02-09T23:39:00.000+05:302009-02-09T23:39:00.000+05:30I would be inclined to keep the list of the dead s...I would be inclined to keep the list of the dead separate from the list of the living to ensure that they don't get mixed up:<BR/><BR/><BR/>josephus init nth = (dead, last dead)<BR/> where <BR/><BR/> -- circular: alive gets fed back<BR/> (alive,dead) = victim (length init) (init++alive)<BR/><BR/> -- find victims and aggregate sequence<BR/> victim 0 _ = ([],[])<BR/> victim n alive = <BR/> let (f, ~(target:s)) = splitAt (nth-1) alive<BR/> (alive',dead') = victim (n-1) s<BR/> in (f ++ alive',(target:dead'))Anonymousnoreply@blogger.com