User Tools

Site Tools


CSci 326: Daily 2

Purpose: learning to use named functions in a module. For more information, look at the first three sections in Elixir School:Named Functions or Sections 6.1-6.3 of the Thomas text.

Define each of these functions in a module Daily2. Place the module in a file daily2.exs along with code to exercise the functions (and print the results) using the tests given below. 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.

  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}
cs326/daily2.txt · Last modified: 2021/09/13 15:47 by scarl