-- histo(array,lo,hi,nbins) accumulates a histogram of values from 'array'. Returns the histogram in 'bins'. Also calculates total,mean and stdev of values between 'lo' and 'hi'.
-- See also: "histogram" XFun.

histo(array,lo,hi,nbins) = total := 0,
   sumn := 0, sumsq := 0,
   scl := nbins/(hi-lo),
   bins:=0[:nbins],
   i:=1,
   (
    num:=array[i],
    i:=i+1,
    (j := trunc((num-lo)*scl)+1,
     bins[j] := bins[j]+1,
     total := total+1,
     sumn := sumn+num,
     sumsq := sumsq + num*num) when numlo and num<hi) while i  count(array)

mean = sumn/total
stdev = sqrt((sumsq-sumn*sumn/total)/(total-1))

