< 99 Elm Problems

Flatten a nested list structure.

We have to define a new data type, because lists in Elm are homogeneous.

import Html exposing (text)
import List

type NestedList a
    = Elem a
    | NestedList (List (NestedList a))

flatten : NestedList a -> List a
-- your implementation goes here

main = 
  text <| toString <|
    flatten (NestedList [Elem 1, NestedList [Elem 2, NestedList [Elem 3, Elem 4], Elem 5]])

Result:

[1, 2, 3, 4, 5]

Solutions

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.