17.01.2015 Views

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

22<br />

User switch comm<strong>and</strong><br />

--> j<br />

1 {shell,start,[<strong>in</strong>it]}<br />

2* {shell,start,[]}<br />

--> k 1<br />

--> j<br />

2* {shell,start,[]}<br />

--> c<br />

When you do this sort of th<strong>in</strong>g, just be very careful about which job it is you’re kill<strong>in</strong>g, <strong>in</strong><br />

case you have several th<strong>in</strong>gs <strong>in</strong> progress <strong>in</strong> different jobs. When you kill a job, all the history,<br />

previous results <strong>and</strong> other th<strong>in</strong>gs associated with that shell job will disappear. To keep better<br />

track of jobs, you can specify a name when you start a new job, that will show up <strong>in</strong> the list:<br />

--> s important<br />

--> j<br />

2 {shell,start,[]}<br />

3* {important,start,[]}<br />

--> c<br />

We will see more of the Ctrl-G menu <strong>in</strong> chapter 8 when we talk about distributed <strong>Erlang</strong><br />

<strong>and</strong> how to use remote shells. This is as simple as it is powerful, <strong>and</strong> is the s<strong>in</strong>gle most<br />

important tool for remote controll<strong>in</strong>g <strong>and</strong> debugg<strong>in</strong>g production systems.<br />

Now that you have a feel for how to work <strong>in</strong> the <strong>Erlang</strong> console, it is time to start play<strong>in</strong>g<br />

around with the actual programm<strong>in</strong>g language.<br />

2.2 – Data types <strong>in</strong> <strong>Erlang</strong><br />

Underst<strong>and</strong><strong>in</strong>g basic data representation conventions is an essential part of learn<strong>in</strong>g any<br />

programm<strong>in</strong>g language. <strong>Erlang</strong>’s built-<strong>in</strong> data types are straightforward <strong>and</strong> relatively few,<br />

but you can achieve quite a lot with them. Data <strong>in</strong> <strong>Erlang</strong> is usually referred to as “terms”.<br />

Do try enter<strong>in</strong>g some examples of terms while you read this section. (Don’t forget to add a<br />

period before you press return.) Let’s start with the simplest ones.<br />

2.2.1 – Numbers <strong>and</strong> arithmetic<br />

<strong>Erlang</strong> has two numerical data types: <strong>in</strong>tegers <strong>and</strong> float<strong>in</strong>g-po<strong>in</strong>t numbers (“floats”).<br />

Conversion is done automatically by most of the arithmetic operations, so you don’t usually<br />

need to do any explicit type coercion.<br />

INTEGERS<br />

Integers <strong>in</strong> <strong>Erlang</strong> can be of arbitrary size. If they are small enough, they are represented <strong>in</strong><br />

memory by a s<strong>in</strong>gle mach<strong>in</strong>e word; if they get larger (so-called “bignums”), the necessary<br />

space is allocated automatically. This is completely transparent to the programmer, <strong>and</strong><br />

means that you never need to worry about truncation or wrap-around effects <strong>in</strong> arithmetic—<br />

those th<strong>in</strong>gs simply cannot happen.<br />

©Mann<strong>in</strong>g Publications Co. Please post comments or corrections to the Author Onl<strong>in</strong>e forum:<br />

http://www.mann<strong>in</strong>g-s<strong>and</strong>box.com/forum.jspaforumID=454

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!