From 84759bd46e7e50571ff523698e25d0d320e641a5 Mon Sep 17 00:00:00 2001 From: Luke Westby Date: Sat, 9 Apr 2016 20:15:51 -0700 Subject: [PATCH] refactor sublist --- exercises/sublist/Sublist.elm | 7 ++++++ exercises/sublist/Sublist.example | 25 ++++++++++---------- exercises/sublist/SublistTests.elm | 38 +++++++++++++++--------------- 3 files changed, 38 insertions(+), 32 deletions(-) diff --git a/exercises/sublist/Sublist.elm b/exercises/sublist/Sublist.elm index 53ce299..a536680 100644 --- a/exercises/sublist/Sublist.elm +++ b/exercises/sublist/Sublist.elm @@ -1 +1,8 @@ module Sublist (..) where + + +type ListComparison + = Equal + | Superlist + | Sublist + | Unequal diff --git a/exercises/sublist/Sublist.example b/exercises/sublist/Sublist.example index 8cea41b..1f2a36f 100644 --- a/exercises/sublist/Sublist.example +++ b/exercises/sublist/Sublist.example @@ -1,31 +1,30 @@ module Sublist (..) where -import List exposing (..) -import String + +type ListComparison + = Equal + | Superlist + | Sublist + | Unequal -sublist : List a -> List a -> String +sublist : List a -> List a -> ListComparison sublist alist blist = if alist == blist then - "Equal" + Equal else if inList alist blist then - "Superlist" + Superlist else if inList blist alist then - "Sublist" + Sublist else - "Unequal" + Unequal inList : List a -> List a -> Bool inList alist blist = let getLastInList sublist = - case (List.tail sublist) of - Just list -> - list - - Nothing -> - [] + Maybe.withDefault [] (List.tail sublist) in if (List.length alist) < (List.length blist) then False diff --git a/exercises/sublist/SublistTests.elm b/exercises/sublist/SublistTests.elm index aba6f6b..0f89598 100644 --- a/exercises/sublist/SublistTests.elm +++ b/exercises/sublist/SublistTests.elm @@ -3,7 +3,7 @@ module Main (..) where import Task import Console import ElmTest exposing (..) -import Sublist exposing (sublist) +import Sublist exposing (sublist, ListComparison(..)) tests : Test @@ -12,58 +12,58 @@ tests = "Sublist" [ test "empty equals empty" - (assertEqual "Equal" (sublist [] [])) + (assertEqual Equal (sublist [] [])) , test "empty is a sublist of anything" - (assertEqual "Sublist" (sublist [] [ 1, 2 ])) + (assertEqual Sublist (sublist [] [ 1, 2 ])) , test "anything is a superlist of empty" - (assertEqual "Superlist" (sublist [ 1, 2 ] [])) + (assertEqual Superlist (sublist [ 1, 2 ] [])) , test "1 is not 2" - (assertEqual "Unequal" (sublist [ 1 ] [ 2 ])) + (assertEqual Unequal (sublist [ 1 ] [ 2 ])) , test "compare larger equal lists" - (assertEqual "Equal" (sublist [ 1, 1, 1 ] [ 1, 1, 1 ])) + (assertEqual Equal (sublist [ 1, 1, 1 ] [ 1, 1, 1 ])) , test "sublist at start" - (assertEqual "Sublist" (sublist [ 1, 2, 3 ] [ 1, 2, 3, 4, 5 ])) + (assertEqual Sublist (sublist [ 1, 2, 3 ] [ 1, 2, 3, 4, 5 ])) , test "sublist in the middle" - (assertEqual "Sublist" (sublist [ 4, 3, 2 ] [ 5, 4, 3, 2, 1 ])) + (assertEqual Sublist (sublist [ 4, 3, 2 ] [ 5, 4, 3, 2, 1 ])) , test "sublist at end" - (assertEqual "Sublist" (sublist [ 3, 4, 5 ] [ 1, 2, 3, 4, 5 ])) + (assertEqual Sublist (sublist [ 3, 4, 5 ] [ 1, 2, 3, 4, 5 ])) , test "partially matching sublist at start" - (assertEqual "Sublist" (sublist [ 1, 1, 2 ] [ 1, 1, 1, 2 ])) + (assertEqual Sublist (sublist [ 1, 1, 2 ] [ 1, 1, 1, 2 ])) , test "sublist early in huge list" - (assertEqual "Sublist" (sublist [ 3, 4, 5 ] [1..100000])) + (assertEqual Sublist (sublist [ 3, 4, 5 ] [1..100000])) , test "huge sublist not in list" - (assertEqual "Unequal" (sublist [10..100001] [1..100000])) + (assertEqual Unequal (sublist [10..100001] [1..100000])) , test "superlist at start" - (assertEqual "Superlist" (sublist [ 1, 2, 3, 4, 5 ] [ 1, 2, 3 ])) + (assertEqual Superlist (sublist [ 1, 2, 3, 4, 5 ] [ 1, 2, 3 ])) , test "superlist in middle" - (assertEqual "Superlist" (sublist [ 5, 4, 3, 2, 1 ] [ 4, 3, 2 ])) + (assertEqual Superlist (sublist [ 5, 4, 3, 2, 1 ] [ 4, 3, 2 ])) , test "superlist at end" - (assertEqual "Superlist" (sublist [ 1, 2, 3, 4, 5 ] [ 3, 4, 5 ])) + (assertEqual Superlist (sublist [ 1, 2, 3, 4, 5 ] [ 3, 4, 5 ])) , test "partially matching superlist at start" - (assertEqual "Superlist" (sublist [ 1, 1, 1, 2 ] [ 1, 1, 2 ])) + (assertEqual Superlist (sublist [ 1, 1, 1, 2 ] [ 1, 1, 2 ])) , test "superlist early in huge list" - (assertEqual "Superlist" (sublist [1..100000] [ 3, 4, 5 ])) + (assertEqual Superlist (sublist [1..100000] [ 3, 4, 5 ])) , test "recurring values sublist" - (assertEqual "Sublist" (sublist [ 1, 2, 1, 2, 3 ] [ 1, 2, 3, 1, 2, 1, 2, 3, 2, 1 ])) + (assertEqual Sublist (sublist [ 1, 2, 1, 2, 3 ] [ 1, 2, 3, 1, 2, 1, 2, 3, 2, 1 ])) , test "recurring values unequal" - (assertEqual "Unequal" (sublist [ 1, 2, 1, 2, 3 ] [ 1, 2, 3, 1, 2, 3, 2, 3, 2, 1 ])) + (assertEqual Unequal (sublist [ 1, 2, 1, 2, 3 ] [ 1, 2, 3, 1, 2, 3, 2, 3, 2, 1 ])) ]