Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unfoldl :: (a -> Maybe (a, b)) -> a -> [b]
- unfoldl f a = go a []
- where
- go a l = case f a of
- Nothing -> l
- Just (a, b) -> go a (b:l)
- unfoldr :: (a -> Maybe (b, a)) -> a -> [b]
- unfoldr f = go
- where
- go a = case f a of
- Nothing -> []
- Just (b, a) -> b:go a
- natl :: Int -> Maybe (Int, Int)
- natl x
- | x <= 0 = Nothing
- | otherwise = Just (x-1, x)
- testl :: Bool
- testl = unfoldl natl 10 == [1,2,3,4,5,6,7,8,9,10]
- natr :: Int -> Maybe (Int, Int)
- natr = fmap (\(x,y) -> (y,x)) . natl
- testr :: Bool
- testr = unfoldr natr 10 == [10,9,8,7,6,5,4,3,2,1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement