Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type 'a tree = Br of 'a * 'a tree * 'a tree | Lf
- let a_tree =
- Br (
- (3, "three"),
- Br (
- (1, "one"), Lf, Br ((2, "two"), Lf, Lf)
- ),
- Br ((4, "four"), Lf, Lf)
- )
- let rec cata f_lf f_br t =
- match t with
- | Lf -> f_lf ()
- | Br (x, l, r) ->
- f_br x (cata f_lf f_br l) (cata f_lf f_br r)
- let fold_right op tree init = cata
- (fun unit -> init)
- (fun value l r -> op value l r)
- tree
- let size (t : 'a tree) : int = fold_right (fun x y z -> x+y+z) t 0
- let count = size a_tree
- (* This expression has type (int * string) tree
- but an expression was expected of type int tree
- Type int * string is not compatible with type int *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement