Patern Matching on Koans

Hi! Could someone help me on that?

I’m trying to solve that one, it seems to be right but is not passing:

The original code is:

koan "Lists must match exactly" do
    assert_raise ___, fn ->
      [a, b] = [1, 2, 3]
    end
  end  

And I’m trying that:

koan "Lists must match exactly" do
    assert_raise ___, fn ->
      [a, b, c] = [1, 2, 3]
    end
  end

Then the output is like “still the same”:

Lists must match exactly
Assertion failed in lib/koans/12_pattern_matching.ex:42
assert_raise(___, fn → [a, b, c] = [1, 2, 3] end)

Maybe I’m not understanding that structure calling an anonymous function.

I already had tested on my terminal doing just:

[a, b, c] = [1, 2, 3]

And it seems to be right :sweat_smile:

The test is using assert_raise, so I guess you do not have to make the match work but just to provide the correct exception name at ___.

1 Like

@lud Gotcha!

Tried to fix:

koan "Lists must match exactly" do
    assert_raise "MatchError (no match of right hand side value: [1, 2, 3])", fn ->
      [a, b] = [1, 2, 3]
    end
  end

But it seems still no pass:

Now meditate upon PatternMatching
|================> | 119 of 209

Lists must match exactly
Assertion failed in lib/koans/12_pattern_matching.ex:44
Expected exception “MatchError (no match of right hand side value: [1, 2, 3])” but got MatchError (no match of right hand side value: [1, 2, 3])

Take a look at the docs for assert_raise/2 and assert_raise/3 to have the correct syntax.

3 Likes

@lud Just solved:

koan "Lists must match exactly" do
    assert_raise MatchError, "no match of right hand side value: [1, 2, 3]", fn ->
      [a, b] = [1, 2, 3]
    end
end

Thanks for helping!

3 Likes