{"id":375,"date":"2011-03-09T21:00:03","date_gmt":"2011-03-09T20:00:03","guid":{"rendered":"http:\/\/trigonakis.com\/blog\/?p=375"},"modified":"2011-04-07T16:14:49","modified_gmt":"2011-04-07T15:14:49","slug":"introduction-to-erlang-basic-types-2","status":"publish","type":"post","link":"http:\/\/trigonakis.com\/blog\/2011\/03\/09\/introduction-to-erlang-basic-types-2\/","title":{"rendered":"Introduction to Erlang : Basic Types (2\/2)"},"content":{"rendered":"<div class=\"seriesmeta\">This entry is part 5 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>More Data Types<\/h3>\n<p>Today we will see some more sophisticated Erlang&#8217;s data types; <code>tuple, list<\/code>, and <code>fun<\/code>.<\/p>\n<h3>Tuple<\/h3>\n<p>Tuple is a <i>compound<\/i> data type; it consists of <i>elements<\/i> of any data type. A tuple has the form:<\/p>\n<pre lang=\"erlang\">\r\n{Element1, Element2, ..., ElementN}\r\n<\/pre>\n<p>where <strong><code>N<\/code><\/strong> is called the <i>size<\/i> the tuple.<\/p>\n<pre lang=\"erlang\">\r\n1> {1,2}.\r\n{1,2}\r\n2> {true, {value, whatever}}.\r\n{true,{value,whatever}}\r\n3> {1, a, 1.1, {{{{4}}}}}.\r\n{1,a,1.1,{{{{4}}}}}\r\n4> is_tuple({}). % built-in function (BIF)\r\ntrue\r\n5> element(3, {1,2,3}). % the element at position 3\r\n3\r\n6> size({1,2,3}).% the size of the tuple\r\n3\r\n<\/pre>\n<p><!--more--><\/p>\n<h3>List<\/h3>\n<p>As in all functional programming language, <code>list<\/code> is one of the most used data type. Again, Erlang borrows the list syntax from Prolog. Because of their importance, we will later have a post (maybe 2) dedicated to lists.<\/p>\n<p>Lists are defined as <\/p>\n<pre lang=\"erlang\">\r\n[Element1, Element2, ..., ElementN]\r\n<\/pre>\n<p>where <strong><code>N<\/code><\/strong> is called the <i>length<\/i> of the list.<\/p>\n<pre lang=\"erlang\">\r\n1> [1,2,3].\r\n[1,2,3]\r\n2> [1,a,{1,2}, 3.14].\r\n[1,a,{1,2},3.14]\r\n3> []. % the empty list\r\n[]\r\n4> is_list([]).\r\ntrue\r\n5> [1, [2, [3, [a, b, {{{c}}}]]], d].\r\n[1,[2,[3,[a,b,{{{c}}}]]],d]\r\n6> List = [1,2,3], length(List).\r\n3\r\n<\/pre>\n<p>A list can be &#8220;recursively&#8221; defined as either the <i>empty list<\/i> (<code>[]<\/code>), or a compound structure consisting of the <strong>head<\/strong> (the first element of the list) and the <strong>tail<\/strong> (the rest of the list), which is a list.<br \/>\nOne of the most commonly used operator on lists is the <strong>|<\/strong>, which can be used to express the above definition as <code>[Head | Tail]<\/code>. Therefore:<\/p>\n<pre lang=\"erlang\">\r\n[] % is a list\r\n[E1 | []] == [E1] % is a list\r\n[E1 | [E2 | []]] == [E1, E2] % is a list\r\n[E1 | [E2 | [...]]] == [E1, E2, ...] % is a list\r\n<\/pre>\n<p>Notice that the <code>[E1, E2, ..., EN]<\/code> is a shorthand for <code>[E1 | [E2 | ... | [EN | []]...]]<\/code>.<\/p>\n<pre lang=\"erlang\">\r\n1> A = [1,2,3].\r\n[1,2,3]\r\n2> [B | C] = [1,2,3]. % pattern matching using |\r\n[1,2,3]\r\n3> B.\r\n1\r\n4> C.\r\n[2,3]\r\n5> [D | E] = [1].\r\n[1]\r\n6> D. \r\n1\r\n7> E.\r\n[]\r\n8> [F | G] = []. % doesn't pattern match with the empty list\r\n** exception error: no match of right hand side value []\r\n9> [] = [].\r\n[]\r\n10> [] == [].\r\ntrue\r\n<\/pre>\n<h4>Operations<\/h4>\n<pre lang=\"erlang\">\r\n1> A = [1,2,3].\r\n[1,2,3]\r\n2> [newhead | A].\r\n[newhead,1,2,3]\r\n3> [n1, n2 | A].     \r\n[n1,n2,1,2,3]\r\n4> B = [3,4,5].\r\n[3,4,5]\r\n5> A ++ B. % list concat\r\n[1,2,3,3,4,5]\r\n6> B ++ A.\r\n[3,4,5,1,2,3]\r\n7> A -- B. % list difference\r\n[1,2]\r\n8> [1,1,2,2,3,3] -- [1,2,2,3].\r\n[1,3]\r\n<\/pre>\n<p>As I wrote before, we will see much more staff about lists in later posts.<\/p>\n<h3>Fun(ction)<\/h3>\n<p>Erlang supports <i>higher-order functions<\/i>. In a nutshell, this means that functions can be assigned to variables, be passed as arguments to a function call, and be the return value of a function call (<a href=\"http:\/\/en.wikipedia.org\/wiki\/Higher_order_function\" target=\"_new\">Read more<\/a>). This is supported by the <code>fun<\/code> type in Erlang, which is a functional object.<\/p>\n<pre lang=\"erlang\">\r\n1> Same = fun(X) -> X end.\r\n#Fun<erl_eval .6.13229925>\r\n2> Same(abc).\r\nabc\r\n3> Double = fun(X) -> 2*X end.\r\n#Fun<\/erl_eval><erl_eval .6.13229925>\r\n4> Double(17).\r\n34\r\n5> Double(17.7).\r\n35.4\r\n6> Apply = fun(X, Fun) -> Fun(X) end.\r\n#Fun<\/erl_eval><erl_eval .12.113037538>\r\n7> Apply(33, Double).\r\n66\r\n8> Inc = fun(X) -> X+1 end, GiveInc = fun() -> Inc end.\r\n#Fun<\/erl_eval><erl_eval .20.67289768>\r\n9> GiveInc().\r\n#Fun<\/erl_eval><erl_eval .6.13229925>\r\n10> (GiveInc())(3).\r\n4\r\n<\/erl_eval><\/pre>\n<p>Don&#8217;t worry about the syntax of a function since we will see how to declare (anonymous) functions soon.<\/p>\n<h3>Other types<\/h3>\n<p>There are some other types, such as <code>binary, reference, Pid<\/code>, etc. I will explain them when needed.<\/p>\n<h3>Next<\/h3>\n<p>In the next post, we will see what is a <i>module<\/i> and how to create and compile one.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"seriesmeta\">This entry is part 5 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>More Data Types Today we will see some more sophisticated Erlang&#8217;s data types; tuple, list, and fun. Tuple Tuple is a compound data type; it consists of elements of any data type. A tuple has the form: {Element1, Element2, &#8230;, ElementN} where N is called the size the tuple. 1> {1,2}. {1,2} 2> {true, {value, [&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-63","_links":{"self":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/375"}],"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=375"}],"version-history":[{"count":15,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/375\/revisions"}],"predecessor-version":[{"id":515,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/375\/revisions\/515"}],"wp:attachment":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/media?parent=375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/categories?post=375"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/tags?post=375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}