Quick actions

cmd+k|ctrl+k

Navigation

Languages

linked list pretty show

Snippet info

Language

Haskell

Visibility

public

Author

danielmg17

Created

2019-07-19T21:33:05Z

Updated

2019-07-20T22:23:05Z

{-# language GADTs #-}
data List a where
    Nil :: List a
    Cons :: (Show a, Ord a) => a -> List a -> List a
    
instance Show (List a) where
    show list = "[ " ++ show' list ++ " ]"
   
lfoldr :: (a -> a -> a) -> List a -> a
lfoldr f Nil = error "empty List"
lfoldr f (Cons a (Cons b Nil)) = f a b
lfoldr f (Cons a as) = f a $ lfoldr f as
 
findMax :: List a -> List a
findMax Nil = Nil
findMax (Cons a Nil) = Cons a Nil
findMax (Cons a (Cons b ls))
    | a > b = Cons a $ Cons b ls
    | otherwise = findMax $ Cons b ls


show' :: List a -> String
show' (Nil) = ""
show' (Cons a Nil) = show a
show' (Cons a as) = show a ++ " -> " ++ show' as

selectSort :: List a -> List a
selectSort = undefined

main = do
    print $ lfoldr max $ Cons 1 $ Cons 2 $ Cons 3 Nil
    print $ findMax $ Cons 1 $ Cons 3 $ Cons 2 $ Cons 1 Nil
INFO