Created
May 3, 2021 01:37
-
-
Save leque/f49aea50032a846a63017bd071a3a4a1 to your computer and use it in GitHub Desktop.
smlsharp: read a json-represented list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"type": "cons", | |
"head": 1, | |
"tail": { | |
"type": "cons", | |
"head": 42, | |
"tail": { | |
"type": "nil" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
type Dlist = { type: 'nil' } | { type: 'cons', head: number, tail: Dlist } | |
*) | |
type dlist = { "type": string } | |
type dcons = { "type": string, head: int, tail: dlist Dynamic.dyn } | |
val j = Dynamic.fromJsonFile "list.json"; | |
val L = _dynamic j as dlist Dynamic.dyn; | |
fun dlistToList (x : dlist Dynamic.dyn) = | |
case #"type" (Dynamic.view x) of | |
"nil" => [] | |
| "cons" => | |
let | |
val xs = _dynamic x as dcons | |
in | |
#head xs :: dlistToList (#tail xs) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment