Skip to content

Instantly share code, notes, and snippets.

@chrisamaphone
Created April 2, 2015 15:35
Show Gist options
  • Save chrisamaphone/c2c43150028c434d2821 to your computer and use it in GitHub Desktop.
Save chrisamaphone/c2c43150028c434d2821 to your computer and use it in GitHub Desktop.
continuation-passing function that returns the value of a key in a table as a (key,value) list, and also the table with that key removed, if it exists.
(* lookupSplit : ''a -> (''a * 'b) list -> ('b * (''a * 'b) list) option *)
fun lookupSplit id table =
let
fun lookupSplit' id nil cont = NONE
| lookupSplit' id ((k,v)::table) cont =
if k = id then SOME (v, cont table)
else lookupSplit' id table (fn suffix => cont ((k,v)::suffix))
in
lookupSplit' id table (fn x => x)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment