User Tools

Site Tools


cs330_f2016:erlang1

This is an old revision of the document!


Objective:

Learn the basics of Elixir.


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.


Deliverables:

Create a module named Elxir_Intro that contains the following functions:

  • fib/1
  • area/2
  • sqrList/1
  • calcTotals/1
  • map/2
  • quickSortServer/0

You do not need to validate function input.

fib

def fib(N) do ...

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

def area(Shape, ShapeInfo) do ....

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.

sqrList

def sqrList(List) do ....

Returns a new list in which each item of List has been squared.

calcTotals

def calcTotals(List) do ...

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.)

map

def map(Function, List) do ...

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).

quickSortServer

def quickSortServer() do ...

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)

cs330_f2016/erlang1.1488917374.txt.gz · Last modified: 2021/06/30 23:40 (external edit)