Skip to content

Instantly share code, notes, and snippets.

@essic
Last active April 6, 2017 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save essic/420c50a36b335be91713a000de90b426 to your computer and use it in GitHub Desktop.
Save essic/420c50a36b335be91713a000de90b426 to your computer and use it in GitHub Desktop.
LinkedList in Haskell created by essic - https://repl.it/GuFF/49
data LinkedList a =
Cons a (LinkedList a) | Nil
deriving (Show)
push :: a -> LinkedList a -> LinkedList a
push x y =
Cons x y
push_back :: a -> LinkedList a -> LinkedList a
push_back a Nil =
Cons a Nil
push_back a (Cons x y) =
Cons x $ push_back a y
length_list :: LinkedList a -> Integer
length_list Nil =
0
length_list (Cons x y) =
(+) 1 $ length_list y
last_element :: LinkedList a -> Maybe a
last_element x =
case x of
Nil -> Nothing
Cons x Nil -> Just x
Cons _ y -> last_element y
find_first :: (a -> Bool) -> LinkedList a -> Maybe a
find_first f Nil =
Nothing
find_first f (Cons x y)
| f x = Just x
| otherwise = find_first f y
remove :: (a -> Bool) -> LinkedList a -> Maybe (LinkedList a)
remove _ Nil =
Nothing
remove predicate (Cons x xs)
| predicate x = Just xs
| otherwise = fmap (\a -> Cons x a) $ remove predicate xs
filter :: (a -> Bool) -> LinkedList a -> Maybe (LinkedList a)
filter _ Nil =
Nothing
filter predicate (Cons x xs) =
case predicate x of
True -> case xs of
(Cons y Nil) -> Just Nil
(Cons y ys) -> fmap (\a -> Cons y a) $ filter predicate ys
False -> fmap (\a -> Cons x a) $ filter predicate xs
filter' :: (a -> Bool) -> LinkedList a -> LinkedList a
filter' _ Nil = Nil
filter' predicate (Cons x xs) =
go $ predicate x
where
go True = filter' predicate xs
go _ = Cons x $ filter' predicate xs
main :: IO ()
main = do
print $ "list members at push_back 3 on :: Cons 2 Nil -> " ++ show result
print $ "list members after push 1 on :: Cons 2 (Cons 3 Nil) -> " ++ show result2
print $ "list length of :: Cons 1 (Cons 2 (Cons 3 Nil) ) -> " ++ (show . length_list) result2
print $ "last element of :: Cons 1 (Cons 2 (Cons 3 Nil) ) -> " ++ (show . last_element) result2
print $ "last element of empty list Nil -> " ++ (show . last_element) Nil
print $ "find 50 on (Cons 2 (Cons 3 (Cons 50 Nil))) -> " ++ ( show . find_first (\a -> a == 50) ) (Cons 2 (Cons 3 (Cons 50 Nil)))
print $ "find 20 on (Cons 2 (Cons 3 (Cons 50 Nil))) -> " ++ ( show . find_first (\a -> a == 20) ) (Cons 2 (Cons 3 (Cons 50 Nil)))
print $ "remove member 2 of :: Cons 1 (Cons 2 (Cons 3 Nil) ) -> : " ++ (show . remove (\a -> a == 2)) result2
print $ "remove member 45 of :: Cons 1 (Cons 2 (Cons 3 Nil) ) " ++ (show . remove (\a -> a == 45)) result2
print $ "filter all member 3 of :: (Cons 3 (Cons 3 Nil)) : " ++ (show . filter (\a -> a == 3)) (Cons 3 (Cons 3 Nil))
print $ "filter all member 2 of :: (Cons 3 (Cons 3 Nil)) -> " ++ (show . filter (\a -> a == 2)) (Cons 3 (Cons 3 Nil))
print $ "filter all member 2 of :: (Cons 3 (Cons 3 Nil)) with filter'-> " ++ (show . filter' (\a -> a == 2)) (Cons 3 (Cons 3 Nil))
print $ "filter all member 3 with filter' of :: (Cons 3 (Cons 3 (Cons 3 Nil))) -> " ++ (show . filter' (\a -> a == 3)) (Cons 3 (Cons 3 (Cons 3 Nil)))
print $ "filter all member 2 on empty list of Nil -> " ++ (show . filter' (\a -> a == 2)) Nil
where result = push_back 3 $ Cons 2 Nil
result2 = push 1 result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment