[erlang-questions] checksum calculation

Vance Shipley vances@REDACTED
Mon Apr 6 01:09:40 CEST 2009


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).


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 !



More information about the erlang-questions mailing list