(* * This file contains definitions for a simple Lisp-like alist and associated * functions. What's simple is that alist items are restricted to be * string*int tuples, which means that only int values can be stored in the * alist. The example lispval.ml defines a more general form of Lisp data * value. The alist definition in this file can be combined with the general * value definition to build alists that fully represent Lisp data and * bindings. *) type binding = string * int; type alist = binding list; val nilAlist = ("", 0):binding; fun bindingKey(b:binding):string = #1(b); fun bindingVal(b:binding):int = #2(b); fun assoc(key:string, al:alist):binding = if al = nil then nilAlist else if key = bindingKey(hd(al)) then hd(al) else assoc(key, tl(al)); (* * Simple tests of the alist functions. *) val a = [("x",10),("y",20),("z",30)]; bindingVal(assoc("x", a)); bindingVal(assoc("z", a)); bindingVal(assoc("w", a));