cs326:daily3

**This is an old revision of the document!**

In **iex**, run a set of pattern matches. After each match, query the variables involved to see their new bindings. Run these in **emacs** so you can turn in a transcript file. The patterns:

`a = 1`

`{c, d} = {2, 3}`

`{x, :abc} = {123, :abc}`

`{x, y} = {333, :abc, “cat”}`

`{x, y, z} = { {:abc, 123}, 42, {:abc, 123} }`

`{x, y, x} = { {:abc, 123}, 42, {:abc, 123} }`

`{x, y, x} = { {:abc, 123}, 42, true }`

`“Elixir ” <> rest = “Elixir Rocks!”`

`[e, f, g] = [4, :five, [6, 7]]`

`[head | tail] = [4, :five, [6, 7]]`

`[head | tail] = “cat”`

`[a, b, c | tail] = [1, 2, 3, 4, 5, 6]`

You saw the tuple data type in Daily 1. There are two built-in functions for accessing tuple elements and creating new tuples from existing ones:

`elem(tp, index)`

returns the*nth*value in the tuple`tp`

, starting at 0`put_elem(tp, n, value)`

returns a new tuple replacing the*nth*element of`tp`

with`value`

We can represent a quadratic equation as a *tuple* where each element is a coefficient; for example we can use `{:quad, 1, 3, 2}`

to represent x^{2} + 3x + 2. Likewise, we can use a tuple to represent a cubic equation, like `{:cubic, 1, 3, 2, 16}`

for x^{3} + 3x^{2} + 2x + 16.

- Write a function
`eval_equation`

that takes a tuple and a value for`x`

and returns the result of evaluating the equation at that value. Use pattern matching and multiple clauses to determine which type of equation you have, quadratic or cubic (assume the tuples are the correct length and contains numbers). - Define your functions in a module
`Daily3`

. Name the file containing the module**daily3.exs**along with code to exercise the function and print the results.

cs326/daily3.1517201786.txt.gz · Last modified: 2018/01/28 22:56 by scarl