Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Pattern Matching (Yugesh Kothari)
   2. Re:  Pattern Matching (Bob Ippolito)


----------------------------------------------------------------------

Message: 1
Date: Sat, 27 Apr 2019 20:01:18 +0530
From: Yugesh Kothari <kothariyug...@gmail.com>
To: beginners@haskell.org
Subject: [Haskell-beginners] Pattern Matching
Message-ID:
        <cacsi4wal+7orvz9tobxhfazsvz827bpjmm8ychmyf+q4jsd...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

This is probably a stupid question but I can't seem to understand the use
of @ in haskell pattern matching.

Ex -
compress (x:ys@(y:_))
| x==y = compress us
| otherwise = x : compress us
compress us = us

Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20190427/e3ded698/attachment-0001.html>

------------------------------

Message: 2
Date: Sat, 27 Apr 2019 08:24:07 -0700
From: Bob Ippolito <b...@redivi.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Pattern Matching
Message-ID:
        <cacwmpm_5rx0d+joxwqky_c9aw9_062+jx05wxthzbywkpu-...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

That’s called an as-pattern. It binds part of the pattern match to a
variable. In this case you might want to do that for efficiency reasons.

You can find an example of them here:
https://www.haskell.org/tutorial/patterns.html

I think your example isn’t correct, the `us` variable is only defined for
the last clause. The first two should use `compress ys`.

Without as-patterns this would look like:

compress (x:y:ys’)
| x==y = compress (y:ys’)
| otherwise = x : compress (y:ys’)
compress us = us

It can be more efficient and concise to use ys@(y:_) in the pattern match
and ys elsewhere  instead of having to repeat (y:ys’) which without
optimizations would call the : constructor again and may not share memory
in the same way.

-bob


On Sat, Apr 27, 2019 at 07:31 Yugesh Kothari <kothariyug...@gmail.com>
wrote:

> This is probably a stupid question but I can't seem to understand the use
> of @ in haskell pattern matching.
>
> Ex -
> compress (x:ys@(y:_))
> | x==y = compress us
> | otherwise = x : compress us
> compress us = us
>
> Thanks!
>
> _______________________________________________
> Beginners mailing list
> Beginners@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/20190427/ebc71c3d/attachment-0001.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 130, Issue 8
*****************************************

Reply via email to