User Tools

Site Tools


cs326:match

CSci 326: Daily 3

Part 1. The following pattern matches use basic collection types. Make a table with two columns: in the first column guess if the match succeeds or fails, and what the value of each variable will be if it succeeds. In the second column, show the actual result and bindings you get when you run these in iex.

  1. a = 1
  2. {c, d} = {2, 3}
  3. {x, :abc} = {123, :abc}
  4. {x, y} = {333, :abc, “cat”}
  5. {x, y, z} = { {:abc, 123}, 42, {:abc, 123} }
  6. {x, y, x} = { {:abc, 123}, 42, {:abc, 123} }
  7. {x, y, x} = { {:abc, 123}, 42, true }
  8. “Elixir ” <> rest = “Elixir Rocks!”
  9. [e, f, g] = [4, :five, [6, 7]]
  10. [head | tail] = [4, :five, [6, 7]]
  11. [head | tail] = “cat”
  12. [a, b, c | tail] = [1, 2, 3, 4, 5, 6]

Note: if you copy/paste these, the double-quote character won't copy correctly, giving unexpected errors. Type the double-quote directly from the keyboard.

cs326/match.txt · Last modified: 2021/09/03 12:10 by scarl