Permutations with repetitions: Difference between revisions
Applescript →Strict evaluation of the whole set: updated
(Applescript →Strict evaluation of the whole set: updated) |
|||
Line 15:
=={{header|AppleScript}}==
===Strict evaluation of the whole set===
Permutations with repetitions, using strict evaluation, generating the entire set (where system constraints permit) with some degree of efficiency. For lazy or interruptible evaluation, see the second example below.
<lang AppleScript>--
-- {{1, 1, 1}, {1, 1, 2}, {1, 2, 1}, {1, 2, 2}, {2, 1, 1},
-- {2, 1, 2}, {2, 2, 1}, {2, 2, 2}}
--
on
script go
script cons
on |λ|(a, bs)
{a} & bs
end
end script
on |λ|(x)
if x ≤ 0 then
{{}}
else
liftA2List(cons, xs, |λ|(x - 1))
end if
end |λ|
end script
go's |λ|(n)
end replicateM
-- TEST
on run
--
end run
-- GENERIC FUNCTIONS
--
on
set lng to length of xs
set acc to {}
tell mReturn(f)
repeat with i from 1 to lng
set acc to acc & |λ|(item i of xs, i, xs)
end repeat
end tell
return acc
end concatMap
-- liftA2List :: (a -> b -> c) -> [a] -> [b] -> [c]
on liftA2List(f, xs, ys)
script
property g : mReturn(f)'s |λ|
on |λ|(x)
script
on |λ|(y)
{
end |λ|
end script
concatMap(result, ys)
end |λ|
end script
concatMap(result, xs)
end liftA2List
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn ::
on mReturn(f)
if class of f is script then
Line 125 ⟶ 92:
end script
end if
end mReturn</lang>
{{Out}}
<lang AppleScript>{{1, 1}, {1, 2}, {1, 3}, {2, 1}, {2, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3}}</lang>
|