elm/exercises/sublist/Sublist.example

40 lines
680 B
Text
Raw Normal View History

module Sublist exposing (..)
2015-10-12 16:46:15 +00:00
version : Int
version =
2
2016-04-10 03:15:51 +00:00
type ListComparison
= Equal
| Superlist
| Sublist
| Unequal
2016-04-10 03:15:51 +00:00
sublist : List a -> List a -> ListComparison
2015-10-12 16:46:15 +00:00
sublist alist blist =
if alist == blist then
2016-04-10 03:15:51 +00:00
Equal
else if inList alist blist then
2016-04-10 03:15:51 +00:00
Superlist
else if inList blist alist then
2016-04-10 03:15:51 +00:00
Sublist
else
2016-04-10 03:15:51 +00:00
Unequal
2015-10-12 16:46:15 +00:00
inList : List a -> List a -> Bool
inList alist blist =
let
getLastInList sublist =
2016-04-10 03:15:51 +00:00
Maybe.withDefault [] (List.tail sublist)
2015-10-12 16:46:15 +00:00
in
if (List.length alist) < (List.length blist) then
False
else if (List.take (List.length blist) alist) == blist then
True
else
inList (getLastInList alist) blist