Skip to content

Instantly share code, notes, and snippets.

@leque
Created May 3, 2021 01:37
Show Gist options
  • Save leque/f49aea50032a846a63017bd071a3a4a1 to your computer and use it in GitHub Desktop.
Save leque/f49aea50032a846a63017bd071a3a4a1 to your computer and use it in GitHub Desktop.
smlsharp: read a json-represented list
{
"type": "cons",
"head": 1,
"tail": {
"type": "cons",
"head": 42,
"tail": {
"type": "nil"
}
}
}
(*
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