% NOTE: when compiling with Portable CLU, % this program needs to be merged with 'useful.lib' to get log() % % pclu -merge $CLUHOME/lib/useful.lib -compile entropy.clu shannon = proc (s: string) returns (real) % find the frequency of each character freq: array[int] := array[int]$fill(0, 256, 0) for c: char in string$chars(s) do i: int := char$c2i(c) freq[i] := freq[i] + 1 end % calculate the component for each character h: real := 0.0 rlen: real := real$i2r(string$size(s)) for i: int in array[int]$indexes(freq) do if freq[i] ~= 0 then f: real := real$i2r(freq[i]) / rlen h := h - f * log(f) / log(2.0) end end return (h) end shannon start_up = proc () po: stream := stream$primary_output() stream$putl(po, f_form(shannon("1223334444"), 1, 6)) end start_up