From 43aeba836af14d3a75b411f02af75fdcec6b40b4 Mon Sep 17 00:00:00 2001 From: Jonathan Shih Date: Mon, 18 Apr 2016 05:17:26 -0700 Subject: [PATCH 1/2] Add list-ops exercise --- config.json | 3 +- elm-package.json | 3 +- exercises/list-ops/ListOps.elm | 1 + exercises/list-ops/ListOps.example | 59 +++++++++++++++++++++++++ exercises/list-ops/ListOpsTests.elm | 68 +++++++++++++++++++++++++++++ exercises/list-ops/elm-package.json | 16 +++++++ 6 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 exercises/list-ops/ListOps.elm create mode 100644 exercises/list-ops/ListOps.example create mode 100644 exercises/list-ops/ListOpsTests.elm create mode 100644 exercises/list-ops/elm-package.json diff --git a/config.json b/config.json index 41414a8..ff99925 100644 --- a/config.json +++ b/config.json @@ -26,7 +26,8 @@ "phone-number", "grade-school", "allergies", - "robot-simulator" + "robot-simulator", + "list-ops" ], "deprecated": [ diff --git a/elm-package.json b/elm-package.json index 8f0d4bf..cc8ddd3 100644 --- a/elm-package.json +++ b/elm-package.json @@ -27,7 +27,8 @@ "./exercises/phone-number", "./exercises/grade-school", "./exercises/allergies", - "./exercises/robot-simulator" + "./exercises/robot-simulator", + "./exercises/list-ops" ], "exposed-modules": [], "dependencies": { diff --git a/exercises/list-ops/ListOps.elm b/exercises/list-ops/ListOps.elm new file mode 100644 index 0000000..4adeaff --- /dev/null +++ b/exercises/list-ops/ListOps.elm @@ -0,0 +1 @@ +module ListOps (..) where diff --git a/exercises/list-ops/ListOps.example b/exercises/list-ops/ListOps.example new file mode 100644 index 0000000..a2f1514 --- /dev/null +++ b/exercises/list-ops/ListOps.example @@ -0,0 +1,59 @@ +module ListOps (..) where + + +length : List a -> Int +length list = + foldl (\_ acc -> 1 + acc) 0 list + + +reverse : List a -> List a +reverse list = + foldl (::) [] list + + +foldl : (a -> b -> b) -> b -> List a -> b +foldl f acc list = + case list of + [] -> + acc + + head :: tail -> + foldl f (f head acc) tail + + +foldr : (a -> b -> b) -> b -> List a -> b +foldr f acc list = + case list of + [] -> + acc + + head :: tail -> + f head (foldr f acc tail) + + +map : (a -> b) -> List a -> List b +map f list = + foldr (\x acc -> f x :: acc) [] list + + +filter : (a -> Bool) -> List a -> List a +filter f list = + foldr + (\x acc -> + if f x then + x :: acc + else + acc + ) + [] + list + + +append : List a -> List a -> List a +append xs ys = + foldr (::) ys xs + + +concat : List (List a) -> List a +concat list = + foldr append [] list diff --git a/exercises/list-ops/ListOpsTests.elm b/exercises/list-ops/ListOpsTests.elm new file mode 100644 index 0000000..6338ae1 --- /dev/null +++ b/exercises/list-ops/ListOpsTests.elm @@ -0,0 +1,68 @@ +module Main (..) where + +import Task +import Console +import ElmTest exposing (..) +import ListOps exposing (..) + + +tests : Test +tests = + suite + "List Ops" + [ suite + "length" + [ test "empty list" (assertEqual 0 (length [])) + , test "non-empty list" (assertEqual 4 (length [1..4])) + ] + , suite + "reverse" + [ test "empty list" (assertEqual [] (reverse [])) + , test "non-empty list" (assertEqual [ 4, 3, 2, 1 ] (reverse [1..4])) + ] + , suite + "map" + [ test "empty list" (assertEqual [] (map ((+) 1) [])) + , test "non-empty list" (assertEqual [ 2, 3, 4, 5 ] (map ((+) 1) [1..4])) + ] + , suite + "filter" + [ test "empty list" (assertEqual [] (filter (\_ -> True) [])) + , test + "non-empty list" + (assertEqual [ 2, 4 ] (filter (\x -> x % 2 == 0) [1..4])) + ] + , suite + "foldr" + [ test "empty list" (assertEqual 0 (foldl (+) 0 [])) + , test "non-empty list" (assertEqual 10 (foldl (+) 0 [1..4])) + ] + , suite + "foldr" + [ test "empty list" (assertEqual 0 (foldr (+) 0 [])) + , test "non-empty list" (assertEqual 10 (foldr (+) 0 [1..4])) + ] + , suite + "append" + [ test "empty lists" (assertEqual [] (append [] [])) + , test + "empty and non-empty lists" + (assertEqual [1..4] (append [] [1..4])) + , test + "non-empty and empty lists" + (assertEqual [1..4] (append [1..4] [])) + , test "non-empty lists" (assertEqual [1..8] (append [1..4] [5..8])) + ] + , suite + "concat" + [ test "empty list" (assertEqual [] (concat [])) + , test + "list of lists" + (assertEqual [1..10] (concat [ [1..3], [], [4..7], [8..10] ])) + ] + ] + + +port runner : Signal (Task.Task x ()) +port runner = + Console.run (consoleRunner tests) diff --git a/exercises/list-ops/elm-package.json b/exercises/list-ops/elm-package.json new file mode 100644 index 0000000..d93a035 --- /dev/null +++ b/exercises/list-ops/elm-package.json @@ -0,0 +1,16 @@ +{ + "version": "1.0.0", + "summary": "Exercism problems in Elm.", + "repository": "https://github.com/exercism/xelm.git", + "license": "BSD3", + "source-directories": [ + "." + ], + "exposed-modules": [], + "dependencies": { + "deadfoxygrandpa/elm-test": "3.0.1 <= v < 4.0.0", + "elm-lang/core": "2.0.0 <= v < 4.0.0", + "laszlopandy/elm-console": "1.1.0 <= v < 2.0.0" + }, + "elm-version": "0.15.0 <= v < 0.17.0" +} From 74eeb7160e7c44e4dd82daae71daf7df639eec57 Mon Sep 17 00:00:00 2001 From: Jonathan Shih Date: Mon, 18 Apr 2016 06:52:26 -0700 Subject: [PATCH 2/2] Fix typo --- exercises/list-ops/ListOpsTests.elm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/list-ops/ListOpsTests.elm b/exercises/list-ops/ListOpsTests.elm index 6338ae1..129a74d 100644 --- a/exercises/list-ops/ListOpsTests.elm +++ b/exercises/list-ops/ListOpsTests.elm @@ -23,7 +23,7 @@ tests = , suite "map" [ test "empty list" (assertEqual [] (map ((+) 1) [])) - , test "non-empty list" (assertEqual [ 2, 3, 4, 5 ] (map ((+) 1) [1..4])) + , test "non-empty list" (assertEqual [2..5] (map ((+) 1) [1..4])) ] , suite "filter" @@ -33,7 +33,7 @@ tests = (assertEqual [ 2, 4 ] (filter (\x -> x % 2 == 0) [1..4])) ] , suite - "foldr" + "foldl" [ test "empty list" (assertEqual 0 (foldl (+) 0 [])) , test "non-empty list" (assertEqual 10 (foldl (+) 0 [1..4])) ]