Haskell数据类型

Haskell数据类型,haskell,recursion,grammar,algebraic-data-types,recursive-datastructures,Haskell,Recursion,Grammar,Algebraic Data Types,Recursive Datastructures,以下定义的Bin数据类型的值不是哪个 data Bin = B Bin | C [ Int] a) C[] b) b(bc[2]) c) B(c[1..5]) d) B(B(B(C[2,3]))通过使用ghci并简单地输入代码片段,您将看到哪些值被接受: Prelude> data Bin = B Bin | C [ Int] Prelude> a = C [ ] Prelude> b = B ( B C [ 2]) <interactive>:3:9: err

以下定义的Bin数据类型的值不是哪个

data Bin = B Bin | C [ Int]
a)
C[]

b)
b(bc[2])

c)
B(c[1..5])


d)
B(B(B(C[2,3]))
通过使用
ghci
并简单地输入代码片段,您将看到哪些值被接受:

Prelude> data Bin = B Bin | C [ Int]
Prelude> a = C [ ]
Prelude> b = B ( B C [ 2])

<interactive>:3:9: error:
    • Couldn't match expected type ‘[Integer] -> Bin’
                  with actual type ‘Bin’
    • The function ‘B’ is applied to two arguments,
      but its type ‘Bin -> Bin’ has only one
      In the first argument of ‘B’, namely ‘(B C [2])’
      In the expression: B (B C [2])

<interactive>:3:11: error:
    • Couldn't match expected type ‘Bin’
                  with actual type ‘[Int] -> Bin’
    • Probable cause: ‘C’ is applied to too few arguments
      In the first argument of ‘B’, namely ‘C’
      In the first argument of ‘B’, namely ‘(B C [2])’
      In the expression: B (B C [2])
Prelude> c = B ( C [ 1..5])
Prelude> d = B (B (B (C[2,3])))
Prelude>data Bin=B Bin | C[Int]
前奏曲>a=C[]
前奏曲>b=b(b C[2])
:3:9:错误:
•无法匹配预期的类型“[Integer]->Bin”
实际类型为“Bin”
•函数“B”应用于两个参数,
但其类型“Bin->Bin”只有一个
在“B”的第一个参数中,即“(bc[2])”
在表达式中:B(bc[2])
:3:11:错误:
•无法匹配预期的类型“Bin”
实际类型为“[Int]->Bin”
•可能原因:“C”用于太少的参数
在“B”的第一个参数中,即“C”
在“B”的第一个参数中,即“(bc[2])”
在表达式中:B(bc[2])
前奏曲>c=B(c[1..5])
前奏曲>d=B(B(B(C[2,3]))

要自己轻松回答这些问题,您需要轻松理解Haskell的代数数据类型定义:

  -- v  a type being defined
data Bin = 
           B  Bin 
        -- ^  ^ the type of the argument to the data constructor `B`
        -- ^ the name of the data constructor function `B`:  B :: Bin -> Bin
        --          (read: B is of type Bin -> Bin, i.e. a function
        --                 taking a Bin type value and producing a Bin type value)
         | C  [Int]
        -- ^  ^ the type of the argument to the data constructor `C`
        -- ^ the name of the data constructor function `C`:  C :: [Int] -> Bin
        --          (read: C is of type [Int] -> Bin, i.e. a function
        --                 taking a list of Int values and producing a Bin type value)
这意味着,我们可以使用以下两种方法之一创建
Bin
类型的数据:

  • 首先,我们可以使用一个参数调用名为
    B
    的函数,该参数的值类型为
    Bin
  • 其次,我们可以使用一个参数调用名为
    C
    的函数,该参数是一个Int类型的值,一个Int列表
第二种方法简单明了:只需使用任何整数列表,如
C[1]
C[4,3,2]

x1 :: Bin        -- read: `x1` is of type `Bin`
x1 = C [1]       -- or,
但是第一个呢?我们需要使用类型为
Bin
的值作为参数:

x1 = B x2
但是我们在哪里可以得到x2?我们正在创建一个
Bin
,它希望我们给它一个
Bin
?这是一个恶性循环吗

不!因为我们有第二种创建
Bin
值的方法:

x2 = C [2]    -- x1 = B (C [2])
但我们也可以使用第一个:

x2 = B x3     -- x1 = B (B x3)
那么x3呢?同样地

x3 = C [3]    -- x1 = B (B (C [3]))   -- or,
x3 = B x4     -- x1 = B (B (B x4))

看到了吗?类型为
Bin
的有效值由
B
构造函数的嵌套链组成,在链的最底部,内部携带
C[…]
。正当数据类型定义用作生成该类型有效术语的语法

现在很容易回答您的问题:

  • C[]
    :-这是一个嵌套的
    B
    链,底部是
    C
    :__

  • B(bc[2])
    :-这是一个嵌套的
    B
    链,底部是
    C
    :__

  • B(C[1..5])
    :-这是一个嵌套的
    B
    链,底部是
    C
    :__

  • B(B(B(C[2,3]))
    :-这是一个底部带有
    C
    的嵌套
    B
    链吗__

同一规则的这种重复应用称为递归,因此该数据类型
Bin
是一种递归数据类型,其定义中有两个子句:递归子句和基子句


哪个是哪个

您可以尝试在
ghci
中输入类型定义和不同的值,自己计算出来。我使用hugs,但无法识别。非常感谢☺多谢各位☺不客气。那么,哪个是哪个?你能告诉我吗?数据类型定义中的哪个子句是递归的,而哪个不是递归的?递归定义是Myes,但其中有两个子句-一个用于B,另一个用于C。其中哪一个是递归的,哪一个是基本情况?
x4 = C [4]    -- x1 = B (B (B (C [4])))   -- or,
x4 = B x5     -- x1 = B (B (B (B x5)))