Erlang, list comprehension syntax

I saw this code in Erlang:

[X-$0 || X<-someFun()]

In that line I found the -$0 syntax very useful.

I read the code and estimated what it means, but I'm not quite sure: is it split all numbers?

I'd like to see the explanation or man page of that syntax but I can't find it. Can anyone show me the right page?

Answers


What that code is doing is taking the output from someFun() (which needs to return a list), and for each element in the list it is assigning the element's value to the variable X and then subtracting the ASCII value of the character 0 from that value. The resulting list is then the value of that whole expression.

What it's doing, in practice (and I've written this code dozens of times myself), is assuming that someFun/0 is a function that returns a string with just digits in it, and then converting that string into a list of the digits. So, if someFun() returned "12345", the result of this list comprehension is [1, 2, 3, 4, 5].

If you're familiar with the concept of a map function (as in, MapReduce), then this should be sounding pretty familiar by now.

This wikibooks page looks like a good introduction to Erlang list comprehensions:

http://en.wikibooks.org/wiki/Erlang_Programming/List_Comprehensions

Joe Armstrong's book "Programming Erlang", from the Pragmatic Bookshelf, (http://pragprog.com/titles/jaerlang/programming-erlang) also covers list comprehensions really well (along with everything else Erlang related). Excellent book, highly recommended, etc.


The normal documentation page for list comprehensions is in the Erlang Reference Manual Section 6.22 and in Programming Examples Section 3.

Both of these are on the main documentation page (click on the Erlang Programming Folder on the left hand menu bar).

The Erlang documentation is fab - it's just its layout is a tad shonky...


Need Your Help

Enable antialiasing using Xlib

drawing x11 antialiasing xlib

I'm trying to develop a custom set of libraries for creating GUIs in Linux, with, you know, widgets, buttons, etc. So I'm now learning to creating user interfaces using X11 and its Xlib. I get to the

Why is there a difference in map() between ipython3 and the ipython3 notebook?

python-3.x ipython ipython-notebook

This is the result as expected when I run ipython3 (Python3.2.3) in the shell:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.