Last active
August 29, 2015 14:06
-
-
Save Softwave/369c364b1d044acc73a0 to your computer and use it in GitHub Desktop.
This file contains 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
data Color = Red | Black | Rgb Int Int Int | |
toString : Color -> String | |
toSring color = | |
case color of | |
Red -> "Red" | |
Black -> "Black" | |
Rgb r g b -> "Other" | |
data List = Empty | NotEmpty Int List | |
--list = NotEmpty 1 (NotEmpty 2 (NotEmpty 3 Empty)) | |
list = 1 :: 2 :: 3 :: [] -- 1,2,3 | |
--length list = | |
-- case list of | |
-- Empty -> 0 | |
-- NotEmpty n rest -> 1 + length rest | |
length list = | |
case list of | |
[] -> 0 | |
n :: rest -> 1 + length rest | |
sum : List -> Int | |
sum list = | |
case list of | |
Empty -> 0 | |
NotEmpty n rest -> n + sum rest | |
--squareAll list = | |
-- case list of | |
-- Empty -> Empty | |
-- NotEmpty n rest -> NotEmpty (n^2) (squareAll rest) | |
squareAll list = map (\x -> x^2) list | |
incrementAll list = map (\x -> x + 1) list | |
map : (a -> b) -> [a] -> [b] | |
map f list = | |
case list of | |
[] -> [] | |
n :: rest -> f n :: map f rest | |
--add a b = a + b | |
add = \a -> \b -> a + b | |
reduce : (a -> b -> b) -> b -> [a] -> b | |
reduce accumulate base list = | |
case list of | |
[] -> base | |
n :: reset -> accumulate n (reduce accumulate base rest) | |
--length list = reduce (\n acc -> acc + 1) 0 list | |
data Boolean = Tru | |
| Fls | |
| Not Boolean | |
| And Boolean Boolean | |
| Or Boolean Boolean | |
expr = Or (Not Tru) Fls | |
eval : Boolean -> Bool | |
eval boolean = | |
case boolean of | |
Fls -> False | |
Tru -> True | |
Not b -> not (eval b) | |
And b1 b2 -> eval b1 && eval b2 | |
Or b1 b2 -> eval b1 || eval b2 | |
data Tree a = Node a (Tree a) (Tree a) | EmptyTree | |
sum : Tree Int -> Int | |
sum tree = | |
case tree of | |
EmptyTree -> 0 | |
Node x left right -> n + sum left + sum right | |
main = asText (eval expr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment