From 44772d7e3355a8c98a00eecd5bba0447937deb77 Mon Sep 17 00:00:00 2001 From: darkf Date: Sat, 26 Oct 2013 23:22:56 -0700 Subject: [PATCH] add association list example --- examples/association_list.lamb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 examples/association_list.lamb diff --git a/examples/association_list.lamb b/examples/association_list.lamb new file mode 100644 index 0000000..d0cc889 --- /dev/null +++ b/examples/association_list.lamb @@ -0,0 +1,25 @@ +-- insert a pair into a map +map_insert(assoc, pair) -> pair :: assoc. + +-- lookup by key +map_lookup([], _) -> ("nothing"). +map_lookup((k,v)::xs, key) -> + if k == key then ("just", v) + else map_lookup(xs, key). + +-- remove a key from a map +map_remove([], key) -> []. +map_remove((k,v)::xs, key) -> + if k == key then xs + else (k,v) :: map_remove(xs, key). + +m = []. +m = map_insert(m, ("hi", "there")). +m = map_insert(m, ("k", "v")). +m = map_insert(m, ("ready", "go")). + +print(m). +print(map_remove(m, "k")). +("just", x) = map_lookup(m, "hi"). +("nothing") = map_lookup(m, "foo"). +print(x). \ No newline at end of file