SHARE
TWEET

OCaml Binary Tree Catamorphism

a guest Jun 21st, 2019 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. type 'a tree = Br of 'a * 'a tree * 'a tree | Lf    
  2.  
  3. let rec cata map_lf map_br t =
  4.     match t with
  5.     | Lf -> map_lf ()
  6.     | Br (x, l, r) ->
  7.         let reduced = cata map_lf map_br (Br (x, l, r)) in
  8.         map_br reduced
  9.        
  10. let fold_right op tree init = cata
  11.     (fun unit -> init)
  12.     (fun value -> op value)
  13.     tree
  14.  
  15. let size t = fold_right (fun x -> 1 + x) t 0
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top