[erlang-questions] checksum calculation
Tony Rogvall
tony@REDACTED
Mon Apr 6 12:57:21 CEST 2009
A small fix.
On 6 apr 2009, at 01.09, Vance Shipley wrote:
> Gamoto,
>
> You could call your checksum/2 function with zero for the
> accumulator but the conventional way is to export a checksum/1
> function and just use checksum/2 internally. Put a clause head
> first which matches the empty input binary.
>
> -Vance
>
>
> -export([checksum/1]).
>
> checksum(Binary) ->
> checksum(Binary, 0).
>
> checksum( <<>>, Csum) ->
> Csum;
> checksum( <<N,Rest>>, Csum) ->
> checksum(Rest, Csum bxor N).
The last clause should be:
checsum(<<N,Rest/binary>>, CSum) ->
...
Otherwise the matching will match a binary with two byte values.
<<A,B>> = <<1,2,3,4,5>>.
** exception error: no match of right hand side value <<1,2,3,4,5>>
<<A,B/binary>> = <<1,2,3,4,5>>.
<<1,2,3,4,5>>
3> A.
1
4> B.
<<2,3,4,5>>
/Tony
>
>
>
> On Mon, Apr 06, 2009 at 12:29:12AM +0100, Gamoto wrote:
> } I have a string of bytes and would like to compute the
> } checksum which is the xor of all bytes.
> }
> } Let S the string, N the current byte
> }
> } checksum( <<N,Rest>>,Csum) ->
> } checksum(Rest,Csum bxor N);
> }
> } The problems are: I don't know how to start (checksum(<<S>>) ???)
> and how to finish !
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
More information about the erlang-questions
mailing list