This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1 @@
FUNCTION MEAN(some-table (ALL))

View file

@ -0,0 +1,28 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. find-mean.
DATA DIVISION.
LOCAL-STORAGE SECTION.
01 i PIC 9(4).
01 summ USAGE FLOAT-LONG.
LINKAGE SECTION.
01 nums-area.
03 nums-len PIC 9(4).
03 nums USAGE FLOAT-LONG
OCCURS 0 TO 1000 TIMES
DEPENDING ON nums-len.
01 result USAGE FLOAT-LONG.
PROCEDURE DIVISION USING nums-area, result.
IF nums-len = 0
MOVE 0 TO result
GOBACK
END-IF
DIVIDE FUNCTION SUM(nums (ALL)) BY nums-len GIVING result
GOBACK
.

View file

@ -0,0 +1,4 @@
(defn mean [sq]
(if (empty? sq)
0
(/ (reduce + sq) (count sq))))

View file

@ -0,0 +1,4 @@
(defn mean [sq]
(if (empty? sq)
0
(float (/ (reduce + sq) (count sq)))))

View file

@ -1,34 +1,18 @@
#define std'basic'*.
#define std'patterns'*.
#define std'dictionary'*.
#define system.
#class MeanAction
// --- Sum ---
#class MeanAction : BasePattern
{
#field theValue.
#field theCount.
#role Empty
{
#method save : aWriter = 0 save:aWriter.
#method evaluate : aValue
[
theValue := Real::0.
theCount := Integer::0.
#shift.
self evaluate:aValue.
]
}
#initializer
#constructor new
[
#shift Empty.
theValue := Real new.
theCount := Integer new.
]
#method save : aWriter = (theValue / theCount) save:aWriter.
#method evaluate : aValue
[
theCount += 1.
@ -36,15 +20,12 @@
theValue += aValue.
]
#method start : aPattern
[
aPattern run:self.
^ Real64Value::self.
]
#method Number = theValue / theCount.
}
#symbol Program =
// --- Program ---
#symbol program =
[
'program'Output << MeanAction start:Scan::(1, 2, 3, 4, 5, 6, 7, 8).
console writeLine:(MeanAction new foreach:(1, 2, 3, 4, 5, 6, 7, 8) Number).
].

View file

@ -0,0 +1 @@
=AVERAGE(A1:A10)

View file

@ -0,0 +1 @@
=AVERAGE(

View file

@ -0,0 +1,17 @@
:- object(averages).
:- public(arithmetic/2).
% fails for empty vectors
arithmetic([X| Xs], Mean) :-
sum_and_count([X| Xs], 0, Sum, 0, Count),
Mean is Sum / Count.
% use accumulators to make the predicate tail-recursive
sum_and_count([], Sum, Sum, Count, Count).
sum_and_count([X| Xs], Sum0, Sum, Count0, Count) :-
Sum1 is Sum0 + X,
Count1 is Count0 + 1,
sum_and_count(Xs, Sum1, Sum, Count1, Count).
:- end_object.

View file

@ -0,0 +1,3 @@
| ?- averages::arithmetic([1,2,3,4,5,6,7,8,9,10], Mean).
Mean = 5.5
yes