![]() |
Edit |
![]() |
|
![]() |
Recent Changes |
![]() |
Subscriptions |
![]() |
Lost and Found |
![]() |
Find References |
![]() |
Rename |
| Search |
![]() |
List all versions |
Check out the formatting tips on the right for help formatting and making links.
Use the template below:
In map.ml and set.ml records are used to play the role of OCaml “structures” (with some important limitations which I won’t go into in this article). These records are just sets of functions that manipulate values of one or more related types. The records are just of type “Map.ProviderUntagged<K,V>” (for some K,V). They manipulate values of type Tagged.MapUntagged<K,V>, which are just immutable dictionaries implemented using binary trees. The functions in the record close over the comparison function used for keys where appropriate.
For example:
let IgnoreCase s1 s2 =
System.String.Compare(s1,s2,true) // "true" indicates "ignore case"
let StringIntMaps : Map.ProviderUntagged<string,int> =
Map.Make(IgnoreCase)
Now StringIntMaps is a record of function values that can be used to create and manipulate maps of type Tagged.MapUntagged<string,int>. For example:
let emptyMap = StringIntMaps.empty let map1 = StringIntMaps.add "Fred" 372 emptyMap let map2 = StringIntMaps.add "Sibylle" 374 map1 let map3 = StringIntMaps.add "fred" 376 map2 // replace the first entry
The most interesting functions in the structure are “map” and “fold”. These are polymorphic in their own right (you can see that from the “‘b.” syntax in their type). That is you can use
let res = StringIntMaps.fold (fun key data acc -> String.length key + data + acc) map2 0 do Printf.printf "result = %d\n" res
Which folds using an accumulator of type “int”. Likewise you could use fold with other accumulator types.
This use of records could also be simulated using interfaces C#. (map and fold would become generic methods within an interface, one of the corner features which took up a surprising amount of my time while writing the language specification for C# generics and implementing the support for generics in the CLR). Thus you see that records (of functions etc.), interfaces, objects and structures all overlap somewhat.
![]() |
| This site supports the new NoFollow anti-spam initiative. |
Recent Topics