Practice Matching a Tuple

You learn by doing. Here are a few exercises to play with in IEx. The solutions are here but hidden. Find the solution yourself first. Experiment. Have fun!

Exercise #1

Write the left hand side of this statement. Your goal is to only match when the first element of the tuple is :ok and then bind the value 1500 to a variable named answer. What does the left side look like?

_your_statement = {:ok, 1500}

Exercise #2

Given a tuple that represents a date used in native Erlang functions, how would you bind a variable to the month value? The format is {year, month, day}

_your_statement = {2020, 2, 14}

Comments are closed

This is a static version of the site. Comments are not available.

Comments are closed on this static version of the site.