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

@ -1,73 +1,88 @@
-- List comprehension by direct and unsugared use of list monad
-- pythagoreanTriples :: Int -> [(Int, Int, Int)]
on pythagoreanTriples(maxInteger)
script X
on lambda(X)
script Y
on lambda(Y)
script Z
on lambda(Z)
if X * X + Y * Y = Z * Z then
unit([X, Y, Z])
on pythagoreanTriples(n)
script x
on |λ|(x)
script y
on |λ|(y)
script z
on |λ|(z)
if x * x + y * y = z * z then
[[x, y, z]]
else
[]
end if
end lambda
end |λ|
end script
bind(Z, range(1 + Y, maxInteger))
end lambda
|>>=|(enumFromTo(1 + y, n), z)
end |λ|
end script
bind(Y, range(1 + X, maxInteger))
end lambda
|>>=|(enumFromTo(1 + x, n), y)
end |λ|
end script
bind(X, range(1, maxInteger))
|>>=|(enumFromTo(1, n), x)
end pythagoreanTriples
-- TEST -----------------------------------------------------------------------
on run
-- Pythagorean triples drawn from integers in the range [1..n]
-- {(x, y, z) | x <- [1..n], y <- [x+1..n], z <- [y+1..n], (x^2 + y^2 = z^2)}
pythagoreanTriples(25)
--> {{3, 4, 5}, {5, 12, 13}, {6, 8, 10}, {7, 24, 25}, {8, 15, 17}, {9, 12, 15}, {12, 16, 20}, {15, 20, 25}}
--> {{3, 4, 5}, {5, 12, 13}, {6, 8, 10}, {7, 24, 25}, {8, 15, 17},
-- {9, 12, 15}, {12, 16, 20}, {15, 20, 25}}
end run
-- MONADIC FUNCTIONS (for list monad)
-- GENERIC FUNCTIONS ----------------------------------------------------------
-- Monadic bind for lists is simply ConcatMap
-- which applies a function f directly to each value in the list,
-- Monadic (>>=) (or 'bind') for lists is simply flip concatMap
-- (concatMap with arguments reversed)
-- It applies a function f directly to each value in the list,
-- and returns the set of results as a concat-flattened list
-- bind :: (a -> [b]) -> [a] -> [b]
on bind(f, xs)
-- concat :: a -> a -> [a]
script concat
on lambda(a, b)
-- The concatenation eliminates any empty lists,
-- combining list-wrapped results into a single results list
-- (>>=) :: Monad m => m a -> (a -> m b) -> m b
on |>>=|(xs, f)
concat(map(f, xs))
end |>>=|
-- concat :: [[a]] -> [a] | [String] -> String
on concat(xs)
script append
on |λ|(a, b)
a & b
end lambda
end |λ|
end script
foldl(concat, {}, map(f, xs))
end bind
if length of xs > 0 and class of (item 1 of xs) is string then
set empty to ""
else
set empty to {}
end if
foldl(append, empty, xs)
end concat
-- Monadic return/unit/inject for lists: just wraps a value in a list
-- a -> [a]
on unit(a)
[a]
end unit
---------------------------------------------------------------------------
-- GENERIC LIBRARY FUNCTIONS
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if n < m then
set d to -1
else
set d to 1
end if
set lst to {}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
end enumFromTo
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
@ -75,7 +90,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
@ -87,7 +102,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
@ -100,21 +115,7 @@ on mReturn(f)
f
else
script
property lambda : f
property |λ| : f
end script
end if
end mReturn
-- range :: Int -> Int -> [Int]
on range(m, n)
if n < m then
set d to -1
else
set d to 1
end if
set lst to {}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
end range