Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating infinite list out of ADT

Tags:

haskell

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?

like image 842
Dilawar Avatar asked Jan 20 '16 16:01

Dilawar


3 Answers

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

like image 171
chi Avatar answered Oct 21 '22 02:10

chi


as an alternative to behzads answer you can do

b :: [Subunit]
b = take 6 $ repeat P

too - this one does not need the Enum

like image 38
Random Dev Avatar answered Oct 21 '22 00:10

Random Dev


[n,n'..] is syntax sugar for enumFromThen so you need your type be an instance of Enum type class.

You also need a value, not a value constructor:

\> data Subunit = O | P deriving (Eq, Show, Enum)
\> let a = P
\> take 6 [a,a..]
[P,P,P,P,P,P]

or (per @sepp2k comment):

\> take 6 [P,P ..]
   --         ^ space
[P,P,P,P,P,P]
like image 3
behzad.nouri Avatar answered Oct 21 '22 01:10

behzad.nouri