Guest User

Untitled

a guest
Aug 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. Recursive addition in F# using
  2. let rec plus x y =
  3. match y with
  4. | 0 -> x;
  5. | succ(y) -> succ( plus(x y) );
  6.  
  7. let rec plus x y =
  8. match y with
  9. | 0 -> x
  10. | y -> 1 + (plus x (y - 1))
  11.  
  12. let (|Zero|Succ|) n =
  13. if n < 0 then failwith "Unexpected!"
  14. if n = 0 then Zero else Succ(n - 1)
  15.  
  16. let rec plus x y =
  17. match y with
  18. | Zero -> x
  19. | Succ y -> 1 + (plus x y)
  20.  
  21. type Number =
  22. | Zero
  23. | Succ of Number
  24.  
  25. let rec plus x y =
  26. match y with
  27. | Zero -> x
  28. | Succ(y1) -> Succ (plus x y1)
  29.  
  30. let rec (+) x y =
  31. match y with
  32. | Zero -> x
  33. | Succ(y1) -> Succ (x + y1)
  34.  
  35. type N = Zero | Succ of N
  36.  
  37. let rec NtoInt n =
  38. match n with
  39. | Zero -> 0
  40. | Succ x -> 1 + NtoInt x
  41.  
  42. let rec plus x y =
  43. match x with
  44. | Zero -> y
  45. | Succ n -> Succ (plus n y)
  46.  
  47. > plus (Succ (Succ Zero)) Zero |> NtoInt ;;
  48. val it : int = 2
  49. > plus (Succ (Succ Zero)) (Succ Zero) |> NtoInt ;;
  50. val it : int = 3
  51.  
  52. let rec plus x y =
  53. match y with
  54. | 0 -> x
  55. | _ -> plus (x+1) (y-1)
Add Comment
Please, Sign In to add comment