Modules

06/05/2009

All the functions will be stored in modules, and they must be compiled before the code can be run.
When you write a module its extension will be .erl but when it will be compiled it changes in .beam

ThingsToBuy = [ {iphone, 100},
{mac, 1500},
{mouse, 20},
{keyboard, 30}].

This is the list of items we want to buy, how much money will we spend to buy all these items? Let’s create a module to calculate it!

-module(cost).
-export([total/1]).

total([{_, Money}|T]) -> Money + total(T);
total([]) -> 0.

(save this code in cost.erl)

Now in shell you have to compile your module writing:

c(cost).

Now you can calculate the sum of all these items doing:

cost:total(ThingsToBuy).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: