# DokuWiki

cs326:fcns

### CSci 326: Daily 3

Purpose: using named functions in a module.

NOTE!!! We probably don't need the Part 3 problem anymore…does it offer anything new???

Reading: the first three sections in Elixir School:Named Functions and Sections 6.1-6.3 of the Thomas text.

Create a file `maths.exs` while defines a module `Maths`. Define the functions below in this module, along with code to exercise the functions (and print the results) using the tests as shown. For the second and third function, add two tests of your own (along with expected results). This script should run properly using the elixir program which is how I'll test them.

While you are developing your solutions, you can try things out in iex; the first section of Ch. 6 describes ways to compile while using iex.

Part 1.

1. Write a function to average two numbers. Test with:
• `Daily2.average(10, 10)`
• `Daily2.average(5, 7)`
• `Daily2.average(5, 8)`
• `Daily2.average(5.0, 8.0)`
• `Daily2.average(12.4, 13.7)`
2. Define `discrim(a, b, c)` which returns b2 - 4ac; for example, `Daily2.discrim(2, 7, 5)` should return 9.0
3. Use your `discrim` function to define `quad_roots(a, b, c)` that returns the two roots of a quadratic function, should they exist, as a tuple. Note that Elixir does not define a square root function, since it is already available in the Erlang libraries. To call this function, use `:math.sqrt`.
• For example, `Daily2.quad-roots(2, 7, 5)` would return `{-1.0 -2.5}`
• Hint: returning two values as a tuple is super easy. Consider:
```def return_tuple() do
x = 1
y = 2
{x, y}
end```

Part 2. 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 x2 + 3x + 2. Likewise, we can use a tuple to represent a cubic equation, like `{:cubic, 1, 3, 2, 16}` for x3 + 3x2 + 2x + 16.

1. create a module `Daily4.Quadratic` in a script file `daily4.exs`.
2. add a new 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 (you can assume tuples are the correct length and contain numbers in the right places).
3. Add code to exercise this function and print the results when run using the elixir command.

Part 3. In module `Daily4.Quadratic` containing these functions:

1. Develop a set of functions to determine if a quadratic equation has 2, 1, or 0 solutions.The equation has 0 solutions if it is degenerate that is, if the first coefficient is 0. The equation has 1 solution if the equation has repeated roots (when the discriminant is 0). The equation has 2 solutions otherwise, but notice that the solutions are complex if the discriminant is negative. Example functions:
• `degenerate` - takes a number, the first coefficient
• `quad_solutions` - takes a tuple containing three numbers, the coefficients
• `discriminant` - this you already have 