Problem 10 Solution

Solution 1

  1. Use List.map to convert from List (Int, a) to List (Int, Maybe a).
  2. Use removeNothings to convert to List (Int, a).
import List exposing (map, length, head)
runLengths : List (List a) -> List ( Int, a )
runLengths xss =
    map (\xs -> ( length xs, head xs )) xss |> removeNothings


removeNothings : List ( Int, Maybe a ) -> List ( Int, a )
removeNothings xs =
    case xs of
        [] ->
            []

        ( len, Nothing ) :: rest ->
            removeNothings rest

        ( len, Just y ) :: rest ->
            ( len, y ) :: removeNothings rest

Back to Problem 10

results matching ""

    No results matching ""