blob: eb8441425322400859b33b534fd60aac08b330f1 [file] [log] [blame] [edit]
type Heap
type Bag = { count : U32, sum : U32 }
newBag : Heap -> < Failure Heap | Success (Bag, Heap) >
freeBag : (Heap, Bag) -> Heap
addToBag : (U32, Bag) -> Bag
addToBag (x, b { count = c, sum = s }) = b { count = c + 1, sum = s + x }
averageBag : Bag! -> < EmptyBag | Success U32 >
averageBag b = if b.count == 0 then EmptyBag else Success (b.sum / b.count)
type List a
reduce : all (a, b). ((List a)!, (a!, b) -> b, b) -> b
average : (Heap, (List U32)!) -> (Heap, U32)
average (h, ls) =
newBag h
| Success (bag, h') ->
let bag' = reduce (ls, addToBag, bag)
in averageBag bag' !bag'
| Success n -> (freeBag (h', bag'), n)
| EmptyBag -> (freeBag (h', bag'), 0)
| Failure h' -> (h', 0)