Software engineer in the wireless communications industry. I’m interested in mobile communications networks, large-scale software projects, and anything related to C++. I’m just getting my feet wet with Erlang.

Posts by Bryan

 12 Sep 2008, 05:08 Bryan (6 posts) I’m still trying to wrap my head around Erlang and functional programming, and I am very much stuck with looping. I have some code in a CRC algorithm that I want to write in Erlang. The C code is as follows: for(i=0; i<8; i++){ if( x & 0x0001 == 0x0001) x = POLY ^ (x >> 1); else x = x >> 1; } The inner logic of the loop is easy enough in Erlang: if X band 16#0001 == 1 -> POLY bxor (X bsr 1); X band 16#0001 == 0 -> X bsr 1 end. However, I am not sure how to do the loop mechanics. I must use the updated value of X in the next iteration of the loop. Do I need to append the output of each iteration to a list, and then at the end take the tail of the list for my result? Is there an easier way to do this? 13 Sep 2008, 15:36 Bryan (6 posts) Thank you very much for walking me through the thought process and explaining the mechanics in such detail. It is all much clearer now, thank you! 15 Sep 2008, 04:59 Bryan (6 posts) I’m very much embarrassed to say this, but I have a syntax problem and I cannot figure out where it is. The code is as follows: -module(crc16). -export([calculate/1]). -export([inner_loop/3]). %%% CRC16 CCITT Normalized Polynomial -define(POLYNOMIAL, 16#1021). inner_loop(0, X, _) -> X; inner_loop(N, X, F) -> inner_loop(N-1, F(X), F). calculate(X) -> inner_loop(8, X, fun(X) -> if X band 16#0001 == 1 -> ?POLYNOMIAL bxor (X bsr 1); X band 16#0001 == 0 -> X bsr 1 end ). %%% outer_loop() -> %%% for(i=0; i<256; i++) { %%% // call inner_loop to calculate value %%% // add calculated value to crc lookup table at index i %%% } The output of the Erlang interpreter is as follows: 38> c(crc16). ./crc16.erl:16: syntax error before: ')' ./crc16.erl:2: function calculate/1 undefined error I would greatly appreciate any hints. I’m sure I’ve made a very elementary mistake but I just can’t seem to find it. Thank you for your time and help! 16 Sep 2008, 01:28 Bryan (6 posts) Thank you very much for your help. It sure was the “end” for the fun that I missed. 24 Sep 2008, 10:16 Bryan (6 posts) Is there an easy way to prove that a given solution works? Being new to Erlang I’m not sure how I’d generate a testbench that would prove a solution works. 24 Sep 2008, 10:22 Bryan (6 posts) Alain, Thanks for sharing your solution, it is very elegant. I hope eventually I’ll be able to adjust my “gears” to functional thinking.

6 posts