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).
runLengths : List (List a) -> List ( Int, a )
runLengths xss =
    List.map (\xs -> ( List.length xs, List.head xs )) xss |> removeNothings


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

        ( c, Nothing ) :: ys ->
            removeNothings ys

        ( c, Just y ) :: ys ->
            ( c, y ) :: removeNothings ys

Back to Problem 10

results matching ""

    No results matching ""