Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Looping over a function in Haskell
- split :: [a] -> ([a],[a])
- split xs = splitAt (length xs `div` 2) xs
- riffle :: [a] -> [a] -> [a]
- riffle xs [] = xs
- riffle [] ys = ys
- riffle (x:xs) (y:ys) = x:y:riffle xs ys
- shuffle :: Int -> [a] -> [a]
- shuffle 0 xs = xs
- shuffle n xs = shuffle (n-1) (riffle a b)
- where (a, b) = split xs
- riffle [1,2,3] [4,5,6] = [1,4,2,5,3,6]
- repeats :: [Int] -> Int
- > iterate (uncurry riffle . split) "ABCDEF"
- ["ABCDEF","ADBECF","AEDCBF","ACEBDF","ABCDEF","ADBECF","AEDCBF","ACEBDF", ...]
- > takeWhile (/= "ABCDEF") . tail $ iterate (uncurry riffle . split) "ABCDEF"
- ["ADBECF","AEDCBF","ACEBDF"]
- iterate f x = [x, f x, f (f x), f (f (f x)) ....]
- repeats xs = iter 1 (...) where
- iter n ys = if ys == xs
- then n
- else iter (...) (...)
- repeats xs = (...) $ takeWhile (...) $ iterate (shuffle 1) (...)
Add Comment
Please, Sign In to add comment