This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
cs330_f2016:erlang1 [2017/03/07 20:05] dhart [Deliverables:] |
cs330_f2016:erlang1 [2021/06/30 23:42] (current) |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | |||
| ====Objective:==== | ====Objective:==== | ||
| Line 8: | Line 7: | ||
| ====Preparation:==== | ====Preparation:==== | ||
| - | Install Elixir on your own machine. Use Erlang OTP version 19. Note that Erlang is available on all of the open-lab linux machines as ''/usr/bin/erl''. | + | Install Elixir on your own machine. Use Erlang OTP version 19 (or more recent versions). |
| ---- | ---- | ||
| Line 14: | Line 13: | ||
| ====Deliverables:==== | ====Deliverables:==== | ||
| - | Create a module named ''Elxir_Intro'' that contains the following functions: | + | Create a module named ''Elixir_Intro'' that contains the following functions: |
| * ''fib/1'' | * ''fib/1'' | ||
| * ''area/2'' | * ''area/2'' | ||
| Line 26: | Line 25: | ||
| === fib === | === fib === | ||
| <code erlang> | <code erlang> | ||
| - | def fib(N) do ... | + | def fib(n) do ... |
| </code> | </code> | ||
| - | fib will return the n'th Fibonacci number where ''fib(1) = 1'', ''fib(2) = 1'', and ''fib(N) = fib(N-1) + fib(N-2).'' | + | fib will return the n'th Fibonacci number where ''fib(1) = 1'', ''fib(2) = 1'', and ''fib(n) = fib(n-1) + fib(n-2).'' |
| === area=== | === area=== | ||
| <code erlang> | <code erlang> | ||
| - | def area(Shape, ShapeInfo) do .... | + | def area(shape, shape_info) do .... |
| </code> | </code> | ||
| - | Area takes either '':rectangle'', '':square'', '':circle'', or '':triangle'' and the shape info, and returns its area. Note for rectangle the shape Info will be a tuple ''{Length, Height}'', triangle's shapeInfo will be a tuple ''{Base, Height}'', and circle and square will each get a single scalar (radius and side length, respectively). Use pattern matching. | + | Area takes either '':rectangle'', '':square'', '':circle'', or '':triangle'' and the shape info, and returns its area. Note for rectangle the shape Info will be a tuple ''{length, height}'', triangle's shape_info will be a tuple ''{base, height}'', and circle and square will each get a single scalar (radius and side length, respectively). Use pattern matching. Also note that pi is called via :math.pi . |
| === sqrList == | === sqrList == | ||
| <code erlang> | <code erlang> | ||
| - | def sqrList(List) do .... | + | def sqrList(nums) do .... |
| </code> | </code> | ||
| - | Returns a new list in which each item of ''List'' has been squared. Use list comprehensions. | + | Returns a new list in which each item of ''nums'' has been squared. |
| === calcTotals === | === calcTotals === | ||
| <code erlang> | <code erlang> | ||
| - | def calcTotals(List) do ... | + | def calcTotals(inventory) do ... |
| </code> | </code> | ||
| - | Takes a List of tuples of the form ''{Item, Quantity, Price}'' and returns a list of the form ''{Item, TotalPrice}''. (Treat the list item by item: no need to consider the possibility of multiple tuples with the same "item" name.) | + | Takes an inventory list of tuples of the form ''{item, quantity, price}'' and returns a list of the form ''{item, total_price}''. (Treat the list item by item: no need to consider the possibility of multiple tuples with the same "item" name.) |
| === map === | === map === | ||
| <code erlang> | <code erlang> | ||
| - | def map(Function, List) do ... | + | def map(function, vals) do ... |
| </code> | </code> | ||
| - | Map takes a function and List and applies that function to each item in the list. To test it the call should look like this ''map(fun module:functionName/arity, list)''. | + | Map takes a function and a list vals and applies that function to each item in the list. Note that when using higher order functions in Elixir, you use ''&Module.functionName/arity'' to pass it, and ''functionName.(…)'' to call it. Thus,to test it, the call should look like this ''map(&Module.functionName/arity, list)''. For this lab, you can assume the arity of the function being passed in is 1. |
| === quickSortServer === | === quickSortServer === | ||
| Line 58: | Line 57: | ||
| def quickSortServer() do ... | def quickSortServer() do ... | ||
| </code> | </code> | ||
| - | quickSortServer will start a simple server (see slides, particularly slide 19 which shows the translate_service module that receives a message and sends one back) that will receive a list and sort it and send it to the caller. It should sort via a modified version of quickSort as discussed in class. quickSort will choose the pivot randomly, and you will need to implement the new pivot functionality. The module ''random'' function ''random:uniform(N)'' will be useful for this, as well as ''lists:nth(N, List)'' | + | quickSortServer will start a simple server that will receive a list and sort it and send it to the caller. It should sort via a modified version of quickSort as discussed in class. quickSort will choose the pivot randomly, and you will need to implement the new pivot functionality. The module ''random'' function '':random.uniform(N)'' will be useful for this, as well as '':lists.nth(N, List)'' |
| + | Remember that starting a new process would look like ''pid = spawn &Elixir_Intro.quickSortServer/0'' | ||
| + | To test your quickSortServer(), it might be helpful to set up a simple client module like this: | ||
| + | <code python> | ||
| + | defmodule Client do | ||
| + | def callServer(pid,nums) do | ||
| + | send(pid, {nums, self()}) | ||
| + | listen() | ||
| + | end | ||
| + | |||
| + | def listen do | ||
| + | receive do | ||
| + | {sorted, pid} -> sorted | ||
| + | end | ||
| + | end | ||
| + | end | ||
| + | </code> | ||