- 论坛徽章:
- 1
|
这是Cornell Ocaml course的一个function dictionary,谁能给解释一下该怎么用吗?我怎么感觉那个make定义的不对是不
dictionary
- module FunctionDict : DICTIONARY =
- struct
- type key = string
- type 'a dict = string -> 'a
- (* The function f represents the mapping in which x is mapped to
- * (f x), except for x that are not in the mapping, in which case
- * f raises NotFound.
- *)
- exception NotFound
- let make () = fun _ -> raise NotFound
- let lookup (d : 'a dict) (key : string) : 'a = d key
- let insert (d : 'a dict) (k : key) (x : 'a) : 'a dict =
- fun k' -> if k = k' then x else d k'
- let map (f : 'a -> 'b) (d : 'a dict) = fun k -> f (d k)
- end
复制代码
|
|