smokydrips
smokydrips

Reputation: 19

create a type with a constructor that takes in a string and a list

I want to extend a type ExprTree (expression tree) with a new constructor App that takes in a string and a list as arguments. Below is the type ExprTree:

type ExprTree =
    | Const of int
    | Ident of string
    | Sum of ExprTree * ExprTree
    | Let of string * ExprTree * ExprTree

Upvotes: 0

Views: 62

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243051

The definition that you posted in a comment is correct. Your version from the comments was:

type ExprTree =
    | Const of int
    | Ident of string
    | Sum of ExprTree * ExprTree
    | Let of string * ExprTree * ExprTree
    | App of string * ((ExprTree) list)

The definition of the App constructor has some unnecessary parentheses - you do not need any and can write just string * ExprTree list, but they do not hurt eihter. I suspect that the issue was not with the definition, but with how you use the constructor. The following is the right syntax:

App("foo", [Const 1; Ident "x"])

Upvotes: 1

Related Questions