Welcome to OStack Knowledge Sharing 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 Answer

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 ...)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...