Dec 20, 2007
Generic-user-small Peter Miller 4 posts

Topic: Programming Erlang / Problem 8.11 and register/2

Agreed. Thanks for sharing your version of the solution.

 
Nov 6, 2007
Generic-user-small Peter Miller 4 posts

Topic: Programming Erlang / Help with anagram example

But I’m still stuck with the problem of the syntactic sugar obscuring behavior. I don’t see how I am supposed to comprehend (pardon the pun) this behavior by looking at the code. Certainly in the future I’ll know what’s going on, hopefully. But how many people look at something like this (especially those of us from the imperative world) and just give up?

A good question to ask. I did not know what to expect coming out of that code until I ran it and reverse engineered how it should work. I also find the comparison to nested for loops helpful. Maybe, as you suggest, this was just a “tough love” approach to get us to learn on our own…

 
Nov 4, 2007
Generic-user-small Peter Miller 4 posts

Topic: Programming Erlang / Help with anagram example

Matt,

I am also learning Erlang and I blogged about the perms function, so please check it out and maybe that will help you understand what is going on (or at least what I think is going on).

I basically used substitution to fully expand out the function calls/recursion. Try evaluating the following in an Erlang session:

[ X+Y || X <- [1,10,20], Y <- [1,2,3] ].

You will get:

[2,3,4,11,12,13,21,22,23]

So in effect, you are right that X “anchors” itself at 1 while Y goes through 1, 2 and 3.

 
Oct 29, 2007
Generic-user-small Peter Miller 4 posts

Topic: Programming Erlang / Problem 8.11 and register/2

I’m new to Erlang and learning about it by reading through “Programming Erlang” (thank you Joe Armstrong for this excellent book). I just finished chapter 8 and was mulling over the first of the programming exercises from section 8.11:

Write a function start(AnAtom, Fun) to register AnAtomas spawn(Fun). Make sure your program works correctly in the case when two parallel processes simultaneously evaluate start/2. In this case, you must guarantee that one of these processes succeeds and the
other fails.

I was stumped at first, but then I found the following discussion post, Programming Erlang Exercise 8.11 , which presented a seemingly logical solution.

The solution in that thread did raise 2 interesting questions for me that I wanted to throw out to any experienced Erlang programmers:

1. How is the BIF register/2 function implemented to be an atomic call? I looked in the Erlang documentation online and could not find any details. As a BIF it is implemented in C, so I suppose there a lot of possibilities, but is there any way for a curious person to find out?

2. Is this problem of multiple processes trying to call register/2 at the same time something that you (experienced with Erlang) run into a lot and have to code around or is this problem more theoretical?

4 posts