从ADT创建无限列表 [英] Creating infinite list out of ADT
问题描述
在Haskell中,
> a = [1,1..]
创建一个无限列表.现在,我有以下内容
creates an infinite list. Now I have the following
data Subunit = O | P deriving (Eq, Show)
如果我愿意
b :: [Subunit]
b = take 6 [P,P..]
我得到以下信息:
parse error on input ‘]’
为什么失败了?我需要添加什么才能创建无限列表?
Why this is failing? What I need to add to be able to create an infinite list?
推荐答案
不错!确实,它出错了...
Nice catch! Indeed it errors out ...
> take 10 [P, P..]
<interactive>:6:16: parse error on input ‘]’
...但这不是
> take 10 [P, P ..] -- one more space
[P,P,P,P,P,P,P,P,P,P]
为什么空白很重要?因为否则语法会与模块前缀的名称重叠,该名称的格式为Module.name
.例如,下面是访问Prelude
中的运算符.
的方法.
Why the whitespace is significant? Because otherwise the syntax overlaps with module-prefixed names, which have the form Module.name
. Here's how the operator .
from Prelude
is accessed, for instance.
> :t (Prelude..)
(Prelude..) :: (b -> c) -> (a -> b) -> a -> c
> :t succ Prelude.. succ -- infix use!
succ Prelude.. succ :: Enum c => c -> c
因此,P..
是模块P
中的.
,而P ..
在列表枚举中工作正常.
Hence, P..
is .
from module P
, while P ..
works fine in a list enumeration.
(是的,这是一个不幸的语法问题……)
(Yes, this is an unfortunate quirk of the syntax ...)
这篇关于从ADT创建无限列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!