small medium large xlarge

Dave_gnome_head_isolated_pragsmall
15 Jul 2013, 03:42
Dave Thomas (381 posts)
  • 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>

200-g_pragsmall
27 Aug 2013, 04:28
Aleksey Gureiev (11 posts)

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.

Casual - sqr_pragsmall
16 Sep 2013, 02:57
Ryan Cromwell, Sr (4 posts)

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

Generic-user-small
09 Dec 2013, 05:55
John Marcus (2 posts)

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

Photo-4_pragsmall
12 Dec 2013, 13:16
antonio lorusso (1 post)

I got it as each line is separate statement too.

Generic-user-small
05 Jan 2014, 17:17
Federico Tomassetti (5 posts)

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

Hoodie_hacker_small_pragsmall
02 Dec 2014, 05:05
Stephen Aghaulor (1 post)

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.

Smee2_pragsmall
20 Dec 2014, 02:39
Gerard de brieder (1 post)

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.

2c83da9f629326fa55fe7e8b6a4b8cee_pragsmall
09 Mar 2016, 04:24
Ron Williams (1 post)

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.

Generic-user-small
04 Sep 2016, 16:28
Juha Lehtonen (1 post)

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

Generic-user-small
08 Sep 2016, 06:32
Rafael Porras Lucena (5 posts)

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! :)

You must be logged in to comment