Created
April 6, 2016 23:24
-
-
Save chuck0523/e686659155f034e6aa6e77b7d77d78a3 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
import Graphics.Element exposing (show) | |
import Graphics.Collage exposing (collage, filled, move, ngon) | |
import Color exposing (green, purple) | |
import List exposing (filter) | |
-- list length(http://elm-lang.org/examples/length) | |
-- main = | |
-- show (length [1..9]) -- 9 | |
length : List a -> Int | |
length list = | |
case list of | |
[] -> | |
0 | |
first :: rest -> | |
1 + length rest | |
-- zip(http://elm-lang.org/examples/zip) | |
-- main = | |
-- show (zip ["Tom", "Sue", "Bob"] [45, 31, 26]) -- [("Tom",45),("Sue",31),("Bob",26)] | |
zip : List a -> List b -> List (a, b) | |
zip xs xy = | |
case (xs, xy) of | |
(x :: xs', y :: ys') -> | |
(x, y) :: zip xs' ys' | |
(_, _) -> | |
[] | |
-- quick-sort(http://elm-lang.org/examples/quick-sort) | |
-- main = | |
-- show (quicksort [5, 3, 8, 1, 9, 4, 7]) -- [1,3,4,5,7,8,9] | |
quicksort : List comparable -> List comparable | |
quicksort list = | |
case list of | |
[] -> | |
[] | |
pivot :: rest -> | |
let | |
lower = filter (\n -> n <= pivot) rest | |
higher = filter (\n -> n > pivot) rest | |
in | |
quicksort lower ++ [pivot] ++ quicksort higher | |
-- merge-sort(http://elm-lang.org/examples/merge-sort) | |
main = | |
show (mergesort[5, 3, 8, 1, 9, 4, 7]) -- [1,3,4,5,7,8,9] | |
mergesort : List comparable -> List comparable | |
mergesort list = | |
case list of | |
[] -> | |
list | |
[_] -> | |
list | |
_ -> | |
let | |
(xs, ys) = split list | |
in | |
merge (mergesort xs) (mergesort ys) | |
split : List a -> (List a, List a) | |
split list = | |
case list of | |
[] -> | |
([], []) | |
x :: rest -> | |
let | |
(xs, ys) = split rest | |
in | |
(ys, x :: xs) | |
merge : List comparable -> List comparable -> List comparable | |
merge xs ys = | |
case (xs, ys) of | |
(x :: xs', y :: ys') -> | |
if x < y | |
then x :: merge xs' ys | |
else y :: merge xs ys' | |
([], rest) -> | |
rest | |
(rest, []) -> | |
rest | |
-- main = | |
-- show (isEven 2, filter isEven [1..9]) -- (True,[2,4,6,8]) | |
isEven : Int -> Bool | |
isEven int = | |
int % 2 == 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment