Wednesday, April 25, 2007

Erlang Nights in Utah

Well, it looks like I’m not the only Ruby hacker in Utah that’s looking at Erlang. In the UtahValley.rb’s April meeting, we decided to set up an informal study group to work through Programming Erlang—several of us already have the beta book.




I don’t know if we’ll start on the first or the fifteenth of May, but I do know it will be fun. I’ll plan on posting about them here.

Wednesday, April 18, 2007

Getting to know erlang-mode

The very first errata listed for the beta Programming Erlang: Software for a Concurrent World is that Joe should include a chapter on erlang programming tools, and it specifically mentions erlang-mode for emacs. Being an emacs user, this piqued my interest and I went off in search of it. Here's a quick run-down of what I found.



Installing and Running erlang-mode



erlang-mode is part of the OTP erlang distribution, although it's kind of hidden. Since I compiled and installed my distribution in the /usr tree, All my examples will point there. Even though the needed elisp files are installed and easy to get runing, you'll need to download and install the erlang man pages. I put them in /usr/lib/erlang on my system.



Once the code and documentation is installed, you can set it up by adding the following elisp code to your .emacs file:


;; setup erlang mode
;; add the location of the elisp files to the load-path
(setq load-path (cons "/usr/lib/erlang/lib/tools-2.5.3/emacs"
load-path))
;; set the location of the man page hierarchy
(setq erlang-root-dir "/usr/lib/erlang")
;; add the home of the erlang binaries to the exec-path
(setq exec-path (cons "/usr/lib/bin" exec-path))
;; load and eval the erlang-start package to set up
;; everything else
(require 'erlang-start)

(Feel free to strip out the comments, They aren't in my .emacs file either.)



First Impressions



The first thing that I found to like about erlang-mode was the ability to run an erlang shell inside emacs. Since I like to play in a REPL while I program, having a shell running inside my editor is great. While I normally have my emacs running much larger than this, I wanted to show a full-sized screenshot so you could read the text. Working this way just rocks!



Picture of an emacs session with the erl shell and syntax highlighting



The screenshot above also shows off erlang-mode's syntax highlighting. There are four levels of syntax highlighting ranging from off to 'Christmas Tree Mode' (which is the highlighting level I run in).



Another thing that leaped out at me, was the auto-formatter/indenter that's built into erlang-mode. It was a little bit strange to be typing along and hit an arrow (->) only to have emacs automagically move to the next line and indent for me. I got used to it in a couple of minutes, and realized that I could get to like it — if I felt like it was using good style, it doesn't match up against the example in Programming Erlang: Software for a Concurrent World. Anyone want to weigh in on how good or bad it looks?


qsort([]) ->
[];
qsort([Pivot|T]) ->
qsort([X || X <- T,
X < Pivot])
++ [Pivot] ++
qsort([X || X <- T,
X >= Pivot]).

(By the way, it also automatically creates an indented, new line in a lot of cases. I'm not quite sure what all of them are yet.)



Since I'm on the topic of code formatting, I thought I'd toss out several small but interesting functions.



  • erlang-align-arrows (C-c C-a): I think that well used whitespace can make reading a program easier, and aligning similar parts of a program is one example. Running erlang-align-arrows against this region:

    for(Max, Max, F) -> [F(Max)];
    for(I, Max, F) -> [F(I)| for(I+1, Max, F)].

    sum([H|T]) -> H + sum(T);
    sum([]) -> 0.

    map(_, []) -> [];
    map(F, [H|T])-> [F(H) | map(F, T)].

    will reformat it to look like this:

    for(Max, Max, F) -> [F(Max)];
    for(I, Max, F) -> [F(I)| for(I+1, Max, F)].

    sum([H|T]) -> H + sum(T);
    sum([]) -> 0.

    map(_, []) -> [];
    map(F, [H|T]) -> [F(H) | map(F, T)].


  • erlang-generate-new-clause (C-c C-j): This command (and the next one) will likely save some typing. It generates a new clause in the curent function, immediately below the current clause, and puts the point at the argument list inside the parens. For example, running erlang-generate-new-clause with the point on the rectangle clause of the following function:

    area({rectangle, Width, Ht}) -> Width * Ht;
    area({circle, R}) -> 3.14159 * R * R;
    area({square,X}) -> geometry:area({rectangle, X, X}).

    will create:

    area({rectangle, Width, Ht}) -> Width * Ht;
    area() ->
    area({circle, R}) -> 3.14159 * R * R;
    area({square,X}) -> geometry:area({rectangle, X, X}).

    One small gotcha is that there's no closing punctuation for the generated clause, so you'll need to type your own semicolon or period.


  • erlang-clone-arguments (C-c C-y): This command will insert the arguments from the previous clause at the current point. If you run it immediately after the erlang-generate-new-clause command above, you'll the new clause will be changed to read:

    area({rectangle, Width, Ht}) ->

    which would be easy to edit to create a triangle clause.



One set of commands I didn't play with were the compilation commands. Once I get to that section of Programming Erlang: Software for a Concurrent World, I'm sure I'll dig back in — I'll plan on writing another post about erlang-mode then.



I hope this was useful. I think I'll be spending a fair amount of time in erlang-mode, so I plan on getting comfortable here. Happy hacking!

Tuesday, April 17, 2007

Why

Ok, so I've settled on Erlang as the functional language I'm going to study this year. I'm not really going to jump in until May, but I'll play in the wading pool until then.



I'll be using Programming Erlang: Software for a Concurrent World as my study guide, along with any good looking blogs/websites I track down while I'm working on it. I've already seen Dave Thomas' two posts on Erlang (A First Erlang Program and Adding Concurrency to Our Erlang Program) and Yariv Sadan's blog looks good too. If I'm missing some good blogs/resources, please, let me know.



I'll track my progress here. Don't expect any whiz-bang results right away, but hopefully it will be both interesting and useful (at least to other Erlang newbies out there).

Erlang and Haskell Books: First Impressions

I recently picked up a beta copy of Programming Erlang: Software for a Concurrent World to go along with my (paper) copy of Programming in Haskell. I figured if I had both books, I could make quick runs through them as I try to decide between Haskell and Erlang as the FP language I'm going to focus on this summer. What follows isn't really a review of either book, just a few first impressions.





Programming Erlang: Software for a Concurrent World looks like to be a very accessible book, that also goes into enough depth to be worthwhile. The version I got yesterday had 279 pages in the PDF, but an update was released today and my new copy hasn't found it's way here yet. I like what I've read of Joe Armstrong's writing and examples, and have caught on quickly to the initial concepts.





Programming in Haskell surprised me by being really small, only 171 pages (including the index). It on the scale of 'The C Programming Language', and so far it reminds me a lot of that book. I have to admit though, that I've always been put off by the seeming need of Haskell writers to use 'special Haskell characters' in their text, Programming Haskell even includes a table of 15 such symbols and how to represent them in ASCII. Bleargh!



So far, I'm leaning towards Erlang (mostly because of concurrency), but I'm not going to make up my mind for a while yet — too many other deadlines I need to deal with right now. As I get closer to the summer, I'll let you know which way I decide to go. (I'll also get real reviews of the two books up fairly soon.)