如果在ML中,递归数据类型的示例是:
datatype llist = Nil | Node of int * llist
什么是相互递归的数据类型,其中有一个例子,在ML?
答案 0 :(得分:5)
一个这样的例子可能是这些愚蠢的数据类型。
datatype a = A | Ab of b
and b = B | Ba of a
它们没有任何意义,但是它们表明可以使用and
关键字(就像函数一样)来引用通常不可能的“前方”
他们是相互(因为他们俩......)递归(......互相引用)
答案 1 :(得分:2)
相互递归数据类型的标准基本示例是树和林:林是树的列表,而树是值和林(根的值和子项的子树)。在标准ML中,这可以定义如下,允许空树:
datatype 'a tree = Empty | Node of 'a * 'a forest
and 'a forest = Nil | Cons of 'a tree * 'a forest
来自“Data Types”,编程标准ML,作者:Robert Harper(2000)。
另一个例子是通过生成规则在正式语法中定义表达式,例如以下对于整数的带括号的算术表达式:
datatype int_exp = plus of int_term * int_term
| minus of int_term * int_term
and int_term = times of int_factor * int_factor
| divide of int_factor * int_factor
| modulo of int_factor * int_factor
and int_factor = int_const of int
| paren of int_exp;
来自“Defining datatypes”。
我在维基百科上更新了“Mutual recursion”以举例(包括标准ML)。