Skip to content

Instantly share code, notes, and snippets.

@solidnerd
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solidnerd/26432de3e0171c5ad5f6 to your computer and use it in GitHub Desktop.
Save solidnerd/26432de3e0171c5ad5f6 to your computer and use it in GitHub Desktop.
Lets go shopping in Erlang
% shop.erl
-module(shop).
-export([cost/1]).
cost(oranges) -> 5;
cost(newspaper) -> 8;
cost(apples) -> 2;
cost(pears) -> 9;
cost(milk) -> 7.
% shop1.erl
-module(shop1).
-export([total/1]).
total([ {What,N} | T]) -> shop:cost(What)*N + total(T);
total([]) -> 0.
% See the result for
shop1:total([{oranges,5},{milk,3}] .
% First 5 * 5 + total([{milk,3}]
% Second 25 + 21 + total([])
% Third 25 + 21 + 0
% Result 46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment