Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Recursive addition in F# using
- let rec plus x y =
- match y with
- | 0 -> x;
- | succ(y) -> succ( plus(x y) );
- let rec plus x y =
- match y with
- | 0 -> x
- | y -> 1 + (plus x (y - 1))
- let (|Zero|Succ|) n =
- if n < 0 then failwith "Unexpected!"
- if n = 0 then Zero else Succ(n - 1)
- let rec plus x y =
- match y with
- | Zero -> x
- | Succ y -> 1 + (plus x y)
- type Number =
- | Zero
- | Succ of Number
- let rec plus x y =
- match y with
- | Zero -> x
- | Succ(y1) -> Succ (plus x y1)
- let rec (+) x y =
- match y with
- | Zero -> x
- | Succ(y1) -> Succ (x + y1)
- type N = Zero | Succ of N
- let rec NtoInt n =
- match n with
- | Zero -> 0
- | Succ x -> 1 + NtoInt x
- let rec plus x y =
- match x with
- | Zero -> y
- | Succ n -> Succ (plus n y)
- > plus (Succ (Succ Zero)) Zero |> NtoInt ;;
- val it : int = 2
- > plus (Succ (Succ Zero)) (Succ Zero) |> NtoInt ;;
- val it : int = 3
- let rec plus x y =
- match y with
- | 0 -> x
- | _ -> plus (x+1) (y-1)
Add Comment
Please, Sign In to add comment