Merge pull request #71 from jcshih/list-ops

Add list-ops exercise
This commit is contained in:
Erik Simmler 2016-04-19 20:58:00 -04:00
commit 91fa86a077
6 changed files with 148 additions and 2 deletions

View file

@ -26,7 +26,8 @@
"phone-number", "phone-number",
"grade-school", "grade-school",
"allergies", "allergies",
"robot-simulator" "robot-simulator",
"list-ops"
], ],
"deprecated": [ "deprecated": [

View file

@ -27,7 +27,8 @@
"./exercises/phone-number", "./exercises/phone-number",
"./exercises/grade-school", "./exercises/grade-school",
"./exercises/allergies", "./exercises/allergies",
"./exercises/robot-simulator" "./exercises/robot-simulator",
"./exercises/list-ops"
], ],
"exposed-modules": [], "exposed-modules": [],
"dependencies": { "dependencies": {

View file

@ -0,0 +1 @@
module ListOps (..) where

View file

@ -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

View file

@ -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..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
"foldl"
[ 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)

View file

@ -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"
}