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?
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 ...)
as an alternative to behzads answer you can do
b :: [Subunit]
b = take 6 $ repeat P
too - this one does not need the Enum
[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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With