Advertisement
Guest User

Untitled

a guest
Mar 13th, 2014
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.14 KB | None | 0 0
  1. type Tree =
  2.      | Empty
  3.      | Node of Tree * int * Tree
  4.  
  5. // printTree : Tree -> int
  6. let rec printTree t =
  7.     match t with
  8.     | Empty -> printf "nul"; 0
  9.     | Node (l, v, r) -> printf "%A (" v
  10.                         let lc = printTree l
  11.                         printf ", "
  12.                         let rc = printTree r
  13.                         printf ")"
  14.                         lc + rc + 1
  15.  
  16. printTree (Node (Empty, 1, Empty)) |> ignore
  17.  
  18. // printTree'cps : Tree -> (int -> 'a) -> 'a
  19. let rec printTree'cps t k =
  20.    match t with
  21.    | Empty -> k (printf "nul"; 0)
  22.    | Node (l, v, r) -> printf "%A (" v
  23.                        printTree'cps l
  24.                           (fun lc ->
  25.                              printf ", "
  26.                              printTree'cps r
  27.                                (fun rc ->
  28.                                   printf ")"
  29.                                   k (lc + rc + 1)
  30.                                )                                          
  31.                          )
  32.  
  33. printTree'cps (Node (Node (Empty, 2, Empty), 1, (Node (Empty, 3, Empty)))) (fun cnt -> printfn "\ncount = %A" cnt)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement