 # paulfioravanti's solution

## to Sublist in the Elm Track

Published at Jul 25 2019 · 0 comments
Instructions
Test suite
Solution

Given two lists determine if the first list is contained within the second list, if the second list is contained within the first list, if both lists are contained within each other or if none of these are true.

Specifically, a list A is a sublist of list B if by dropping 0 or more elements from the front of B and 0 or more elements from the back of B you get a list that's completely equal to A.

Examples:

• A = [1, 2, 3], B = [1, 2, 3, 4, 5], A is a sublist of B
• A = [3, 4, 5], B = [1, 2, 3, 4, 5], A is a sublist of B
• A = [3, 4], B = [1, 2, 3, 4, 5], A is a sublist of B
• A = [1, 2, 3], B = [1, 2, 3], A is equal to B
• A = [1, 2, 3, 4, 5], B = [2, 3, 4], A is a superlist of B
• A = [1, 2, 4], B = [1, 2, 3, 4, 5], A is not a superlist of, sublist of or equal to B

## Elm Installation

Refer to the Installing Elm page for information about installing elm.

## Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

``````\$ elm-test
``````

Automatically run tests again when you save changes:

``````\$ elm-test --watch
``````

As you work your way through the test suite, be sure to remove the `skip <|` calls from each test until you get them all passing!

## Submitting Incomplete Solutions

It is possible to submit an incomplete solution so you can see how others have completed the exercise.

### Tests.elm

``````module Tests exposing (tests)

import Expect
import Sublist exposing (ListComparison(..), sublist)
import Test exposing (..)

tests : Test
tests =
describe "Sublist"
[ test "empty equals empty" <|
\() -> Expect.equal Equal (sublist [] [])
, skip <|
test "empty is a sublist of anything" <|
\() -> Expect.equal Sublist (sublist [] [ 1, 2 ])
, skip <|
test "anything is a superlist of empty" <|
\() -> Expect.equal Superlist (sublist [ 1, 2 ] [])
, skip <|
test "1 is not 2" <|
\() -> Expect.equal Unequal (sublist [ 1 ] [ 2 ])
, skip <|
test "compare larger equal lists" <|
\() -> Expect.equal Equal (sublist [ 1, 1, 1 ] [ 1, 1, 1 ])
, skip <|
test "sublist at start" <|
\() -> Expect.equal Sublist (sublist [ 1, 2, 3 ] [ 1, 2, 3, 4, 5 ])
, skip <|
test "sublist in the middle" <|
\() -> Expect.equal Sublist (sublist [ 4, 3, 2 ] [ 5, 4, 3, 2, 1 ])
, skip <|
test "sublist at end" <|
\() -> Expect.equal Sublist (sublist [ 3, 4, 5 ] [ 1, 2, 3, 4, 5 ])
, skip <|
test "partially matching sublist at start" <|
\() -> Expect.equal Sublist (sublist [ 1, 1, 2 ] [ 1, 1, 1, 2 ])
, skip <|
test "sublist early in huge list" <|
\() -> Expect.equal Sublist (sublist [ 3, 4, 5 ] (List.range 1 100000))
, skip <|
test "huge sublist not in list" <|
\() -> Expect.equal Unequal (sublist (List.range 10 5001) (List.range 1 5000))
, skip <|
test "superlist at start" <|
\() -> Expect.equal Superlist (sublist [ 1, 2, 3, 4, 5 ] [ 1, 2, 3 ])
, skip <|
test "superlist in middle" <|
\() -> Expect.equal Superlist (sublist [ 5, 4, 3, 2, 1 ] [ 4, 3, 2 ])
, skip <|
test "superlist at end" <|
\() -> Expect.equal Superlist (sublist [ 1, 2, 3, 4, 5 ] [ 3, 4, 5 ])
, skip <|
test "partially matching superlist at start" <|
\() -> Expect.equal Superlist (sublist [ 1, 1, 1, 2 ] [ 1, 1, 2 ])
, skip <|
test "superlist early in huge list" <|
\() -> Expect.equal Superlist (sublist (List.range 1 100000) [ 3, 4, 5 ])
, skip <|
test "recurring values sublist" <|
\() -> Expect.equal Sublist (sublist [ 1, 2, 1, 2, 3 ] [ 1, 2, 3, 1, 2, 1, 2, 3, 2, 1 ])
, skip <|
test "recurring values unequal" <|
\() -> Expect.equal Unequal (sublist [ 1, 2, 1, 2, 3 ] [ 1, 2, 3, 1, 2, 3, 2, 3, 2, 1 ])
]``````
``````module Sublist exposing (ListComparison(..), sublist)

type ListComparison
= Equal
| Superlist
| Sublist
| Unequal

sublist : List a -> List a -> ListComparison
sublist alist blist =
if alist == blist then
Equal

else if alist |> isContainedIn blist then
Sublist

else if blist |> isContainedIn alist then
Superlist

else
Unequal

-- PRIVATE

isContainedIn : List a -> List a -> Bool
isContainedIn xlist ylist =
if ylist |> isShorter xlist then
False

else if ylist |> isSublist xlist then
True

else
let
xtail =
List.drop 1 xlist
in
isContainedIn xtail ylist

isShorter : List a -> List a -> Bool
isShorter xlist ylist =
List.length xlist < List.length ylist

isSublist : List a -> List a -> Bool
isSublist xlist ylist =
let
ylength =
List.length ylist
in
xlist
|> List.take ylength
|> (==) ylist``````