Guest User

Untitled

a guest
Sep 14th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. Looping over a function in Haskell
  2. split :: [a] -> ([a],[a])
  3. split xs = splitAt (length xs `div` 2) xs
  4.  
  5. riffle :: [a] -> [a] -> [a]
  6. riffle xs [] = xs
  7. riffle [] ys = ys
  8. riffle (x:xs) (y:ys) = x:y:riffle xs ys
  9.  
  10. shuffle :: Int -> [a] -> [a]
  11. shuffle 0 xs = xs
  12. shuffle n xs = shuffle (n-1) (riffle a b)
  13. where (a, b) = split xs
  14.  
  15. riffle [1,2,3] [4,5,6] = [1,4,2,5,3,6]
  16.  
  17. repeats :: [Int] -> Int
  18.  
  19. > iterate (uncurry riffle . split) "ABCDEF"
  20. ["ABCDEF","ADBECF","AEDCBF","ACEBDF","ABCDEF","ADBECF","AEDCBF","ACEBDF", ...]
  21.  
  22. > takeWhile (/= "ABCDEF") . tail $ iterate (uncurry riffle . split) "ABCDEF"
  23. ["ADBECF","AEDCBF","ACEBDF"]
  24.  
  25. iterate f x = [x, f x, f (f x), f (f (f x)) ....]
  26.  
  27. repeats xs = iter 1 (...) where
  28. iter n ys = if ys == xs
  29. then n
  30. else iter (...) (...)
  31.  
  32. repeats xs = (...) $ takeWhile (...) $ iterate (shuffle 1) (...)
Add Comment
Please, Sign In to add comment