Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. unfoldl :: (a -> Maybe (a, b)) -> a -> [b]
  2. unfoldl f a = go a []
  3. where
  4. go a l = case f a of
  5. Nothing -> l
  6. Just (a, b) -> go a (b:l)
  7.  
  8. unfoldr :: (a -> Maybe (b, a)) -> a -> [b]
  9. unfoldr f = go
  10. where
  11. go a = case f a of
  12. Nothing -> []
  13. Just (b, a) -> b:go a
  14.  
  15. natl :: Int -> Maybe (Int, Int)
  16. natl x
  17. | x <= 0 = Nothing
  18. | otherwise = Just (x-1, x)
  19.  
  20. testl :: Bool
  21. testl = unfoldl natl 10 == [1,2,3,4,5,6,7,8,9,10]
  22.  
  23. natr :: Int -> Maybe (Int, Int)
  24. natr = fmap (\(x,y) -> (y,x)) . natl
  25.  
  26. testr :: Bool
  27. testr = unfoldr natr 10 == [10,9,8,7,6,5,4,3,2,1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement