Skip to content

Instantly share code, notes, and snippets.

@auduchinok
Created November 14, 2014 09:00
Show Gist options
  • Save auduchinok/de1800ca17ac3e460ed8 to your computer and use it in GitHub Desktop.
Save auduchinok/de1800ca17ac3e460ed8 to your computer and use it in GitHub Desktop.
Tsil
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