small medium large xlarge

• If you assume the variable `a` initially contains the value `2`, which of the following will match?

• `[ a, b, a ] = [ 1, 2, 3 ]`
• `[ a, b, a ] = [ 1, 1, 2 ]`
• `a = 1`
• `^a = 2`
• `^a = 1`
• `^a = 2 - a`

A Possible Solution</summary>

```a = 2           #=> a → 2

[ a, b, a ] = [ 1, 2, 3 ]
# ** (MatchError) no match of right hand side value: [1, 2, 3]
#     :erl_eval.expr/3

[ a, b, a ] = [ 1, 1, 2 ]
# ** (MatchError) no match of right hand side value: [1, 1, 2]
#     :erl_eval.expr/3

a = 1           #=> a → 1

^a = 2
# ** (MatchError) no match of right hand side value: 2
#    :erl_eval.expr/3

^a = 1          #=> matches. a still 1

^a = 2 - a      #=> matches. a still 1
```

</details>

From the task, it’s not clear that those matches should be performed sequentially. In other words, when I got to `^a = 2` I assumed we still have `2` in `a`, and so it would be a match.

I too thought each line was independent. As if a multiple choice question.

Another vote for clarifying that each line was a separate question, as I assumed the variable ‘2’ was assigned for each line.

I got it as each line is separate statement too.

Me too, I was expecting therefore ^a=2 to match

The question is worded such that it suggests multiple choice. Perhaps it should be rephrased to clarify it’s intent. However, as Dave’s posts suggests, the solution is only a possible solution. Another possible solution is that if not run sequentially, then both `a = 1` and `^a = 2` match.

Same here I assuned a to be 2 and then parse each statement as if a was freshly assigned without the history of the previous statement.

The wording in your solution is more clear than the text in the book. The book almost reads as though you should be setting a = 2 for each line. I’m using the latest ebook as of the date of this post.

One more individual here who thought that a should be set to 2 for each separate statement in isolation from the rest.

You all are right, in this case, the statement of the variable a is bound to the value 2 does not make very sense unless the assumption is done for each statement. If we think on the list about a sequence of matchings, then it does not make sense if the value was 2, 3, 4 or whatever, as the initial lines are bounding new values to a.

Anyway, the author is just proposing a solution, we can think about the example in both ways so we actually have two examples! :)

Here is another one which thought that each line was individual :)

You must be logged in to comment