Skip to content

Instantly share code, notes, and snippets.

@BrianHicks
Created November 3, 2016 20:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrianHicks/988e31bd221d2164f984227ecbe1fa1e to your computer and use it in GitHub Desktop.
Save BrianHicks/988e31bd221d2164f984227ecbe1fa1e to your computer and use it in GitHub Desktop.
module Tree exposing (..)
import Html exposing (text)
import Json.Decode exposing (..)
import Json.Decode.Extra exposing (lazy)
type BST
= Branch BST Int BST
| Leaf
bst : Decoder BST
bst =
oneOf
[ null Leaf
, object3
Branch
("left" := (lazy (\_ -> bst)))
("value" := int)
("right" := (lazy (\_ -> bst)))
]
data =
"""{
"value": 2,
"left": {
"value": 1,
"left": null,
"right": null
},
"right": {
"value": 3,
"left": null,
"right": null
}
}"""
goal : BST
goal =
Branch
(Branch Leaf 1 Leaf)
2
(Branch Leaf 3 Leaf)
main =
data
|> decodeString bst
|> toString
|> text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment