[Haskell-beginners] Pattern matching is actually about matching constructors??

Petr Vápenka petr.vapenka at gmail.com
Mon Dec 21 19:00:30 UTC 2015


Hello,

actually the definition with data keyword is right there:

infixr 5 :-:
data List a = Empty | a :-: (List a) deriving (Show, Read, Eq, Ord)

it could be written in prefix form as

data List a = Empty | Cons a (List a) deriving (....)


Petr




On Mon, Dec 21, 2015 at 7:56 PM, Olumide <50295 at web.de> wrote:

> Hello,
>
> On chapter 7 of LYH there's an example of a user-defined operator .++
>
> infixr 5  .++
> (.++) :: List a -> List a -> List a
> Empty .++ ys = ys
> (x :-: xs) .++ ys = x :-: (xs .++ ys)
>
> which is used as follows
>
> let a = 3 :-: 4 :-: 5 :-: Empty
> let b = 6 :-: 7 :-: Empty
> a .++ b
> (:-:) 3 ((:-:) 4 ((:-:) 5 ((:-:) 6 ((:-:) 7 Empty))))
>
> Following this the text reads:
>
> "Notice how we pattern matched on (x :-: xs). That works because pattern
> matching is actually about matching constructors. We can match on :-:
> because it is a constructor for our own list type ..."
> Source:
> http://learnyouahaskell.com/making-our-own-types-and-typeclasses#recursive-data-structures
>
> Is the operator :-: a constructor? I'm confused because the definition of
> :-: is not prefixed by the data keyword?
>
> - Olumide
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20151221/9d6637c7/attachment.html>


More information about the Beginners mailing list