Catamorphism: Difference between revisions
→{{header|AppleScript}}: Primitive and output updated
(→{{header|AppleScript}}: Primitive and output updated) |
|||
Line 248:
(Note that to obtain first-class functions from user-defined AppleScript handlers, we have to 'lift' them into script objects).
<lang AppleScript>
---------------------- CATAMORPHISMS ---------------------
-- the arguments available to the called function f(a, x, i, l) are
Line 287 ⟶ 289:
--- OTHER FUNCTIONS DEFINED IN TERMS OF FOLDL AND FOLDR
-- concat ::
on concat(xs)
on |λ|(a, b)▼
a & b▼
end |λ|▼
set unit to ""▼
set unit to {}▼
end concat
-- product :: Num a => [a] -> a
Line 315 ⟶ 307:
foldr(result, 1, xs)
end product
-- str :: a -> String
on str(x)
x as string
end str
-- sum :: Num a => [a] -> a
Line 328 ⟶ 327:
on run
set xs to {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
{sum(xs), product(xs), concat(map(str, xs))}
--> {55, 3628800, "10987654321"}
Line 338 ⟶ 337:
-- append :: String -> String -> String
on append(a, b)
end append
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
-- The list obtained by applying f
-- to each element of xs.
tell mReturn(f)
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
return lst
end map
-- Lift 2nd class handler function into 1st class script wrapper
Line 352 ⟶ 372:
end mReturn</lang>
{{out}}
<pre>{55, 3628800, "
=={{header|Arturo}}==
|