Skip to content

Instantly share code, notes, and snippets.

@mkhan45
Last active March 11, 2021 18:38
Show Gist options
  • Select an option

  • Save mkhan45/d9b785d4ad71b30cf60748f7b627f06c to your computer and use it in GitHub Desktop.

Select an option

Save mkhan45/d9b785d4ad71b30cf60748f7b627f06c to your computer and use it in GitHub Desktop.
Data.List is already a LinkedList
import Data.Semigroup
data Node a -- a Node containing type a is
= Val a (Node a) -- either a Value of type a and another Node
| End -- or the end of the list
-- Makes it possible for REPL to display a linked list
-- Val 5 (Val 3 (Val 9 End)) becomes 5->3->9->()
instance (Show a) => Show (Node a) where
show (Val a next) = (show a) <> "->" <> (show next)
show End = "()"
-- Allows fmap for linked lists
instance Functor Node where
fmap f End = End
fmap f (Val v next) = Val (f v) (fmap f next)
-- Allows <>, sconcat, and stimes
-- <> is "associative combine"
-- which is basically concatenation
-- (0->2->5->()) <> (3->3->4->()) = (0->2->5->3->3->4->())
-- stimes 3 (0->3->2->()) = (0->3->2->0->3->2->0->3->2->())
instance Semigroup (Node a) where
(<>) End End = End
(<>) End ls = ls
(<>) (Val v next) ls = Val v (next <> ls)
-- gets nth node of list
-- index (0->3->4->()) 1 = 3->4->()
index :: Node a -> Int -> Maybe (Node a)
index n 0 = Just n
index End _ = Nothing
index (Val _ next) i = index next (i - 1)
-- linkedlist from native Haskell list
-- fromList [2, 3, 1] = 2->3->1->()
fromList :: [a] -> Node a
fromList [] = End
fromList (x:xs) = Val x (fromList xs)
ls = fromList [0..10] -- 0->1->2->3->4->5->6->7->8->9->10->()
ls2 = fmap (*2) ls -- 0->2->4->6->8->10->12->14->16->18->20->()
circularLS = (fromList [0..3]) <> circularLS -- 0->1->2->3->0->1->2->3->0->...
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
fibsLL = fromList fibs -- 1->1->2->3->5->8->13...
import Data.Semigroup hiding (Last)
data List a -- a List is either
= Node a (List a) -- a Node followed by another list
| Last a -- or the last node of the list
-- Makes it possible for REPL to display a linked list
-- Val 5 (Val 3 (Val 9 End)) becomes 5->3->9->()
instance (Show a) => Show (List a) where
show (Node a next) = (show a) <> "->" <> (show next)
show (Last a) = show a
-- Allows fmap for linked lists
instance Functor List where
fmap f (Last a) = Last (f a)
fmap f (Node v next) = Node (f v) (fmap f next)
-- Allows <>, sconcat, and stimes
-- <> is "associative combine"
-- which is basically concatenation
-- (0->2->5->()) <> (3->3->4->()) = (0->2->5->3->3->4->())
-- stimes 3 (0->3->2->()) = (0->3->2->0->3->2->0->3->2->())
instance Semigroup (List a) where
(<>) (Last a) ls = Node a ls
(<>) (Node v next) ls = Node v (next <> ls)
-- gets nth node of list
-- index (0->3->4->()) 1 = 3->4->()
index :: List a -> Int -> Maybe (List a)
index n 0 = Just n
index (Node _ next) i = index next (i - 1)
-- linkedlist from native Haskell list
-- fromList [2, 3, 1] = 2->3->1->()
fromList :: [a] -> List a
fromList [x] = Last x
fromList (x:xs) = Node x (fromList xs)
ls = fromList [0..10] -- 0->1->2->3->4->5->6->7->8->9->10->()
ls2 = fmap (*2) ls -- 0->2->4->6->8->10->12->14->16->18->20->()
circularLS = (fromList [0..3]) <> circularLS -- 0->1->2->3->0->1->2->3->0->...
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
fibsLL = fromList fibs -- 1->1->2->3->5->8->13...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment