[Haskell-beginners] Non-exhaustive patterns
Jeffrey Brown
jeffbrown.the at gmail.com
Sun Dec 12 19:08:49 UTC 2021
The function as written assumes that if the first argument is not zero,
then the second argument is nonempty. But if you try to take5 from a list
of length 2 that way, you'll eventually be asking for `myTake 3 []`, which
there's no way to evaluate.
On Sun, Dec 12, 2021 at 12:14 AM Galaxy Being <borgauf at gmail.com> wrote:
> This code
>
> myTakePM :: Int -> [a] -> [a]
> myTakePM 0 _ = []
> myTakePM n (x:xs) = x : myTakePM (n-1) xs
>
> is bad because it allows
>
> myTakePM 4 [1,2,3]
> [1,2,3*** Exception: <interactive>:(395,1)-(396,41): Non-exhaustive
> patterns in function myTakePM
>
> I knew it would not work, but why is it calling this essentially a partial
> function? How does it know this? Again, I expected an error, but what is
> this Non-exhaustive patterns in function myTakePM saying? Or, said
> another way, what exactly is non-exhaustive about this?
> --
> ⨽
> Lawrence Bottorff
> Grand Marais, MN, USA
> borgauf at gmail.com
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
--
Jeff Brown | Jeffrey Benjamin Brown
LinkedIn <https://www.linkedin.com/in/jeffreybenjaminbrown> | Github
<https://github.com/jeffreybenjaminbrown> | Twitter
<https://twitter.com/carelogic> | Facebook
<https://www.facebook.com/mejeff.younotjeff>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20211212/7ff8e5b3/attachment.html>
More information about the Beginners
mailing list