-- 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'

-- max(array) finds max value in array

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 num³lo and num<hi) while i ² count(array)

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

max(array) = maxv:=-1/0,
  i:=0,
  (i:=i+1,
   maxv:=array[i] when array[i]>maxv) while i² count(array),
  maxv

