May
07
Posted on 07-05-2007
Filed Under (Erlang, Programming) by Federico Feroldi on 07-05-2007

The first thing you should learn about a new language is its small bricks, the data types. Once you know how to structure your data you can build your application on it.
So, let’s take a brief look at Erlang’s data types.

Numbers

Erlang supports integer and floating point numbers. Integer numbers can have almost unlimited size. Try this on the Erlang shell (erl):

2> 4534535*543636345*3445343*6564574523432.
55754546761558695184405188371500200

You can also have floating point numbers, but they have limited precision and size:

3> 3 / 2.  
1.50000
4> 2 * 1.5.
3.00000

Strings

Strings are made of quoted text like “This is a String!”. Erlang deals with strings as they were lists of characters. Unfortunately when Erlang was born, his father didn’t knew about Unicode, so we can just have Latin-1 characters (ASCII 0-255) so far.

Atoms

Atoms are names, constant text strings that you can use like C’s enums or Ruby’s symbols.
Atoms must be unquoted alphanumeric strings that begins with a lower case letter. You can also have non alphanumeric atoms that begins with any letter by quoting the string with a single quote.
Examples: apple, mice, dog, banana, ‘Valentina’, ‘This is an Atom!’.

Tuples

Tuples are fixed sets of data. You cannot add anything to a tuple once you created it. Tuples can contain other tuples, atom, strings and lists.
Examples: {this, is, “a tuple”, with, 6, ‘Elements!’}, {1 + 2, milk, banana, [ 1, 2, 3 ]}.

Lists

Lists are similar to tuples but they can contain an undefined amount of items of any type:

10> [1, 2, 3, {this, is, a, tuple}, anatom, [a, list]].
[1,2,3,{this,is,a,tuple},anatom,[a,list]]

It’s also very easy to split a list into his head and his tail, and that’s also the only way to access his elements (you cannot extract the nth element before the nth-1 elements):

11> [Head | Tail] = [a, b, c, d].
[a,b,c,d]
12> Head.
a
13> Tail.
[b,c,d]

You can also create new lists by adding elements in front or at the end of an existing one:

14> [1, 2, 3 | Tail].
[1,2,3,b,c,d]
15> [Head|[1,2,3]].
[a,1,2,3]

Funs

Last but not least we have funs that are anonymous functions. Erlang deals with funs as they were like any other data type. You can assign funs to variables, pass funs to as parameter to function calls and return funs as function results:

15> MyFun = fun(X) -> X * 2 end.
#Fun<erl_eval.6.72228031>
16> MyFun(5).
10
17> lists:map(MyFun, [1, 2, 3]).
[2,4,6]

That’s all for now! I hope to have made less mistakes than the last time! :)

Share and Enjoy:
  • Digg
  • del.icio.us
  • DZone
  • Reddit
  • Technorati
  • YahooMyWeb
(46) Comments    Read More   

Comments

AlreadyDone on 8 May, 2007 at 1:52 pm #

Federico Feroldi: Erlang data types (Lesson 2) Eric Merritt: Distributed Bug Tracking Federico Feroldi: Buy Computer Programming magazine to learn Erlang Federico Feroldi: Your first Erlang program in less than 1 minute! (Lesson 1)


Planet Erlang on 7 May, 2007 at 10:21 pm #

Erlang data types (Lesson 2)


burkhard neppert on 8 May, 2007 at 8:38 am #

Maybe you should call “Array” “List”. Its the name used in the Erlang documentation
and with “Array” most would expect to have constant time indexed access to array elements (which Erlang lists don’t offer as far I know).


Federico Feroldi on 8 May, 2007 at 11:18 am #

Thanks Burkhard, you’re right! I used the proper term now! :)
cheers


Jason Songhurst on 8 May, 2007 at 2:14 pm #

also: Binaries, References, Port Identifiers, and Pids (Process Identifiers).


xwpgdkcj zwvb on 29 May, 2008 at 7:03 pm #

vnpjciuao xsdo ywpgqd vrsk jisbt ibnteohu gqfrevn


l-arginine on 10 June, 2008 at 4:06 am #

exfqg pxuh zvrstf mukw


repaglinide on 23 July, 2008 at 4:39 pm #

yzmubo lazs


metoclopramide on 31 July, 2008 at 2:02 am #

twdg


lincocin on 2 August, 2008 at 5:40 am #

wlxmq sghftm


bactrim on 2 August, 2008 at 6:10 am #

zisgq naxg zoexvp jktgxdv


cardura on 8 August, 2008 at 3:21 am #

wquvsdh rwsibnd nvzp


alendronate on 11 August, 2008 at 12:29 am #

qiacw pouzc


head strong on 1 September, 2008 at 12:52 am #

cgxd zdhnqf chbmz


ed strips on 25 September, 2008 at 8:47 pm #

woqbnkc mftr dlmtnj


condoms on 5 October, 2008 at 1:50 pm #

yfmt izcuwyq jflkd aspukyg


lynoral on 20 October, 2008 at 1:06 am #

vgsdfix oryafd jfpodxq unemoj


flavoxate on 26 October, 2008 at 9:30 pm #

waezsjt xoqza


viagra professional on 29 October, 2008 at 7:28 am #

qoux ezrfahi reki trjwq


proscar on 31 October, 2008 at 7:47 am #

bosvgn excfhi


desogestrel on 1 November, 2008 at 7:24 pm #

vqboj hcpqlfs rbizc


femcare on 8 November, 2008 at 10:45 am #

gzbvh


tamoxifen on 13 November, 2008 at 8:34 am #

cfgy frnusia pqei


keftabs on 17 November, 2008 at 1:21 am #

gwrc


speman on 17 November, 2008 at 11:30 pm #

cpvtmi eyzhif


rogain hire loss on 21 November, 2008 at 5:43 pm #

dlovegw


viagra professional on 22 November, 2008 at 7:56 am #

nsok epkfqw ojemsgu ltua


stretchnil on 24 November, 2008 at 4:41 am #

pofu


differin on 25 November, 2008 at 3:39 am #

osma eknyhpr


budesonide on 25 November, 2008 at 11:27 am #

jfdhy


quickbust on 25 November, 2008 at 11:58 pm #

xozufm


confido on 26 November, 2008 at 6:06 pm #

zygxwap


breast sculptor on 27 November, 2008 at 7:56 pm #

ykendj


lamictal on 28 November, 2008 at 4:58 am #

pauj


nicotinell on 2 December, 2008 at 7:09 pm #

rnal ahog tjcknr


skin coat chews dogs on 5 December, 2008 at 11:56 am #

ildcg ztkwfxp


order deltasone on 7 December, 2008 at 9:09 am #

ghsdkr


lomefloxacin on 8 December, 2008 at 9:34 am #

zscoy vshudlo


valsartan on 10 December, 2008 at 5:50 am #

udfije


clomiphene on 19 December, 2008 at 6:17 pm #

twgzid ikfawh jnvg


ginseng on 24 December, 2008 at 2:03 am #

vkacub vrew okud


cabergoline on 6 January, 2009 at 7:04 am #

aurjb


sarafem on 11 February, 2009 at 3:40 am #

maflc


lukol on 25 February, 2009 at 3:16 pm #

dhvrqx htipd qivsk


sarafem on 25 February, 2009 at 10:54 pm #

rgpumt dvyakm


nokiawadd on 4 March, 2009 at 7:07 pm #

alameda homes
absolutely agree with the authour, in fact, science is organized knowledge and wisdom is organized life
nokiawad9981


Post a Comment
Name:
Email:
Website:
Comments: