CO545 Spring Term 201819 Terminal Class 4
This worksheet covers an application supermarket billing and its implementation in Erlang. This will give you more experience in writing Erlang list processing functions, as well as being your first opportunity to work by extending some existing Erlang code that is provided in the file bill.erl.
Types and specs
The file contains some type definitions, including these:
% An item is represented by a string
-type item() :: string().
% Cost is an integer: think of this as pence.
-type cost() :: integer().
% Barcodes are represented by integers.
-type code() :: integer().
% A list of barcodes
-type codes() :: list(code()).
% The database is a list of triples, mapping
% codes to item/cost pairs.
-type database() :: list({code(),item(),cost()}).
The first three are synonyms using memorable names for existing types. The type codes() is used for a list of code(), and database() for a list of triples: a (bar)code, an item name and its cost.
Using these types were able to give type specifications for functions. For example,
-spec lookup(code(),database()) -> {item(),cost()}.
says that the lookup function takes two things, a code and a database, and the result is a pair of an item and a cost. The effect of this is to use the database to find the code and return its corresponding item and cost. For example, if the database DB contains
{237891,Gin,1099}
and we call lookup(237891,DB) wed get the answer {Gin,1099}.
The task
You will find some definitions in the file that you can use. It contains an example database:
bill:my_db()
and an example input (list of barcodes):
bill:my_codes()
As well as this you can find the show and print functions which allow you to print out lists of codes, bills and databases.
The definitions that you need to complete are currently given with a dummy definition, such as
lookup(_Code,_DB) -> dummy().
Youll need to remove the dummy() and complete the definition.
Important note in most cases you will need to think about how to use pattern matching: in general that means that your definitions will have more than the one clause that the dummies have.
What should the functions do?
While the -spec declarations give you some idea of what the functions should do, heres some more information:
lookup/2
lookup(Code,DB) should lookup the Code in the database DB and return a pair {Item,Cost} corresponding to the Code.
lookup/1
lookup(Code) should lookup the Code in the database my_db
make_bill/1
make_bill(Codes) should turn a list of Code into a Bill: think what function you could use to help you here. You can see an example of this in the next section.
total/1
total(Bill) should take a Bill and return the sum of all the costs in the Bill.
count/2
count(Item,Bill) should count the number of times that a particular
Item occurs in a Bill
multibuy/3
A multi-buy discount comes when multiples of the same item occur. multibuy(Discount,Item,Bill) will return a discount (negative integer) which is the Discount times the number of pairs of Item in the Bill.
add_multibuy/3
Attach to the end of the Bill a new entry corresponding to the multibuy discount multibuy(Discount,Item,Bill). You can see an example of this in the next section.
add_total/1
Attach to the end of the bill a new item representing the total. You can see an example of this in the next section.
How to tackle the problem
Its best to complete the definitions in the order that they occur in the file, as later functions use earlier ones.
When youre testing your answers, its a really good idea to use Variables in the shell to name earlier results. You can see that in this (edited) transcript below:
Important note: you can forget the value of the variable Bill in the shell by typing f(Bill), and you can forget all variables by typing f().
1> Bill = bill:make_bill(bill:my_codes()). [{Fish Fingers,99},
{Gin,1099},
{Tonic,199},
{Tonic,199},
{Fish Fingers,99}]
2> MBill = bill:add_multibuy(50,Fish Fingers,Bill). [{Fish Fingers,99},
{Gin,1099},
{Tonic,199},
{Tonic,199},
{Fish Fingers,99},
{Multibuy,-50}]
3> TBill = bill:add_total(MBill). [{Fish Fingers,99},
{Gin,1099},
{Tonic,199},
{Tonic,199},
{Fish Fingers,99},
{Multibuy,-50},
{TOTAL,1645}]
4> bill:total(Bill).
1695
5> bill:print(TBill).
Fish Fingers99
Gin1099
Tonic199
Tonic199
Fish Fingers99
Multibuy-50
TOTAL1645
ok
Reviews
There are no reviews yet.