Recent Posts by Joe Armstrong
|
Sep 15, 2008
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 - Both fun and if require an end statement. The syntax is
If you line them up it is clearer
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
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 loop(0, X) -> 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; 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> I can make something that loops 8 times like this: loop(0) -> true; then calling loop(8) with evaluate <<something>> 8 times the rest of the loop (see earlier) is just concerned with getting the correct variables /Joe |
|
Feb 26, 2008
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)
becomes
We now call joe(Y) but where does joe(Y) return to? – to the end of foo(X) where it finds a return instruction So we create new variables X1, X2, X3, .... and the space for these can be reclaimed To get the effect of a loop we write a tail-recursive recursive function. So to sum the integers from 1 to N sum(N) -> sum(N, 0). sum(0, Sum) -> Sum; The last line (which calls sum) merely jumps to the start of the sum routine. Although this is a call This above code is a loop (after compilation) BTW We’d really write the second clause of sum as:
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
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 Unfortunately it proved to be rather difficult to write code that correct unpacks all ID3 tags Incidentally, I was able to make the shoutcast code in the book run with my soundbridge so I could Cheers /Joe Armstrng |
|
Nov 4, 2007
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. There is a much simpler example in the difficult to find “getting started” document at http://www.erlang.org/doc/getting_started/conc_prog.html#3.5 Cheers /Joe Armstrong |
|
Oct 23, 2007
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) /Joe |
|
Oct 23, 2007
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
