In the source code for Data.FixedList, I found the following definition:
data FixedList f =>
Cons f a = (:.) {
head :: a,
tail :: (f a)
} deriving (Eq, Ord)
As someone very new to Haskell, it's hard to figure out what's going on here. I understand syntax such as data TypeName = TypeName { a :: Int, b :: Int} deriving (Show) or data TypeName = TypeA | TypeB, but the code above is over my head. Any documentation / or walk-through would be much appreciated!
First of all the FixedList f => enforces the constraint that f is a FixedList when using the constructor. I'm not sure why it's used here. It is generally advised against.
In Haskell, you can make infix data constructors with symbols starting with :. In the library, the constructor is put in brackets (:.) so it can be used with record syntax. Without record syntax it would look like this:
data Cons f a = a :. f a
Pattern matching is very similar to lists. Here's a simple function using it:
mapHead :: FixedList f => (a -> a) -> Cons f a -> Cons f a
mapHead f (a :. as) = f a :. as
Here's a definition without using an infix constructor.
data Cons f a = Cons
{ head :: a
, tail :: f a
}
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