Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,98 @@
|
|||
--------------- CALCULATING THE VALUE OF E ----------------
|
||||
on run
|
||||
|
||||
sum(map(inverse, ¬
|
||||
scanl(product, 1, enumFromTo(1, 16))))
|
||||
|
||||
--> 2.718281828459
|
||||
|
||||
end run
|
||||
|
||||
-- inverse :: Float -> Float
|
||||
on inverse(x)
|
||||
1 / x
|
||||
end inverse
|
||||
|
||||
-- product :: Float -> Float -> Float
|
||||
on product(a, b)
|
||||
a * b
|
||||
end product
|
||||
|
||||
|
||||
--------------------- GENERIC FUNCTIONS ---------------------
|
||||
|
||||
-- enumFromTo :: Int -> Int -> [Int]
|
||||
on enumFromTo(m, n)
|
||||
if m ≤ n then
|
||||
set lst to {}
|
||||
repeat with i from m to n
|
||||
set end of lst to i
|
||||
end repeat
|
||||
lst
|
||||
else
|
||||
{}
|
||||
end if
|
||||
end enumFromTo
|
||||
|
||||
-- foldl :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldl(f, startValue, xs)
|
||||
tell mReturn(f)
|
||||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to |λ|(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldl
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
-- The list obtained by applying f
|
||||
-- to each element of xs.
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
-- scanl :: (b -> a -> b) -> b -> [a] -> [b]
|
||||
on scanl(f, startValue, xs)
|
||||
tell mReturn(f)
|
||||
set v to startValue
|
||||
set lng to length of xs
|
||||
set lst to {startValue}
|
||||
repeat with i from 1 to lng
|
||||
set v to |λ|(v, item i of xs, i, xs)
|
||||
set end of lst to v
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end scanl
|
||||
|
||||
-- sum :: [Num] -> Num
|
||||
on sum(xs)
|
||||
script add
|
||||
on |λ|(a, b)
|
||||
a + b
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
foldl(add, 0, xs)
|
||||
end sum
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
------------- APPROXIMATION OF THE VALUE OF E ------------
|
||||
|
||||
-- eApprox :: Int -> Float
|
||||
on eApprox(n)
|
||||
-- The approximation of E obtained after N iterations.
|
||||
script go
|
||||
on |λ|(efl, x)
|
||||
set {e, fl} to efl
|
||||
set flx to fl * x
|
||||
|
||||
{e + (1 / flx), flx}
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
item 1 of foldl(go, {1, 1}, enumFromTo(1, n))
|
||||
end eApprox
|
||||
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
on run
|
||||
-- The approximation of E obtained after 20 iterations.
|
||||
eApprox(20)
|
||||
end run
|
||||
|
||||
|
||||
------------------------- GENERIC ------------------------
|
||||
|
||||
-- enumFromTo :: Int -> Int -> [Int]
|
||||
on enumFromTo(m, n)
|
||||
if m ≤ n then
|
||||
set xs to {}
|
||||
repeat with i from m to n
|
||||
set end of xs to i
|
||||
end repeat
|
||||
xs
|
||||
else
|
||||
{}
|
||||
end if
|
||||
end enumFromTo
|
||||
|
||||
|
||||
-- foldl :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldl(f, startValue, xs)
|
||||
tell mReturn(f)
|
||||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to |λ|(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldl
|
||||
|
||||
|
||||
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
|
||||
on mReturn(f)
|
||||
-- 2nd class handler function lifted into 1st class script wrapper.
|
||||
if script is class of f then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
Loading…
Add table
Add a link
Reference in a new issue