'Ocaml Custom Data types

type expr =
    | Plus of expr * expr        (* a + b *)
    | Minus of expr * expr       (* a - b *)
    | Times of expr * expr       (* a * b *)
    | Divide of expr * expr      (* a / b *)
    | Var of string              (* "x", "y", etc. *) 

Having this type "expr" I wanted to know how can I work with a customizable number of variables depending on my needs, in this case, we know that Plus is (Expr * Expr) but what if I want to do to do : ( a + b + c) or (a * b * c), is it possible? I got this example at https://ocaml.org/learn/tutorials/data_types_and_matching.html



Solution 1:[1]

Since + is a binary operation, the usual representation of a + b + c is this:

Plus (Plus (Var "a", Var "b"), Var "c")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jeffrey Scofield