【问题标题】:what is a mutually recursive type?什么是相互递归类型?
【发布时间】:2013-02-03 12:39:03
【问题描述】:

如果在 ML 中,递归数据类型的示例是:

datatype llist = Nil | Node of int * llist

在 ML 中什么是相互递归数据类型以及它的一个示例?

【问题讨论】:

    标签: types sml ml recursive-datastructures mutual-recursion


    【解决方案1】:

    相互递归数据类型的标准基本示例是树和森林:森林是树的列表,而树是值和森林(根及其子树的子树的值)。在标准 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”。

    我在 Wikipedia 上更新了“Mutual recursion”以提供示例(包括标准 ML)。

    【讨论】:

      【解决方案2】:

      一个这样的例子可能是这些愚蠢的数据类型。

      datatype a = A | Ab of b
      and      b = B | Ba of a
      

      它们没有意义,但它们表明可以使用 and 关键字(就像函数一样)来引用通常不可能的“提前”内容

      它们是相互的(因为它们都是......)递归(......相互引用)

      【讨论】:

        猜你喜欢
        • 2015-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-25
        • 1970-01-01
        • 2011-04-24
        相关资源
        最近更新 更多