Skip to content

Instantly share code, notes, and snippets.

@TwistingTwists
Created March 22, 2019 09:05
Show Gist options
  • Save TwistingTwists/592a31b04f8e67728e27e159b2115c6f to your computer and use it in GitHub Desktop.
Save TwistingTwists/592a31b04f8e67728e27e159b2115c6f to your computer and use it in GitHub Desktop.
recursive nodes in Elm
module Main exposing (main)
import Browser
import Html exposing (..)
import Html.Events exposing (onClick)
-- The node value and a list of child nodes
type Node a
= Node a (List (Node a))
type alias Model =
{ root : Node Int }
initTree : Node Int
initTree =
Node -22
[ Node 42 []
, Node 100 []
, Node 43
[ Node 87 []
, Node 88
[ Node 77 []
, Node 69 []
]
, Node 33 []
]
, Node 10 []
]
initialModel : Model
initialModel =
{ root = initTree
}
viewNode : Node Int -> Html msg
viewNode (Node value nodes) =
li []
(List.append
[ text (String.fromInt value) ]
[ ul [] (List.map viewNode nodes) ]
)
view : Model -> Html Msg
view model =
ul [] [ viewNode model.root ]
type Msg
= NoOp
update : Msg -> Model -> Model
update msg model =
case msg of
NoOp ->
model
main : Program () Model Msg
main =
Browser.sandbox
{ init = initialModel
, view = view
, update = update
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment