September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -3,63 +3,66 @@ on arithmetic_mean(xs)
-- sum :: Number -> Number -> Number
script sum
on lambda(accumulator, x)
on |λ|(accumulator, x)
accumulator + x
end lambda
end |λ|
end script
foldl(sum, 0, xs) / (length of xs)
end arithmetic_mean
-- geometric_mean :: [Number] -> Number
on geometric_mean(xs)
-- product :: Number -> Number -> Number
script product
on lambda(accumulator, x)
on |λ|(accumulator, x)
accumulator * x
end lambda
end |λ|
end script
foldl(product, 1, xs) ^ (1 / (length of xs))
end geometric_mean
-- harmonic_mean :: [Number] -> Number
on harmonic_mean(xs)
-- addInverse :: Number -> Number -> Number
script addInverse
on lambda(accumulator, x)
on |λ|(accumulator, x)
accumulator + (1 / x)
end lambda
end |λ|
end script
(length of xs) / (foldl(addInverse, 0, xs))
end harmonic_mean
-- TEST
-- TEST -----------------------------------------------------------------------
on run
script test
on lambda(f)
mReturn(f)'s lambda({1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
end lambda
end script
set {A, G, H} to ¬
map(test, {arithmetic_mean, geometric_mean, harmonic_mean})
set {A, G, H} to ap({arithmetic_mean, geometric_mean, harmonic_mean}, ¬
{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}})
{values:{arithmetic:A, geometric:G, harmonic:H}, inequalities:¬
{|A >= G|:A G}, |G >= H|:G H}
end run
---------------------------------------------------------------------------
-- GENERIC FUNCTIONS
-- GENERIC FUNCTIONS ----------------------------------------------------------
-- A list of functions applied to a list of arguments
-- (<*> | ap) :: [(a -> b)] -> [a] -> [b]
on ap(fs, xs)
set {nf, nx} to {length of fs, length of xs}
set acc to {}
repeat with i from 1 to nf
tell mReturn(item i of fs)
repeat with j from 1 to nx
set end of acc to |λ|(contents of (item j of xs))
end repeat
end tell
end repeat
return acc
end ap
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
@ -67,7 +70,7 @@ on foldl(f, startValue, xs)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to lambda(v, item i of xs, i, xs)
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
@ -79,7 +82,7 @@ on map(f, xs)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda(item i of xs, i, xs)
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
@ -92,7 +95,7 @@ on mReturn(f)
f
else
script
property lambda : f
property |λ| : f
end script
end if
end mReturn