September 2013
Intermediate to advanced
548 pages
12h 25m
English
In Defining Lists, we had a shopping list that looked like this:
| | [{oranges,4},{newspaper,1},{apples,10},{pears,6},{milk,3}] |
Now suppose that we’d like to know what our shopping costs. To
work this out, we need to know how much each item in the shopping
list costs. Let’s assume that this information is computed in a module
called shop, defined as follows:
| shop.erl | |
| | -module(shop). |
| | -export([cost/1]). |
| | |
| | cost(oranges) -> 5; |
| | cost(newspaper) -> 8; |
| | cost(apples) -> 2; |
| | cost(pears) -> 9; |
| | cost(milk) -> 7. |
The function cost/1 is made up from five
clauses. The head of each clause contains a
pattern (in this case a very simple pattern that is just an
atom). When we evaluate shop:cost(X), then the
system will try to match X against ...
Read now
Unlock full access