Last active
July 20, 2016 12:12
-
-
Save adicirstei/f2236050997579864ca27592482dd1b5 to your computer and use it in GitHub Desktop.
Ninety-nine Elm Problems
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
type Tree a = | |
Empty | |
| Node a (Tree a) (Tree a) | |
mirror : Tree a -> Tree a | |
mirror t = | |
case t of | |
Empty -> Empty | |
Node v l r -> Node v (mirror r) (mirror l) | |
balancedTree : Int -> Tree Char | |
balancedTree n = | |
case n of | |
0 -> Empty | |
1 -> Node 'x' Empty Empty | |
_ -> | |
let | |
r = (n - 1) // 2 | |
l = n - 1 - r | |
in | |
Node 'x' (balancedTree l) (balancedTree r) | |
isSymmetric : Tree a -> Bool | |
isSymmetric t = t == mirror t | |
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
last : List a -> Maybe a | |
last = List.reverse >> List.head | |
penultimate : List a -> Maybe a | |
penultimate | |
= List.reverse | |
>> (List.drop 1) | |
>> List.head | |
elementAt : List a -> Int -> Maybe a | |
elementAt xs n = | |
xs | |
|> List.drop (n-1) | |
|> List.head | |
countElements : List a -> Int | |
countElements xs = | |
case xs of | |
[] -> 0 | |
_ :: rest -> countElements rest + 1 | |
myReverse : List a -> List a | |
myReverse xs = | |
case xs of | |
[] -> [] | |
h :: tail -> myReverse tail ++ [h] | |
isPalindrome : List a -> Bool | |
isPalindrome xs = | |
xs == List.reverse xs | |
noDupes : List a -> List a | |
noDupes xs = | |
case xs of | |
x :: y :: rest -> if x == y then noDupes ([x] ++ rest) else x :: noDupes ([y] ++ rest) | |
_ -> xs | |
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
type RleCode a = Single a | Run Int a | |
removeZeroes : List (Int, Maybe a) -> List (Int, a) | |
removeZeroes xs = | |
case xs of | |
[] -> [] | |
(_, Nothing) :: rest -> removeZeroes rest | |
(c, Just v) :: rest -> (c,v) :: removeZeroes rest | |
runLengths : List (List a) -> List (Int, a) | |
runLengths xss = | |
xss | |
|> List.map (\xs -> (List.length xs, List.head xs)) | |
|> removeZeroes | |
split : List a -> Int -> (List a, List a) | |
split list count = | |
(List.take count list, List.drop count list) | |
start : a -> List a -> List a | |
start s xs = | |
case xs of | |
[] -> [] | |
x :: rest -> if x == s then [x] ++ (start x rest) else [] | |
head : List a -> List a | |
head xs = | |
case xs of | |
[] -> [] | |
[x] -> [x] | |
x :: rest -> x :: (start x rest) | |
divide : List a -> List (List a) | |
divide xs = | |
case xs of | |
[] -> [] | |
_ -> | |
let | |
h = head xs | |
l = List.length h | |
in | |
h :: divide (List.drop l xs) | |
rleEncode : List a -> List (RleCode a) | |
rleEncode list = | |
list | |
|> divide | |
|> runLengths | |
|> List.map (\(n,c) -> if n == 1 then Single c else Run n c) | |
rleDecode : List (RleCode a) -> List a | |
rleDecode xs = | |
case xs of | |
[] -> [] | |
(Single c) :: tail -> c :: rleDecode tail | |
(Run n c) :: tail -> (List.repeat n c) ++ (rleDecode tail) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment