Problem 4

Elm provides the function List.length. See if you can implement it yourself.

Example

countElements [1, 2, 3, 4, 3, 2, 1] == 7

Unit Test

import Html
import List
import Maybe


countElements : List a -> Int
countElements xs =
    -- your implementation here
    0


main : Html.Html a
main =
    Html.text
        <| case test of
            0 ->
                "Your implementation passed all tests."

            1 ->
                "Your implementation failed one test."

            x ->
                "Your implementation failed " ++ (toString x) ++ " tests."


test : Int
test =
    List.length
        <| List.filter ((==) False)
            [ countElements (List.range 1 4000) == 4000
            , countElements [ 1 ] == 1
            , countElements [] == 0
            , countElements [ 'a', 'b', 'c' ] == 3
            ]

Hints

  1. Use a fold.
  2. Elm has a specialize fold, List.sum. How can you transform the list such that List.sum gives you the answer?

Solutions

Solutions

results matching ""

    No results matching ""