Open blocka opened 8 years ago
I'm not sure what you mean. Pattern matching, destructuring, and case expressions are all the same basically.
You're not pattern matching at all in your example. You are constructing a tuple, and it's a function taking two arguments.
What part of your example do you think needs to be explained? Hopefully we can help you out with it. On Aug 10, 2016 3:46 AM, "blocka" notifications@github.com wrote:
There is a clear explanation of the case syntax. Then there is another example of a syntax called pattern matching, but no case is used, and no explanation of the syntax.
addVectors a b = (fst a + fst b, snd a + snd b)
After reading this a couple of times, I realized that this is destructuring, and pattern matching is not synonymous with the case expression, but can be used here as well.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/learnyouanelm/learnyouanelm.github.io/issues/19, or mute the thread https://github.com/notifications/unsubscribe-auth/ACzn0vEMKxiXecFhOeFNUn7QjS5NzVfqks5qeawRgaJpZM4JhA-k .
I just realized I copied the wrong example
But let me elaborate on my confusion anyway:
This chapter started by talking about the case
expression. All the examples were in the beginning were using that expression.
Then comes:
Pattern matching can also be used in the declared parameters of functions, and on tuples. What if we wanted to make a function that takes two vectors in a 2D space (that are in the form of pairs) and adds them together? To add together two vectors, we add their x components separately and then their y components separately.
[...example of bad way to do it...]
Well, that works, but there’s a better way to do it. Let’s modify the function so that it uses pattern matching.
Now at this point, I am expecting to see a case
expression again.
Instead I see:
addVectors : (Float, Float) -> (Float, Float) -> (Float, Float)
addVectors (x1, y1) (x2, y2) =
(x1 + x2, y1 + y2)
Which confuses me. Luckily, I'm familiar with object destructuring, so I eventually I realized that that's what's going on.
My new understand is that pattern matching is a language feature useful in case
expressions and in object destructuring.
Please let me know if i'm understanding it correct, otherwise, i'll go back to being confused.
I just realized I copied the wrong example
Ahh, that makes way more sense. I'll modify this when I get a chance, though I haven't had time to work on this guide for several months.
You're right, this is basically destructuring here.
You can pattern match in 3 places:
let
definitionBut 2 and 3 can only be done when there's only one possible value this could take. So tuples, for example, are fine here, since there aren't different constructors to deal with. It also works for cases like this
type FileName = FileName String
doWithFile : FileName -> Foo
doWithFile (FileName file) = ...
Here, it's a normal tagged union type, but there's only one tag, so we can pattern match right in the argument.
There is a clear explanation of the case syntax. Then there is another example of a syntax called pattern matching, but no
case
is used, and no explanation of the syntax.After reading this a couple of times, I realized that this is destructuring, and pattern matching is not synonymous with the
case
expression, but can be used here as well.