September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -3,30 +3,28 @@ on levenshtein(sa, sb)
|
|||
set {s1, s2} to {characters of sa, characters of sb}
|
||||
|
||||
script
|
||||
on lambda(ns, c)
|
||||
on |λ|(ns, c)
|
||||
script minPath
|
||||
on lambda(z, c1xy)
|
||||
on |λ|(z, c1xy)
|
||||
set {c1, x, y} to c1xy
|
||||
minimum({y + 1, z + 1, x + fromEnum(c1 is not c)})
|
||||
end lambda
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
set {n, ns1} to uncons(ns)
|
||||
scanl(minPath, n + 1, zip3(s1, ns, ns1))
|
||||
end lambda
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
|last|(foldl(result, range(0, length of s1), s2))
|
||||
|last|(foldl(result, enumFromTo(0, length of s1), s2))
|
||||
end levenshtein
|
||||
|
||||
|
||||
-- TEST ------------------------------------------------------------------------------
|
||||
|
||||
-- TEST -----------------------------------------------------------------------
|
||||
on run
|
||||
script test
|
||||
on lambda(xs)
|
||||
on |λ|(xs)
|
||||
levenshtein(item 1 of xs, item 2 of xs)
|
||||
end lambda
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
map(test, [["kitten", "sitting"], ["sitting", "kitten"], ¬
|
||||
|
|
@ -36,66 +34,61 @@ on run
|
|||
end run
|
||||
|
||||
|
||||
-- GENERIC FUNCTIONS ------------------------------------------------------------
|
||||
-- GENERIC FUNCTIONS -----------------------------------------------------------
|
||||
|
||||
-- minimum :: [a] -> a
|
||||
on minimum(xs)
|
||||
script min
|
||||
on lambda(a, x)
|
||||
if x < a or a is missing value then
|
||||
x
|
||||
else
|
||||
a
|
||||
end if
|
||||
end lambda
|
||||
end script
|
||||
-- enumFromTo :: Enum a => a -> a -> [a]
|
||||
on enumFromTo(m, n)
|
||||
set {intM, intN} to {fromEnum(m), fromEnum(n)}
|
||||
|
||||
foldl(min, missing value, xs)
|
||||
end minimum
|
||||
|
||||
-- fromEnum :: Enum a => a -> Int
|
||||
on fromEnum(x)
|
||||
if class of x is boolean then
|
||||
x as integer
|
||||
if intM > intN then
|
||||
set d to -1
|
||||
else
|
||||
id of x
|
||||
set d to 1
|
||||
end if
|
||||
end fromEnum
|
||||
|
||||
-- uncons :: [a] -> Maybe (a, [a])
|
||||
on uncons(xs)
|
||||
if length of xs > 0 then
|
||||
{item 1 of xs, rest of xs}
|
||||
set lst to {}
|
||||
if class of m is text then
|
||||
repeat with i from intM to intN by d
|
||||
set end of lst to chr(i)
|
||||
end repeat
|
||||
else
|
||||
missing value
|
||||
repeat with i from intM to intN by d
|
||||
set end of lst to i
|
||||
end repeat
|
||||
end if
|
||||
end uncons
|
||||
return lst
|
||||
end enumFromTo
|
||||
|
||||
-- scanl :: (b -> a -> b) -> b -> [a] -> [b]
|
||||
on scanl(f, startValue, xs)
|
||||
-- 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
|
||||
set lst to {startValue}
|
||||
repeat with i from 1 to lng
|
||||
set v to lambda(v, item i of xs, i, xs)
|
||||
set end of lst to v
|
||||
set v to |λ|(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
return v
|
||||
end tell
|
||||
end scanl
|
||||
end foldl
|
||||
|
||||
-- zip3 :: [a] -> [b] -> [c] -> [(a, b, c)]
|
||||
on zip3(xs, ys, zs)
|
||||
script
|
||||
on lambda(x, i)
|
||||
[x, item i of ys, item i of zs]
|
||||
end lambda
|
||||
end script
|
||||
|
||||
map(result, items 1 thru ¬
|
||||
minimum({length of xs, length of ys, length of zs}) of xs)
|
||||
end zip3
|
||||
-- fromEnum :: Enum a => a -> Int
|
||||
on fromEnum(x)
|
||||
set c to class of x
|
||||
if c is boolean then
|
||||
if x then
|
||||
1
|
||||
else
|
||||
0
|
||||
end if
|
||||
else if c is text then
|
||||
if x ≠ "" then
|
||||
id of x
|
||||
else
|
||||
missing value
|
||||
end if
|
||||
else
|
||||
x as integer
|
||||
end if
|
||||
end fromEnum
|
||||
|
||||
-- last :: [a] -> a
|
||||
on |last|(xs)
|
||||
|
|
@ -106,44 +99,18 @@ on |last|(xs)
|
|||
end if
|
||||
end |last|
|
||||
|
||||
-- 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
|
||||
|
||||
-- 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 lambda(item i of xs, i, xs)
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
-- 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 lambda(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 :: Handler -> Script
|
||||
on mReturn(f)
|
||||
|
|
@ -151,7 +118,57 @@ on mReturn(f)
|
|||
f
|
||||
else
|
||||
script
|
||||
property lambda : f
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- minimum :: [a] -> a
|
||||
on minimum(xs)
|
||||
script min
|
||||
on |λ|(a, x)
|
||||
if x < a or a is missing value then
|
||||
x
|
||||
else
|
||||
a
|
||||
end if
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
foldl(min, missing value, xs)
|
||||
end minimum
|
||||
|
||||
-- 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
|
||||
|
||||
-- uncons :: [a] -> Maybe (a, [a])
|
||||
on uncons(xs)
|
||||
if length of xs > 0 then
|
||||
{item 1 of xs, rest of xs}
|
||||
else
|
||||
missing value
|
||||
end if
|
||||
end uncons
|
||||
|
||||
-- zip3 :: [a] -> [b] -> [c] -> [(a, b, c)]
|
||||
on zip3(xs, ys, zs)
|
||||
script
|
||||
on |λ|(x, i)
|
||||
[x, item i of ys, item i of zs]
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
map(result, items 1 thru ¬
|
||||
minimum({length of xs, length of ys, length of zs}) of xs)
|
||||
end zip3
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue