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 22:55]
scarl
cs326:daily3 [2021/09/06 13:47] (current)
scarl
Line 1: Line 1:
 ==== CSci 326: Daily 3 ==== ==== CSci 326: Daily 3 ====
  
-In **iex**, run a set of pattern matches. After each match, query the variables involved to see their new bindingsRun these in **emacs** so you can turn in a transcript fileThe patterns:+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
  
-  ​''​a = 1''​ +**Part 1.**  ​The following pattern matches use basic collection types. Make a table with two columnsin the first column ​**guess*if the match succeeds or failsand what the value of each variable will be if it succeeds. In the second columnshow the actual result and bindings you get when you run these in **iex**.
-  ​''​{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} }''​ +
-  ​''​{xyx} = { {: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:+  - ''​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]''​ 
 + 
 +**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   * ''​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'' ​   * ''​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}''​.+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.
  
-  - 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). +  - 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). 
-  - 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.+  - 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.1517201716.txt.gz · Last modified: 2018/01/28 22:55 by scarl