{"id":347,"date":"2011-03-06T12:17:57","date_gmt":"2011-03-06T11:17:57","guid":{"rendered":"http:\/\/trigonakis.com\/blog\/?p=347"},"modified":"2011-03-14T22:44:06","modified_gmt":"2011-03-14T21:44:06","slug":"introduction-to-erlang-basic-types-1","status":"publish","type":"post","link":"http:\/\/trigonakis.com\/blog\/2011\/03\/06\/introduction-to-erlang-basic-types-1\/","title":{"rendered":"Introduction to Erlang : Basic Types (1\/2)"},"content":{"rendered":"<div class=\"seriesmeta\">This entry is part 4 of 16 in the series <a href=\"http:\/\/trigonakis.com\/blog\/series\/introduction-to-erlang\/\" class=\"series-57\" title=\"Introduction to Erlang\">Introduction to Erlang<\/a><\/div><h3>Command Terminator<\/h3>\n<p>Erlang uses a simple dot (<strong>.<\/strong>) as the command terminator. Consequently every correct Erlang statement should terminate with a dot.<\/p>\n<pre lang=\"erlang\">\r\n1> 1\r\n1> 2.\r\n* 2: syntax error before: 2\r\n1> 1.\r\n1\r\n2> 2.\r\n2\r\n<\/pre>\n<h3>The Most Basic Types<\/h3>\n<p>I will introduce the data types of Erlang and the basic operations on them by example. Most of the material presented today will look familiar to other programming languages. The next post will present some more sophisticated data types.<br \/>\n<!--more--><\/p>\n<h4>Integers<\/h4>\n<p>The most foundamental type is, as usual, the <strong>integer<\/strong>.<\/p>\n<h5>Operations<\/h5>\n<pre lang=\"erlang\">\r\n1> I = 17. % binding\r\n17\r\n2> I = 17. % pattern matching without binding\r\n17\r\n3> I = 18. % unsuccessful\r\n** exception error: no match of right hand side value 18\r\n4> I + 2. % addition\r\n19\r\n5> I - 4. % substraction\r\n13\r\n6> I * 4. % multiplication\r\n68\r\n7> I div 4. % integer division\r\n4\r\n8> I \/ 4. % division\r\n4.25\r\n9> I rem 4. % integer remainder\r\n1\r\n10> I == 4. % equality\r\nfalse\r\n11> I =\/= 4. % unequality\r\ntrue\r\n12> I > 4.  % greater than\r\ntrue\r\n13> I >= 4. % greater-equal\r\ntrue\r\n14> I < 4. % lower than\r\nfalse\r\n15> I =< 4. % lower-equal\r\nfalse\r\n16> is_integer(I). % is integer ?\r\ntrue\r\n17> not(is_integer(I)). % is not integer ?\r\nfalse\r\n18> is_integer(3.3).\r\nfalse\r\n19> 2#10000. % integer in the form base#value\r\n16\r\n20> 4#100.  \r\n16\r\n21> 8#20. \r\n16\r\n22> 10#16.\r\n16\r\n23> 16#1.\r\n<\/pre>\n<p><!--nextpage--><\/p>\n<h5>Characters<\/h5>\n<p>The characters in Erlang are represented as integers (the <i>ASCII<\/i> value):<\/p>\n<pre lang=\"erlang\">\r\n1> $C. \r\n67\r\n2> $\\n.\r\n10\r\n<\/pre>\n<h4>Floats<\/h4>\n<p>Again, nothing special:<\/p>\n<pre lang=\"erlang\">\r\n1> F = 3.14.\r\n3.14\r\n2> F + 2.\r\n5.140000000000001 % lack of precision\r\n3> is_float(F).\r\ntrue\r\n4> 13.1317e3.\r\n13131.7\r\n...\r\n<\/pre>\n<h4>Atoms<\/h4>\n<p>Another Prolog&#8217;s influence is the <i>atom<\/i> data type. Atoms are literals, constants with names. Their meaning is usually defined by the programmer. For example, Erlang itself uses the atoms <code>true<\/code> and <code>false<\/code> with the semantcis that every programmer is used to. Note that there is not a <code>Boolean type; <\/code><code>true<\/code> and <code>false<\/code> are simple atoms. Every character sequencre generated by the <code>[a-z]([a-z0-9_@])*<\/code> regular expression, or enclosed in single quotes (<strong>&#8216;<\/strong>) is an <code>atom<\/code>.<\/p>\n<pre lang=\"erlang\">\r\n1> atom.\r\natom\r\n2> atom@123.\r\natom@123\r\n3> a_tom@123.\r\na_tom@123\r\n4> wr ong.   \r\n* 1: syntax error before: ong\r\n4> 'this is not wr ong'.\r\n'this is not wr ong'\r\n5> '!!~~>>'.\r\n'!!~~>>'\r\n<\/pre>\n<h3>Next<\/h3>\n<p>In the next post I will present some more &#8220;elaborate&#8221; Erlang data types. (tuples, lists, strings, functions)<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"seriesmeta\">This entry is part 4 of 16 in the series <a href=\"http:\/\/trigonakis.com\/blog\/series\/introduction-to-erlang\/\" class=\"series-57\" title=\"Introduction to Erlang\">Introduction to Erlang<\/a><\/div><p>Command Terminator Erlang uses a simple dot (.) as the command terminator. Consequently every correct Erlang statement should terminate with a dot. 1> 1 1> 2. * 2: syntax error before: 2 1> 1. 1 2> 2. 2 The Most Basic Types I will introduce the data types of Erlang and the basic operations on [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[40,51,28],"tags":[52,26,42],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1ouW6-5B","_links":{"self":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/347"}],"collection":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/comments?post=347"}],"version-history":[{"count":18,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/347\/revisions"}],"predecessor-version":[{"id":416,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/347\/revisions\/416"}],"wp:attachment":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/media?parent=347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/categories?post=347"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/tags?post=347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}