Created
February 19, 2018 21:22
-
-
Save migimunz/07a4c86aff564b5ae0bb7fb91edade74 to your computer and use it in GitHub Desktop.
CS162 - haskell examples
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
import Prelude hiding (foldl) | |
data Moody a = Happy a | Depressed a | |
deriving Show | |
foo (Happy a) = Happy (a + 1) | |
foo (Depressed a) = Depressed (a - 1) | |
foldl :: (b -> a -> b) -> b -> [a] -> b | |
foldl f d [] = d | |
foldl f d (x:xs) = foldl f (f d x) xs | |
l = [1, 2, 3, 4, 5, 1, 2, 6, 1] | |
main = print $ foldl f 0 l | |
where f acc a = (show a) |
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
import Prelude hiding (head, Nothing) | |
-- head and bottoms | |
data List a = Nil | Cons a (List a) | |
deriving (Show, Eq) | |
data Option a = Nothing | Some a | |
deriving (Show, Eq) | |
safeHead :: List a -> Option a | |
safeHead Nil = Nothing | |
safeHead (Cons a as) = Some a | |
main = | |
let l = Cons 1 (Cons 2 (Cons 3 Nil)) | |
in print (safeHead l) |
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
import Prelude hiding (head) | |
-- There are no NULLs! | |
data List a = Nil | Cons a (List a) | |
deriving (Show, Eq) | |
data Option a = None | Some a | |
deriving (Show, Eq) | |
head :: List a -> Option a | |
head Nil = undefined | |
head (Cons x xs) = undefined | |
main = | |
let l = Cons 1 (Cons 2 (Cons 3 Nil)) | |
in print (head l) |
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
import Prelude hiding (map) | |
data Tree a = Leaf | Branch a (Tree a) (Tree a) | |
deriving (Show, Eq) | |
tree = Branch 2 (Branch 1 Leaf Leaf) (Branch 4 Leaf Leaf) | |
map :: (a -> b) -> Tree a -> Tree b | |
map f Leaf = Leaf | |
map f (Branch a l r) = Branch (f a) (map f l) (map f r) | |
main = print $ map (*10) tree |
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
-- Let's implement a binary tree | |
data Tree a = Leaf | |
| Branch a (Tree a) (Tree a) | |
deriving (Show, Eq) | |
mapTree :: (a -> b) -> Tree a -> Tree b | |
mapTree f Leaf = undefined | |
mapTree f (Branch a l r) = undefined | |
insert :: (Ord a) => a -> Tree a -> Tree a | |
insert a Leaf = Branch a Leaf Leaf | |
insert a (Branch b l r) = undefined | |
main = | |
let tree = (Branch 1 | |
(Branch 0 Leaf Leaf) | |
(Branch 2 Leaf Leaf)) | |
in print tree |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment