Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of rootulp

rootulp's solution

to Sublist in the Elm Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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 Exercism help page for Elm installation and learning resources.

Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed.

$ elm-package install --yes

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's possible to submit an incomplete solution so you can see how others have completed the exercise.

Tests.elm

module Tests exposing (..)

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


tests : Test
tests =
    describe "Sublist"
        [ test "the solution is for the correct version of the test" <|
            \() -> Expect.equal 2 version
        , 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 ])
        ]

elm-package.json

{
    "version": "3.0.0",
    "summary": "Exercism problems in Elm.",
    "repository": "https://github.com/exercism/elm.git",
    "license": "BSD3",
    "source-directories": [
        ".",
        ".."
    ],
    "exposed-modules": [],
    "dependencies": {
        "elm-lang/core": "5.0.0 <= v < 6.0.0",
        "elm-community/elm-test": "4.0.0 <= v < 5.0.0"
    },
    "elm-version": "0.18.0 <= v < 0.19.0"
}
module Sublist exposing (..)


type ListComparison
    = Equal
    | Unequal
    | Sublist
    | Superlist


sublist : List Int -> List Int -> ListComparison
sublist list1 list2 =
    if list1 == list2 then
        Equal
    else if isSublist list1 list2 then
        Sublist
    else if isSublist list2 list1 then
        Superlist
    else
        Unequal


isSublist : List Int -> List Int -> Bool
isSublist list1 list2 =
    if list1 == List.take (List.length list1) list2 then
        True
    else if List.length list1 > List.length list2 then
        False
    else
        isSublist list1 (List.drop 1 list2)


version : Int
version =
    2

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other peopleโ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?