User Tools

Site Tools


cs326:daily3

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
cs326:daily3 [2018/01/28 21:09]
scarl
cs326:daily3 [2021/09/06 13:47] (current)
scarl
Line 1: Line 1:
 ==== CSci 326: Daily 3 ==== ==== CSci 326: Daily 3 ====
  
-Define each of these functions in a module ''​Daily3''​. Place the module in a file **daily3.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.+Record your results from Part 1 and the results ​of running ​tests from Part 2 on paper and turn in at the beginning ​of the next classYou can turn in a printout of the code for Part 2, or just email it
  
-While you are developing your solutions, you can try things out in **iex**, but you'll have to look up the compile command.+**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 failsand 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**.
  
-  - Write function ​ Test with: +  - ''​= 1''​ 
-     * ''​Daily2.average(1010)''​ +  ​- ​''​{cd} = {2, 3}''​ 
-     * ''​Daily2.average(57)''​ +  ​- ​''​{x:abc} = {123, :abc}''​ 
-     * ''​Daily2.average(58)''​ +  ​- ​''​{xy} = {333, :abc, "​cat"​}''​ 
-     * ''​Daily2.average(5.08.0)''​ +  ​- ​''​{xy, z} = { {:abc, 123}, 42, {:abc, 123} }''​ 
-     * ''​Daily2.average(12.413.7)''​ +  ​- ​''​{xy, x} = { {:abc, 123}, 42, {:abc, 123} }''​ 
-  - Define ​''​something'' ​which  +  - ''​{x, y, x} = { {:abc, 123}, 42, true }''​ 
-  - Hey  +  - ''"​Elixir " <> rest = "​Elixir Rocks!"''​ 
-    * For example, ''​Daily2.quad-roots(275)'' ​would return ​''​{-1.0 -2.5}''​ +  - ''​ [ef, g] = [4, :five, [6, 7]]''​ 
-    * Hint: returning two values as a tuple is super easy. Consider:+  ​''​[head | tail] = [4:five[6, 7]]''​ 
 +  - ''​[head | tail] = "​cat"''​ 
 +  ​''​[a,​ b, c | tail]  = [12, 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.
 +
 +**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 x<​sup>​2</​sup>​ + 3x + 2. Likewise, we can use a tuple to represent a cubic equation, like ''​{:​cubic,​ 1, 3, 2, 16}''​ for x<​sup>​3</​sup>​ + 3x<​sup>​2</​sup>​ + 2x + 16.
 +
 +  - In a module ''​Daily3'',​ 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).
 +  - Name the file containing the module **daily3.exs** along with code to exercise the function and print the results when run using the **elixir** command.
  
    
cs326/daily3.1517195341.txt.gz · Last modified: 2018/01/28 21:09 by scarl