add association list example

This commit is contained in:
darkf 2013-10-26 23:22:56 -07:00
parent 37f2322783
commit 44772d7e33
1 changed files with 25 additions and 0 deletions

View File

@ -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).