将数据定义封装在Haskell中 [英] Encapsulating data definitions in Haskell

查看:77
本文介绍了将数据定义封装在Haskell中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用其他数据类型来定义数据类型,如下所示:

I am trying to define a data type using other data types like this:

data A = Something String | SomethingElse Int

data B = Another B | YetAnother A

data C = A | B

x :: [ C ]
x = [ YetAnother (SomethingElse 0), Something "Hello World" ]

但是这给了我一个错误,说我期望B类型时不能有A类型。为什么会这样?

But this is giving me an error saying that I cannot have a type A when expecting a type B. Why is this?

推荐答案

您缺少 C 的数据构造函数。

You're missing the data constructors for C.

data A = Something String
       | SomethingElse Int

data B = Another    B
       | YetAnother A

data C = C0 A
       | C1 B

x :: [ C ]
x = [ C1 (YetAnother (SomethingElse 0))
    , C0 (Something "Hello World")
     ]

这篇关于将数据定义封装在Haskell中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆