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,12 +1,13 @@
-- KNUTH SHUFFLE -------------------------------------------------------------
-- knuthShuffle :: [a] -> [a]
on knuthShuffle(lst)
on knuthShuffle(xs)
-- randomSwap :: [Int] -> Int -> [Int]
script randomSwap
on lambda(a, i)
on |λ|(a, i)
if i > 1 then
set iRand to random number from 1 to i
tell a
set tmp to item iRand
set item iRand to item i
@ -16,23 +17,35 @@ on knuthShuffle(lst)
else
a
end if
end lambda
end |λ|
end script
foldr(randomSwap, lst, range(1, length of lst))
foldr(randomSwap, xs, enumFromTo(1, length of xs))
end knuthShuffle
-- TEST
-- TEST ----------------------------------------------------------------------
on run
knuthShuffle(["alpha", "beta", "gamma", "delta", "epsilon", ¬
"zeta", "eta", "theta", "iota", "kappa", "lambda", "mu"])
end run
-- GENERIC FUNCTIONS ---------------------------------------------------------
-- GENERIC LIBRARY FUNCTIONS
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if m > n 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
-- foldr :: (a -> b -> a) -> a -> [b] -> a
on foldr(f, startValue, xs)
@ -40,7 +53,7 @@ on foldr(f, startValue, xs)
set v to startValue
set lng to length of xs
repeat with i from lng to 1 by -1
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
@ -53,21 +66,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