Sep 15, 2008
3_small_small Joe Armstrong 7 posts

Topic: Programming Erlang / Loops and accumulated data

The error message is very precise it has found an error before ‘)’ – something is missing -
the error is encountered when the parser hits the ‘)’ (thus the diagnostic)

Both fun and if require an end statement. The syntax is

fun(...) ->
   ...
end
if
   ...
end

If you line them up it is clearer

so it should be
loop(8, X, fun(Y) ->
               if
                   ...
               end
           end).
inside the fun I've changed the free variable to Y (otherwise the value of X is
ambiguous - do you mean the X in the fun(X) or the X in calculate(X)?
finally

calculate(X) -> inner_loop(8, X, fun(Y) -> if Y band 16#0001 1 -> ?POLYNOMIAL bxor (Y bsr 1); Y band 16#0001 0 -> Y bsr 1 end end).

This should do the trick

/Joe

 
Sep 12, 2008
3_small_small Joe Armstrong 7 posts

Topic: Programming Erlang / Loops and accumulated data

Loops have to be programmed using recursion – take this in small steps:

Suppose you have a function f(X) and you want to call it N times:

One iteration of this loop might be

loop(N, X) -> NextX = f(X), loop(N-1, NextX)

The loop index (N) counts down by one each time, so we have to stop the looping when N gets down to 0 – we
add a new line

loop(0, X) -> X;
loop(N, X) -> loop(N-1, f(X)).

(actually I made two changes here and eliminated the temporary variable NextX.

This calls a fixed function f(X) – so I might want to generalize this:

loop(0, X, _) -> X;
loop(N, X, F) -> loop(N-1, F(X), F).

so now loop(N, X, F) just computes F(F(F(F(X)))) (N times)

Now you put it all together

crc32(X) -> loop(8, X, fun(X) -> if X band 16#0001 == 1 -> ... end)

Look what happened to the for(i=0; i<8>
loop – all this is is an idiom saying “loop 8 times”

I can make something that loops 8 times like this:

loop(0) -> true;
loop(N) -> <<something>> , loop(N-1)

then calling loop(8) with evaluate <<something>> 8 times

the rest of the loop (see earlier) is just concerned with getting the correct variables
in and out of the loop.

/Joe

 
Feb 26, 2008
3_small_small Joe Armstrong 7 posts

Topic: Programming Erlang / 2.6 One time assigments

Good question !

Erlang has no loops or for/next cycles so the problem does not occur.

Basically you cannot say X = X + 1, you have to invent a new variable X1 and make sure that X1 goes “out of scope”.

So the old code (in an imperative language)

foo(X) {
    X = X + 1,
    Y = bar(X),
    ho(X, Y),
    joe(Y).
}

becomes

foo(X) -> 
    X1 = X + 1,
    Y = bar(X1),
    ho(X1, Y),
    %% At this point X1 is no longer referred to so the space for X1 can  be freed
    joe(Y).

We now call joe(Y) but where does joe(Y) return to? – to the end of foo(X) where it finds a return instruction
ultimately joe(Y) returns to the place that called foo(X) – the point is that Y is never refereed to again
in the body of foo(X) and can be garbage collected.

So we create new variables X1, X2, X3, .... and the space for these can be reclaimed
when the program can no longer access these variables.

To get the effect of a loop we write a tail-recursive recursive function. So to sum the integers from 1 to N
we might write.

sum(N) -> sum(N, 0).

sum(0, Sum) -> Sum;
sum(N, Sum) -> Sum1 = Sum + 1, %% create a new value of Sum N1 = N – 1, %% and a new value of N sum(N1, Sum1). %% call sum (again)

The last line (which calls sum) merely jumps to the start of the sum routine. Although this is a call
the system does not return to site of the call, since this merely returns immediately, instead it returns,
(this is called “last-call” optimization). Having jumped to the start of sum the variables
N1 and Sum1 are out of scope and can never be accessed again, so they get garbaged. The Erlang compiler
recognises things like this and attempts to save space by storing N1 in the register used for N (if this
is possible).

This above code is a loop (after compilation)

BTW We’d really write the second clause of sum as:

sum(N, Sum) -> sum(N-1, Sum+N)

Without the temporary variables. The version I wrote was so you can see what happens to the variables.

Hope that helps!

/Joe

 
Jan 11, 2008
3_small_small Joe Armstrong 7 posts

Topic: Programming Erlang / SHOUTcast server: mp3_manager module

Excellent. The reason for dumping to a .tmp file was that I sometimes manually removed
some files before renaming to mp3data.

Unfortunately it proved to be rather difficult to write code that correct unpacks all ID3 tags
these can be at the start or end of the file – and some of them seem to have been written by buggy programs so the tags are not according to the standards. Correctly parsing all possible
tags might make a nice project. The code in the book should give you some ideas as how to start this.

Incidentally, I was able to make the shoutcast code in the book run with my soundbridge so I could
remotely control the soundbrideg – If I had more time I’d persue this more….

Cheers

/Joe Armstrng

 
Nov 4, 2007
3_small_small Joe Armstrong 7 posts

Topic: Programming Erlang / question about 'irc lite'

You’re quite right – the call to controller/2 is the key to understanding everything.
I say this on page 194 – but it probably wasn’t clear enough. In retrospect I think the IRC
code is a little tricky to follow – the graphics detracts from the algorithm (thought it does
give a hint as to how to make a GUI in Erlang).

There is a much simpler example in the difficult to find “getting started” document at
the main Erlang web site – this describes a form of instant messenger – which is somewhat simpler than the IRC example.

http://www.erlang.org/doc/getting_started/conc_prog.html#3.5

Cheers

/Joe Armstrong

 
Oct 23, 2007
3_small_small Joe Armstrong 7 posts

Topic: Programming Erlang / Erlang on Google Video

Guess what – I’ve found the script to this movie (and yes it was scripted, I wrote the script)
I’ll scan it in and ORC it anybody is interested.

/Joe

 
Oct 23, 2007
3_small_small Joe Armstrong 7 posts

Topic: Programming Erlang / thanks, and a question

You’re right – I’m an idiot. I’m the nitwit who “fixed” it. Silly me.

The last clause of compute_area/1 in area_server.erl should be:

compute_area({rectonge, X, Y}) -> X * Y.

Glad you’re enjoying the book.

Cheers

/Joe Armstrong

7 posts