Different ways/notations to add two integers

I tried to write different ways to add two integers.
Here is the code,

type tuplexy=int *int
type recordxy={ x:int;y:int}
type intlist= int list
let _=
    (* Two arguments , ie curried, can be partially applied*)
    let adda:int->(int->int) = fun x-> (fun y -> (x+y)) in
    let addb:int->(int->int) = fun x y -> x+y in
    let addc (x:int) (y:int):int = x+y in

    (* One tuple , ie uncurried *)
    let adde (xy:tuplexy):int = fst xy + snd xy in 
    (* One tuple pattern , ie uncurried *)
    let addd ((x:int), (y:int)):int = x+y in


    (*record*)
    let addf (xy:recordxy):int = match xy with
        | {x;y} -> x + y in 

    (*intlist*)
    let addg (alist:intlist):int=match alist with
        | x::y::[] -> x+y
        | _ -> 0 in

If you know other ways/notations feel free to add.

We can define Integers as Peano notation Naturals with sign and then handle negative numbers (don’t do it in production):

open Core

type nat =
  | Z
  | S of nat

let rec int_to_signnat = function
  | 0 -> `Pos, Z
  | n when n < 0 -> `Neg, S (snd @@ int_to_signnat (n + 1))
  | n -> `Pos, S (snd @@ int_to_signnat (n - 1))
;;

let rec signnat_to_int = function
  | _, Z -> 0
  | `Neg, S n -> -1 + signnat_to_int (`Neg, n)
  | `Pos, S n -> 1 + signnat_to_int (`Pos, n)
;;

let rec add_nat b = function
  | Z -> b
  | S n -> S (add_nat n b)
;;

let rec add_signnat a b =
  match a, b with
  | (`Pos, an), (`Pos, bn) -> `Pos, add_nat an bn
  | (`Neg, _), (`Pos, _) -> add_signnat b a
  | (`Pos, Z), (`Neg, _) -> b
  | (`Pos, _), (_, Z) -> a
  | (`Pos, S an), (`Neg, S bn) -> add_signnat (`Pos, an) (`Neg, bn)
  | (`Neg, an), (`Neg, bn) -> `Neg, add_nat an bn
;;

let add a b = add_signnat (int_to_signnat a) (int_to_signnat b) |> signnat_to_int
let lst = [ -5; -4; -3; -2; -1; 0; 1; 2; 3; 4; 5 ]

let%expect_test "add_signnat" =
  lst
  |> List.cartesian_product lst
  |> List.iter ~f:(fun (a, b) -> printf "%d + %d = %d\n" a b (add a b));
  [%expect
    {|
    -5 + -5 = -10
    -5 + -4 = -9
    -5 + -3 = -8
    -5 + -2 = -7
    -5 + -1 = -6
    -5 + 0 = -5
    -5 + 1 = -4
    -5 + 2 = -3
    -5 + 3 = -2
    -5 + 4 = -1
    -5 + 5 = 0
    -4 + -5 = -9
    -4 + -4 = -8
    -4 + -3 = -7
    -4 + -2 = -6
    -4 + -1 = -5
    -4 + 0 = -4
    -4 + 1 = -3
    -4 + 2 = -2
    -4 + 3 = -1
    -4 + 4 = 0
    -4 + 5 = 1
    -3 + -5 = -8
    -3 + -4 = -7
    -3 + -3 = -6
    -3 + -2 = -5
    -3 + -1 = -4
    -3 + 0 = -3
    -3 + 1 = -2
    -3 + 2 = -1
    -3 + 3 = 0
    -3 + 4 = 1
    -3 + 5 = 2
    -2 + -5 = -7
    -2 + -4 = -6
    -2 + -3 = -5
    -2 + -2 = -4
    -2 + -1 = -3
    -2 + 0 = -2
    -2 + 1 = -1
    -2 + 2 = 0
    -2 + 3 = 1
    -2 + 4 = 2
    -2 + 5 = 3
    -1 + -5 = -6
    -1 + -4 = -5
    -1 + -3 = -4
    -1 + -2 = -3
    -1 + -1 = -2
    -1 + 0 = -1
    -1 + 1 = 0
    -1 + 2 = 1
    -1 + 3 = 2
    -1 + 4 = 3
    -1 + 5 = 4
    0 + -5 = -5
    0 + -4 = -4
    0 + -3 = -3
    0 + -2 = -2
    0 + -1 = -1
    0 + 0 = 0
    0 + 1 = 1
    0 + 2 = 2
    0 + 3 = 3
    0 + 4 = 4
    0 + 5 = 5
    1 + -5 = -4
    1 + -4 = -3
    1 + -3 = -2
    1 + -2 = -1
    1 + -1 = 0
    1 + 0 = 1
    1 + 1 = 2
    1 + 2 = 3
    1 + 3 = 4
    1 + 4 = 5
    1 + 5 = 6
    2 + -5 = -3
    2 + -4 = -2
    2 + -3 = -1
    2 + -2 = 0
    2 + -1 = 1
    2 + 0 = 2
    2 + 1 = 3
    2 + 2 = 4
    2 + 3 = 5
    2 + 4 = 6
    2 + 5 = 7
    3 + -5 = -2
    3 + -4 = -1
    3 + -3 = 0
    3 + -2 = 1
    3 + -1 = 2
    3 + 0 = 3
    3 + 1 = 4
    3 + 2 = 5
    3 + 3 = 6
    3 + 4 = 7
    3 + 5 = 8
    4 + -5 = -1
    4 + -4 = 0
    4 + -3 = 1
    4 + -2 = 2
    4 + -1 = 3
    4 + 0 = 4
    4 + 1 = 5
    4 + 2 = 6
    4 + 3 = 7
    4 + 4 = 8
    4 + 5 = 9
    5 + -5 = 0
    5 + -4 = 1
    5 + -3 = 2
    5 + -2 = 3
    5 + -1 = 4
    5 + 0 = 5
    5 + 1 = 6
    5 + 2 = 7
    5 + 3 = 8
    5 + 4 = 9
    5 + 5 = 10 |}]
;;
let addh = function (x, y) -> (x + y)
let addi = ( + )

(* will raise a warning but works if the array length is 2 *)
let addj [| x ; y |] = x + y ;;
1 Like

Indeed,

type tuplexy=int *int
type intarray=int array
let _=
    let addh (xy:tuplexy):int = match xy with
        | (x,y) -> x+y in 
    let addj(ar:intarray):int = match ar with
        | [|x;y|] -> x+y 
        | _ -> 0 
# let add s =
    let x, y = Scanf.sscanf s "%d %d" (fun x y -> x, y) in
    (x + y) ;;
val add : string -> int = <fun>
# add "3 4";;
- : int = 7
let add = function [| x; y |] -> x + y | _ -> 0
1 Like