Created
November 14, 2014 09:00
-
-
Save auduchinok/de1800ca17ac3e460ed8 to your computer and use it in GitHub Desktop.
Tsil
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data Tsil a = Lin | Snoc (Tsil a) a | |
deriving Show | |
foldr' :: (a -> b -> b) -> b -> Tsil a -> b | |
foldr' f a Lin = a | |
foldr' f a (Snoc xs x) = foldr' f (f x a) xs | |
foldl' :: (b -> a -> b) -> b -> Tsil a -> b | |
foldl' f a Lin = a | |
foldl' f a (Snoc xs x) = f (foldl' f a xs) x | |
fromList :: [a] -> Tsil a | |
fromList = foldl (\acc x -> Snoc acc x) Lin | |
toList :: Tsil a -> [a] | |
toList = foldr' (\x acc -> x : acc) [] | |
length' :: Tsil a -> Int | |
length' = foldr' (\_ acc -> acc + 1) 0 | |
map' :: (a -> b) -> Tsil a -> Tsil b | |
map' f = foldl' (\acc x -> Snoc acc $ f x) Lin | |
reverse' :: Tsil a -> Tsil a | |
reverse' = foldr' (\x acc -> Snoc acc x) Lin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment