![]() Similar functions can be written to look up values in a dictionary to convertĪ list of pairs to or from a tree dictionary, and so on. It is known as a binary search tree: # let rec insert ( k, v ) = function | Leaf -> Node ( Leaf, ( k, v ), Leaf ) | Node ( l, ( k', v' ), r ) -> if k k' then Node ( l, ( k', v' ), insert ( k, v ) r ) else Node ( l, ( k, v ), r ) val insert : 'a * 'b -> ( 'a * 'b ) tree -> ( 'a * 'b ) tree = Larger key, we have a data structure for dictionaries which performs better Insist that the keys are unique and that a smaller key is always left of a ![]() Instead of integers, we could build a tree of key-value pairs. Let's try our new functions out: # let all = total t val all : int = 10 # let flipped = flip t val flipped : int tree = Node ( Node ( Leaf, 4, Node ( Leaf, 3, Leaf ) ), 2, Node ( Leaf, 1, Leaf ) ) # t = flip flipped - : bool = true ![]() Here, flip is polymorphic while total operates only on trees of type int tree. Pattern matching on our new constructors: # let rec total = function | Leaf -> 0 | Node ( l, x, r ) -> total l + x + total r val total : int tree -> int = # let rec flip = function | Leaf -> Leaf | Node ( l, x, r ) -> Node ( flip r, x, flip l ) val flip : 'a tree -> 'a tree = Now we can write recursive and polymorphic functions over these trees by In our example, we built an integer tree, but any type can be A Node holds a left tree, a value of type 'a,Īnd a right tree. Notice that we give the type parameter 'a before the type name (if there is Here is an OCaml data typeįor a binary tree carrying any kind of data: # type 'a tree = | Leaf | Node of 'a tree * 'a * 'a tree type 'a tree = Leaf | Node of 'a tree * 'a * 'a tree # let t = Node ( Node ( Leaf, 1, Leaf ), 2, Node ( Node ( Leaf, 3, Leaf ), 4, Leaf ) ) val t : int tree = Node ( Node ( Leaf, 1, Leaf ), 2, Node ( Node ( Leaf, 3, Leaf ), 4, Leaf ) ) Therefore, a record definition is a set of field name and type: # type point = Example: Treesĭata types may be polymorphic as well as recursive. In records, a field has a name order is no longer relevant. In tuples, a field sits at some place: first, second, third, etc. We have records, which are like labeled tuples. Next, we have tuples, which collect a fixed number of elements together: # ( 5.0, 6.5 ) - : float * float = ( 5. Number of elements sharing the same type: # - : 'a list = # - : int list = # ] - : int list list = ] # - : bool list = First, we have lists, which are ordered collections of any Let's recap the built-in compound data types we can use in OCaml toĬombine such values. ![]() We have already seen simple data types such as int, float, string, andīool. Unless these examples start with a # toplevel prompt andĮnd with, it isn't an expression to evaluate but rather an example of code structure. When in the ocaml or utop toplevel, the command prompt appears as a #, as shownĪlso remember that an expression must end with įor OCaml to evaluate it. Whereas # denoted a comment and $ the command prompt in the Please note throughout this tutorial the code is written in the ocaml toplevel. In this tutorial, we learn how to build our own types in OCaml and to write
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |