{"id":519,"date":"2011-04-13T08:49:55","date_gmt":"2011-04-13T07:49:55","guid":{"rendered":"http:\/\/trigonakis.com\/blog\/?p=519"},"modified":"2011-04-19T15:48:02","modified_gmt":"2011-04-19T14:48:02","slug":"introduction-to-erlang-bifs-predefined-modules","status":"publish","type":"post","link":"http:\/\/trigonakis.com\/blog\/2011\/04\/13\/introduction-to-erlang-bifs-predefined-modules\/","title":{"rendered":"Introduction to Erlang : BIFs &#038; Predefined Modules"},"content":{"rendered":"<div class=\"seriesmeta\">This entry is part 11 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>Built-in Functions (BIFs)<\/h3>\n<p>Erlang&#8217;s <strong>Built-in Functions<\/strong> (shorthand <strong>BIF<\/strong>s) are commonly used functions that are intergrated into the Erlang&#8217;s <acronym title=\"Virtual Machine\">VM<\/acronym> for performance reasons. Most of them belong to the <code>erlang<\/code> module, but there are some in other modules, such as <code>lists<\/code>.<\/p>\n<p>The BIFs can be separated to <i>standard<\/i> and <i>non-standard<\/i>. The standard ones are auto-imported; they can be called without the use of the module name prefix (remember the effect of the <code>-import(...)<\/code> directive). On the other hand, the non-standard ones have to be called following the normal <code>module:function(...)<\/code> convension. In the <code>erlang<\/code> module&#8217;s man pages (<a href=\"http:\/\/www.erlang.org\/doc\/man\/erlang.html\" target=\"_new\">here<\/a>) the distinction between standard and non-standard is visible by the lack or existence of the <code>erlang<\/code> (module&#8217;s name) prefix.<\/p>\n<h4>elrang<\/h4>\n<h5>abs\/1<\/h5>\n<p>Arithmetic absolut value of an integer or float.<\/p>\n<h5>erlang:append_element\/2<\/h5>\n<p>Appends an element to a tuple.<\/p>\n<h5>apply\/2|3<\/h5>\n<p>Calls the function passed as a parameter.<\/p>\n<h5>atom_to_list\/1<\/h5>\n<p>Returns a string which corresponds to the text representation of Atom.<br \/>\n<!--more--><\/p>\n<h5>date\/0<\/h5>\n<p>Returns the date in the <code>{Year, Month, Day}<\/code> format.<\/p>\n<h5>element\/2<\/h5>\n<p>Called as <code>element(N, Tuple)<\/code>. Returns the <code>N<\/code>th of the <code>Tuple<\/code>.<\/p>\n<h5>error\/1|2<\/h5>\n<p>Stops the execution with an error Reason.<\/p>\n<h5>exit\/1|2<\/h5>\n<p>Stops the execution with an exit Reason.<\/p>\n<h5>float\/1<\/h5>\n<p>Converts the number to float.<\/p>\n<h5>float_to_list\/1<\/h5>\n<p>Returns a string which corresponds to the text representation of Float.<\/p>\n<h5>hd\/1<\/h5>\n<p>Returns the head of a list (the first element).<\/p>\n<h5>integer_to_list\/1|2<\/h5>\n<p>Returns a string which corresponds to the text representation of Integer.<\/p>\n<h5>is_atom\/1<\/h5>\n<p>Returns true if Term is an atom; otherwise returns false.<\/p>\n<h5>is_boolean\/1<\/h5>\n<p>Returns true if Term is either the atom true or the atom false (i.e. a boolean); otherwise returns false.<\/p>\n<h5>is_float\/1<\/h5>\n<p>Returns true if Term is a floating point number; otherwise returns false.<\/p>\n<h5>is_integer\/1<\/h5>\n<p>Returns true if Term is an integer; otherwise returns false.<\/p>\n<h5>is_list\/1<\/h5>\n<p>Returns true if Term is a list with zero or more elements; otherwise returns false.<\/p>\n<h5>is_number\/1<\/h5>\n<p>Returns true if Term is either an integer or a floating point number; otherwise returns false.<\/p>\n<h5>is_tuple\/1<\/h5>\n<p>Returns true if Term is a tuple; otherwise returns false.<\/p>\n<h5>length\/1<\/h5>\n<p>Returns the length of a list.<\/p>\n<h5>list_to_atom\/1<\/h5>\n<p>Returns the atom whose text representation is String.<\/p>\n<h5>list_to_existing_atom\/1<\/h5>\n<p>Returns the atom whose text representation is String, but only if there already exists such atom.<\/p>\n<p>Failure: badarg if there does not already exist an atom whose text representation is String.<\/p>\n<pre lang=\"erlang\">\r\n2> erlang:loaded().\r\n[otp_internal,lib,shell_default,erl_internal,edlin_expand,\r\n sets,ordsets,erl_lint,unicode,io,erl_scan,erl_parse,epp,\r\n filelib,ram_file,beam_lib,file_io_server,orddict,erl_eval,\r\n file,c,error_logger_tty_h,kernel_config,shell,io_lib_format,\r\n proplists,io_lib,edlin,group|...]\r\n3> list_to_existing_atom(\"lib\").\r\nlib\r\n4> list_to_existing_atom(\"lib11\").\r\n** exception error: bad argument\r\n     in function  list_to_existing_atom\/1\r\n        called as list_to_existing_atom(\"lib11\")\r\n<\/pre>\n<h5>list_to_float\/1<\/h5>\n<p>Returns the float whose text representation is String.<\/p>\n<h5>list_to_integer\/1|2<\/h5>\n<p>Returns an integer whose text representation is String.<\/p>\n<h5>list_to_tuple\/1<\/h5>\n<p>Returns a tuple which corresponds to List. List can contain any Erlang terms.<\/p>\n<h5>erlang:loaded\/0<\/h5>\n<p>Returns a list of all loaded Erlang modules.<\/p>\n<h5>erlang:localtime\/0<\/h5>\n<p>Returns the current local time and date in the format <code>{{Year, Month, Date}, {Hour, Minute, Second}}<\/code>.<\/p>\n<h5>max\/2<\/h5>\n<p>Returns the maximum between two terms, or the first one if equal.<\/p>\n<h5>min\/2<\/h5>\n<p>Returns the minimum between two terms, or the first one if equal.<\/p>\n<h5>now\/0<\/h5>\n<p>Returns the tuple <code>{MegaSecs, Secs, MicroSecs}<\/code> which is the elapsed time since 00:00 GMT, January 1, 1970 (zero hour).<\/p>\n<h5>round\/1<\/h5>\n<p>Returns the integer resulting by rounding the input.<\/p>\n<h5>self\/0<\/h5>\n<p>Return the process identifier (Pid) of the current process. This function will be used extensively in the next post and what is a Pid will be explained in more detail.<\/p>\n<h5>erlang:send\/2<\/h5>\n<p>Called as <code>erlang:send(Dest, Msg)<\/code>. Sends message <code>Msg<\/code> to <code>Dest<\/code>. This is equivalent with <code>Dest ! Msg<\/code>. We will see the details of message passing in an upcoming post.<\/p>\n<h5>setelement\/3<\/h5>\n<p>Returns a new tuple where the value of the specified element is set to the one provided.<\/p>\n<h5>size\/1<\/h5>\n<p>Returns the size of the provided tuple or binary.<\/p>\n<h5>spawn\/1|2|3|4<\/h5>\n<p>Creates a new process and returns its Pid. It is one of the most important and used functions in Erlang. There will be a dedicated post about processes.<\/p>\n<h5>throw\/1<\/h5>\n<p>A non-local return from a function. If evaluated within a catch, catch will return the value Any (called as <code>throw(Any)<\/code>).<\/p>\n<pre lang=\"erlang\">\r\n7> throw(my_exception).\r\n** exception throw: my_exception\r\n8> catch throw(my_exception).\r\nmy_exception\r\n<\/pre>\n<h5>time\/0<\/h5>\n<p>Returns the local time in <code>{Hours, Minutes, Seconds}<\/code> format.<\/p>\n<h5>tl\/1\/<\/h5>\n<p>Returns the tail of a list (the list without its head element).<\/p>\n<h5>trunc\/1<\/h5>\n<p>Returns an integer by truncating the provided number.<\/p>\n<h5>tuple_size\/1<\/h5>\n<p>Returns the size of the provided tuple.<\/p>\n<h5>tuple_to_list\/1<\/h5>\n<p>Returns a list which corresponds to Tuple. Tuple may contain any Erlang terms.<\/p>\n<h3>Preexisting Modules<\/h3>\n<p>Erlang comes with tons of predefined general purpose or specialized modules. In the majority of the cases, if Erlang provides a function with the functionality you need, you would prefer to use it instead of reimplementing it for three main reasons: (1) why reinventing the wheel, (2) performance, (3) difficult to program.<\/p>\n<p>I will present some of the modules that I consider the most important\/useful. A full list of the existing Erlang&#8217;s (Erlang\/OTP to be precise) modules can be found <a href=\"http:\/\/erlang.org\/doc\/man\/\" target=\"_new\">here<\/a>.<\/p>\n<h4>debugger<\/h4>\n<p>This is the module used to start the Erlang debugger for debugging and testing Erlang programs. Details <a href=\"http:\/\/erlang.org\/doc\/man\/debugger.html\" target=\"_new\">here<\/a>. There will be a post about debugging in the future, so do not worry for now.<\/p>\n<h5>start\/0|1|2<\/h5>\n<p>Starts the debugger.<\/p>\n<h4>file<\/h4>\n<p>Functions used to read and manipulate files. Details <a href=\"http:\/\/erlang.org\/doc\/man\/file.html\" target=\"_new\">here<\/a>.<\/p>\n<h5>open\/2<\/h5>\n<p>Opens a file.<\/p>\n<h5>close\/1<\/h5>\n<p>Closes an opened file.<\/p>\n<h5>read\/2, read_file\/1, read_line\/1, pread\/2|3<\/h5>\n<p>Reading from a file.<\/p>\n<h5>write\/2, write_file\/2|3, pwrite\/2|3<\/h5>\n<p>Writing to a file.<\/p>\n<h5>position\/2<\/h5>\n<p>Moves the current pointer in a file.<\/p>\n<h5>rename\/2<\/h5>\n<p>Renames a file.<\/p>\n<h5>copy\/2|3<\/h5>\n<p>Copies the contents of a file to another.<\/p>\n<h5>list_dir\/1, make_dir\/1, delete_dir\/1<\/h5>\n<p>List, create, and delete directories.<\/p>\n<h4>io<\/h4>\n<p>Functions related to input\/output. Details <a href=\"http:\/\/erlang.org\/doc\/man\/io.html\" target=\"_new\">here<\/a>.<\/p>\n<h5>format\/1|2|3<\/h5>\n<p>Write formatted output to the standard output. (similar to <code>printf<\/code> in C)<\/p>\n<pre lang=\"erlang\">\r\n11> io:format(\"Hello World~n\").        \r\nHello World\r\nok\r\n<\/pre>\n<h5>read\/1|2, read_line\/1|2, get_chars\/2|3<\/h5>\n<p>Reads a term, a line, or a number of chars from the standard input.<\/p>\n<h4>lists<\/h4>\n<p>Functions related to lists. Details <a href=\"http:\/\/erlang.org\/doc\/man\/lists.html\" target=\"_new\">here<\/a>. The next post will be focusing on lists and the <code>lists<\/code> module, so I do not present them here.<\/p>\n<h4>math<\/h4>\n<p>Mathematical functions (<code>sin\/1, cos\/1, tan\/1, exp\/1, log\/1, pow\/2, sqrt\/1<\/code>, etc). Details <a href=\"http:\/\/erlang.org\/doc\/man\/math.html\" target=\"_new\">here<\/a>.<\/p>\n<h4>pman<\/h4>\n<p>A graphical process manager tool used to inspect the Erlang processes executing either locally or on remote nodes. It is also possible to trace events in the individual processes. Details <a href=\"http:\/\/erlang.org\/doc\/man\/pman.html\" target=\"_new\">here<\/a>. We will use this module in the post about debugging.<\/p>\n<h5>start\/0|1<\/h5>\n<p>Starts the process manager.<\/p>\n<h5>proc\/1|3<\/h5>\n<p>Starts a process manager for a specific process.<\/p>\n<h4>string<\/h4>\n<p>Functions related to strings. Details <a href=\"http:\/\/erlang.org\/doc\/man\/string.html\" target=\"_new\">here<\/a>.<\/p>\n<h5>len\/1<\/h5>\n<p>Returns the number of characters of a string.<\/p>\n<h5>equal\/2<\/h5>\n<p>Test two strings for equality.<\/p>\n<h5>concat\/2<\/h5>\n<p>Concatenates two strings and returns the results.<\/p>\n<h5>str\/2, rstr\/2<\/h5>\n<p>Returns the position where the first\/last occurrence of a substring begins in a string. 0 is returned if substring does not exist in the string.<\/p>\n<h5>substr\/2|3<\/h5>\n<p>Returns the substring of a string.<\/p>\n<h5>to_lower\/1, to_upper\/1<\/h5>\n<p>Case convertion.<\/p>\n<h4>timer<\/h4>\n<p>This module provides useful functions related to time. Details <a href=\"http:\/\/erlang.org\/doc\/man\/timer.html\" target=\"_new\">here<\/a>.<\/p>\n<h5>apply_after\/4<\/h5>\n<p>Apply a function after a given time.<\/p>\n<h5>send_after\/2|3<\/h5>\n<p>Send a message to a process after a given time.<\/p>\n<h5>tc\/2|3<\/h5>\n<p>Returns <code>{Time, Value}<\/code>, where <code>Time<\/code> is the microseconds that the execution took and <code>Value<\/code> is the result of the applied function.<\/p>\n<pre lang=\"erlang\">\r\n14> timer:tc(erlang, length, [[1, 2, 3, 4, 5, 6, 7]]).\r\n{6,7} %r esult 7 in 6 microseconds\r\n<\/pre>\n<h5>now_diff\/2<\/h5>\n<p>Returns the difference between two different &#8220;now values&#8221; (got by calling <code>now()<\/code>) in microseconds.<\/p>\n<pre lang=\"erlang\">\r\n15> Now_old = now().\r\n{1302,680891,260499}\r\n16> Now_new = now().\r\n{1302,680895,780496}\r\n17> timer:now_diff(Now_new, Now_old).\r\n4519997\r\n<\/pre>\n<h3>Next<\/h3>\n<p>The next post will be dedicated to <strong>lists and the lists module<\/strong>. <\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"seriesmeta\">This entry is part 11 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>Built-in Functions (BIFs) Erlang&#8217;s Built-in Functions (shorthand BIFs) are commonly used functions that are intergrated into the Erlang&#8217;s VM for performance reasons. Most of them belong to the erlang module, but there are some in other modules, such as lists. The BIFs can be separated to standard and non-standard. The standard ones are auto-imported; they [&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":[26,55,42],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1ouW6-8n","_links":{"self":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/519"}],"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=519"}],"version-history":[{"count":17,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/519\/revisions"}],"predecessor-version":[{"id":549,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/519\/revisions\/549"}],"wp:attachment":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/media?parent=519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/categories?post=519"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/tags?post=519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}