【发布时间】:2016-06-11 14:38:13
【问题描述】:
我在 Haskell 中有一个练习,我需要在其中创建各种类型。第一种类型称为 Finite,其定义如下:
type Finite a = [a]
然后我需要返回一个像这样定义的单例
singleF :: a -> Finite a
所以我是这样实现的:
single n = [n]
然后我创建另一个类型
type Enumeration a = Int -> Finite a
那我需要重新实现单例函数
singleE :: a -> Enumeration a
在我的理解中,Enumeration 类型是从 Int 到 a 类型列表的函数的同义词,但我不明白我该如何实现它。
来自练习(之前的类型“有限”也称为“桶”):An enumeration is an infinite sequence of finite buckets, indexed by natural numbers。
而函数single:I suggest for simplicity that you put the sole item in bucket 0,所以我认为int是枚举中bucket的索引
【问题讨论】:
-
有很多不同的方法可以做到这一点——您希望枚举具有哪些属性? (整数应该确定列表长度、最大长度、起始位置……还是完全不同的东西?)
-
@leftaroundabout 来自练习(之前的类型 'Finite' 也称为 'bucket'):
An enumeration is an infinite sequence of finite buckets, indexed by natural numbers.和函数 single :I suggest for simplicity that you put the sole item in bucket 0,所以我认为int是枚举中bucket的索引。 -
您应该将该信息编辑到问题中。
-
无论如何你都会想要
singleE :: Enumeration a。 -
@chepner 练习清楚地表明 singleE 的定义正如我在上面写的
singleE :: a -> Enumeration a
标签: list haskell functional-programming