Stratus3D

Software Engineering, Web Development and 3D Design

Everything You Need to Know About Erlang's Magic _ Variable

I had someone ask me what happened to values assigned to the underscore variable in Erlang. I realized I didn’t have a clear understand of the way variables beginning with an underscore handled. I decided to re-read the Erlang documentation on variables.

To summarizing what the documentation says, all variables could be put into three groups:

  • Normal variables
  • Normal variables prefixed with with an underscore
  • The anonymous variable (a single underscore)

###Erlang Variables All types of variables must start with either an uppercase letter or an underscore. They may contain alphanumeric characters, the underscore and the @ symbol. All variables are bound via Pattern Matching. At runtime no variables are special except for the anonymous variable.

##Normal variables Normal variables start with an uppercase letter. Pattern matching and all the compile time warnings work normally. Example:

1> {First, Second} = {foo, bar}.
{foo, bar}
2> First.
foo
3> Second.
bar

##Normal variables prefixed with an underscore When a variable is prefixed with an underscore pattern matching and everything at runtime works like a normally. Values are still bound as they normally would and patterns such as this fail like they normally would. {_Val,_Val} = {foo,bar} fails because the first and second items in the tuple are different. The only difference is at compile time. Normally the compiler generates a warning when a value is bound to a variable and the variable is not later used. Often an important value is returned from a function call but the value is unused. By prefixing the variable with an underscore, the variable retains it’s useful name and does not generate any compile time warnings. Example:

1> {_First, _Second} = {foo, bar}.
{foo, bar}
2> _First.
foo
3> _Second.
bar

##The anonymous variable The anonymous variable is denoted by a single underscore (_). The anonymous variable is used when a variable is required but the value needs to be ignored. When a value is assigned to the anonymous variable the value is never bound to the variable. Since the value is never bound it can be used multiple times in a pattern and each time it is allowed to match a different value. Example:

1> {_, _} = {foo, bar}.
{foo, bar}
2> _.
* 1: variable '_' is unbound 
4> {_, Second} = {foo, bar}.
{foo, bar}
5> _.
* 1: variable '_' is unbound 
6> Second.
bar

For more information check out the Erlang documentation on variables

Install Erlang 16 on Mac OSX 10.9 With Kerl

I recently was working on a project that required me to connect to an ODBC database. I hadn’t ever worked with ODBC before, but I saw Erlang had an application for interacting with ODBC databases. I fired up the Erlang console and typed odbc:module_info(). but the function was undefined. After double checking everything and trying to start the odbc application I realized that the odbc module wasn’t part of my Erlang installation. I tried reinstalling Erlang but that didn’t fix the problem. Turns out Erlang’s ODBC application relies on unixodbc, which isn’t included on OSX anymore. Here is how you get Erlang R16B03-1 on Mac OSX 10.9.5:

I use Kerl for building and installing Erlang versions. It’s a relatively simple tool that I have found to be very helpful. I am assuming you are using Kerl in the steps below. If you aren’t using Kerl it should still be possible to build Erlang with ODBC support on OSX, but you will need to alter these commands appropriately. I also assume you are using brew.

  1. Brew does all the hard work for us. Run $ brew install freetds. Make sure freetds was installed successfully and then try running $ tsql. If tsql exists it should print out some help information.
  2. We can use brew install unixodbc. Run $ brew install unixodbc. Make sure it installs successfully and then verify by running $ isql. isql should print out some help information.
  3. Install Erlang with Kerl. Before you build Erlang open up ~/.kerlrc (or create it if it doesn’t exist) and make sure it looks like this:

         KERL_CONFIGURE_OPTIONS="--enable-darwin-64bit --disable-hipe --enable-vm-probes --with-dynamic-trace=dtrace --disable-native-libs --enable-kernel-poll --enable-threads --enable-smp-support --with-wx"
    

    Also, one more thing before running the build command - make sure $GREP_OPTIONS is set to ''. On my system I had customized it and it was causing the build to crash:

         GREP_OPTIONS=''
    

    Finally, you should be able to run the build command. Here I chose erlang_16 as the build name:

         kerl build R16B031 erlang_16
    
  4. If all goes well you should be able to install the build by running kerl install erlang_16 <dir> where <dir> is the directory you want to install the build in. You should now have an installation that includes the odbc application!

If this quick post wasn’t able to help you here are some other resources I found that helped me fix this issue:

Elixir Language

I have been doing Erlang and Elixir programming for the last ten months and I would love to share what I have been learning. It has been quite a change moving from Ruby to a functional language like Erlang. Most of the projects I am working on at work are written Erlang, but I have also been studying Elixir, which is a new language that is built on top the Erlang VM. Since it uses the Erlang VM, it inherits all the strengths of Erlang - concurrency, scalability, and fault tolerance. Elixir is a new language (first major release expected sometime in the next month) with a Ruby-like syntax. It focuses on providing better tooling and allows for much greater productivity than Erlang. If you are interested in building concurrent applications or want to learn functional programming I highly recommend you check it out - elixir-lang.org

I will also be giving a talk on Elixir at the Tampa Ruby Brigade on Oct. 16. It will be an introduction to Elixir followed by some demos. If you interested in learning more Elixir RSVP on meetup.com - www.meetup.com/tampa-rb.

I am very excited about Elixir. I think it has very bright future. I believe it very elegantly solves some scalability issues while also providing tooling (and ease of use) that is comparable to Ruby.

If you have any questions or comments I would love to hear from you.