Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.84 KB | None | 0 0
  1. let add_3 b1 b2 b3 = match b1, b2, b3 with
  2.   | true, true, true -> true, true
  3.   | true, true, false
  4.   | true, false, true
  5.   | false, true, true -> true, false
  6.   | true, false, false
  7.   | false, true, false
  8.   | false, false, true -> false, true
  9.   | false, false, false -> false, false;;
  10.  
  11. let rec add_bin n1 n2 carry = match n1, n2 with
  12.   | [], [] -> [carry]
  13.   | [], n2 -> add_bin [false] n2 carry
  14.   | n1, [] -> add_bin n1 [false] carry
  15.   | d1 :: d1s, d2 :: d2s ->
  16.     let (carry, d) = add_3 d1 d2 carry in
  17.     d :: (add_bin d1s d2s carry);;
  18.  
  19. let rec add_bin_list' xss = match xss with
  20. |[]    -> []
  21. |[h]   -> h
  22. |h1 :: h2 :: t  -> add_bin_list' (([add_bin h1 h2 false]) @ t);;
  23.  
  24. let rec cut xs = match xs with
  25. |[] -> []
  26. |x::xs -> if x = false then cut xs else List.rev (x::xs) ;;
  27.  
  28. let add_bin_list xss = add_bin_list' xss |> List.rev |> cut ;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement