% $Id: moments.slang,v 1.0 2003/01/03 13:38:00 dada Exp $ % http://dada.perl.it/shootout/ % % contributed by John E. Davis define main () { variable nums = array_map (Double_Type, &atof, fgetslines (stdin)); variable n = length (nums); variable sum = 0; foreach (nums) sum += (); variable mean = sum/n; variable average_deviation = 0; variable standard_deviation = 0; variable variance = 0; variable skew = 0; variable kurtosis = 0; % No slang programmer would code explicit loops. foreach (nums) { variable num = (); variable deviation = num - mean; average_deviation += abs(deviation); variance += deviation^2; skew += deviation^3; kurtosis += deviation^4; } average_deviation /= n; variance /= (n - 1); standard_deviation = sqrt(variance); if (variance > 0.0) { skew /= (n * variance * standard_deviation); kurtosis = kurtosis/(n * variance * variance) - 3.0; } nums = nums[array_sort(nums)]; variable mid = n/2; variable median; if (n mod 2) median = nums[mid]; else median = (nums[mid] + nums[mid-1])/2; vmessage ("n: %d", n); vmessage ("median: %f", median); vmessage ("mean: %f", mean); vmessage ("average_deviation: %f", average_deviation); vmessage ("standard_deviation: %f", standard_deviation); vmessage ("variance: %f", variance); vmessage ("skew: %f", skew); vmessage ("kurtosis: %f", kurtosis); } main ();