Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
171
Task/Leonardo-numbers/AppleScript/leonardo-numbers-1.applescript
Normal file
171
Task/Leonardo-numbers/AppleScript/leonardo-numbers-1.applescript
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
------------------------ GENERATOR -------------------------
|
||||
|
||||
-- leo :: Int -> Int -> Int -> Generator [Int]
|
||||
on leo(L0, L1, delta)
|
||||
script
|
||||
property x : L0
|
||||
property y : L1
|
||||
on |λ|()
|
||||
set n to x
|
||||
set {x, y} to {y, x + y + delta}
|
||||
return n
|
||||
end |λ|
|
||||
end script
|
||||
end leo
|
||||
|
||||
|
||||
--------------------------- TEST ---------------------------
|
||||
on run
|
||||
set leonardo to leo(1, 1, 1)
|
||||
set fibonacci to leo(0, 1, 0)
|
||||
|
||||
unlines({"First 25 Leonardo numbers:", ¬
|
||||
twoLines(take(25, leonardo)), "", ¬
|
||||
"First 25 Fibonacci numbers:", ¬
|
||||
twoLines(take(25, fibonacci))})
|
||||
end run
|
||||
|
||||
|
||||
------------------------ FORMATTING ------------------------
|
||||
|
||||
-- twoLines :: [Int] -> String
|
||||
on twoLines(xs)
|
||||
script row
|
||||
on |λ|(ns)
|
||||
tab & intercalate(", ", ns)
|
||||
end |λ|
|
||||
end script
|
||||
return unlines(map(row, chunksOf(16, xs)))
|
||||
end twoLines
|
||||
|
||||
|
||||
------------------------- GENERIC --------------------------
|
||||
|
||||
-- chunksOf :: Int -> [a] -> [[a]]
|
||||
on chunksOf(n, xs)
|
||||
set lng to length of xs
|
||||
script go
|
||||
on |λ|(a, i)
|
||||
set x to (i + n) - 1
|
||||
if x ≥ lng then
|
||||
a & {items i thru -1 of xs}
|
||||
else
|
||||
a & {items i thru x of xs}
|
||||
end if
|
||||
end |λ|
|
||||
end script
|
||||
foldl(go, {}, enumFromThenTo(1, n, lng))
|
||||
end chunksOf
|
||||
|
||||
|
||||
-- enumFromThenTo :: Int -> Int -> Int -> [Int]
|
||||
on enumFromThenTo(x1, x2, y)
|
||||
set xs to {}
|
||||
set d to max(1, (x2 - x1))
|
||||
repeat with i from x1 to y by d
|
||||
set end of xs to i
|
||||
end repeat
|
||||
return xs
|
||||
end enumFromThenTo
|
||||
|
||||
|
||||
-- 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
|
||||
|
||||
|
||||
-- intercalate :: String -> [String] -> String
|
||||
on intercalate(sep, xs)
|
||||
set {dlm, my text item delimiters} to ¬
|
||||
{my text item delimiters, sep}
|
||||
set s to xs as text
|
||||
set my text item delimiters to dlm
|
||||
return s
|
||||
end intercalate
|
||||
|
||||
|
||||
-- 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)
|
||||
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
|
||||
|
||||
|
||||
-- max :: Ord a => a -> a -> a
|
||||
on max(x, y)
|
||||
if x > y then
|
||||
x
|
||||
else
|
||||
y
|
||||
end if
|
||||
end max
|
||||
|
||||
|
||||
-- take :: Int -> [a] -> [a]
|
||||
-- take :: Int -> String -> String
|
||||
on take(n, xs)
|
||||
set c to class of xs
|
||||
if list is c then
|
||||
if 0 < n then
|
||||
items 1 thru min(n, length of xs) of xs
|
||||
else
|
||||
{}
|
||||
end if
|
||||
else if string is c then
|
||||
if 0 < n then
|
||||
text 1 thru min(n, length of xs) of xs
|
||||
else
|
||||
""
|
||||
end if
|
||||
else if script is c then
|
||||
set ys to {}
|
||||
repeat with i from 1 to n
|
||||
set v to xs's |λ|()
|
||||
if missing value is v then
|
||||
return ys
|
||||
else
|
||||
set end of ys to v
|
||||
end if
|
||||
end repeat
|
||||
return ys
|
||||
else
|
||||
missing value
|
||||
end if
|
||||
end take
|
||||
|
||||
|
||||
-- unlines :: [String] -> String
|
||||
on unlines(xs)
|
||||
set {dlm, my text item delimiters} to ¬
|
||||
{my text item delimiters, linefeed}
|
||||
set str to xs as text
|
||||
set my text item delimiters to dlm
|
||||
str
|
||||
end unlines
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
-- spec: record containing none, some, or all of the 'L0', 'L1', and 'add' values.
|
||||
on leonardos(spec, quantity)
|
||||
-- Assign the spec values to variables, using defaults for any not given.
|
||||
set {L0:a, L1:b, add:inc} to spec & {L0:1, L1:1, add:1}
|
||||
-- Build the output list.
|
||||
script o
|
||||
property output : {a, b}
|
||||
end script
|
||||
repeat (quantity - 2) times
|
||||
set c to a + b + inc
|
||||
set end of o's output to c
|
||||
set a to b
|
||||
set b to c
|
||||
end repeat
|
||||
|
||||
return o's output
|
||||
end leonardos
|
||||
|
||||
local output, astid
|
||||
set astid to AppleScript's text item delimiters
|
||||
set AppleScript's text item delimiters to ", "
|
||||
set output to "1st 25 Leonardos:
|
||||
" & leonardos({}, 25) & "
|
||||
1st 25 Fibonaccis:
|
||||
" & leonardos({L0:0, L1:1, add:0}, 25)
|
||||
set AppleScript's text item delimiters to astid
|
||||
return output
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
"1st 25 Leonardos:
|
||||
1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049
|
||||
1st 25 Fibonaccis:
|
||||
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368"
|
||||
Loading…
Add table
Add a link
Reference in a new issue