Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

haskell - Creating infinite list out of ADT

In Haskell,

 > a = [1,1..]

creates an infinite list. Now I have the following

data Subunit = O | P deriving (Eq, Show)           

And if I do

b :: [Subunit]                                                                   
b = take 6 [P,P..]  

I get the following:

 parse error on input ‘]’

Why this is failing? What I need to add to be able to create an infinite list?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Nice catch! Indeed it errors out ...

> take 10 [P, P..]

<interactive>:6:16: parse error on input ‘]’

... but this does not

> take 10 [P, P ..]   -- one more space
[P,P,P,P,P,P,P,P,P,P]

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

Hence, P.. is . from module P, while P .. works fine in a list enumeration.

(Yes, this is an unfortunate quirk of the syntax ...)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...