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 --------------------------------------------------
 
---------------------- 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 :: [[a]] -> [a] | [String] -> Stringstring
on concat(xs)
scriptfoldl(my append, "", xs)
on |λ|(a, b)
a & b
end |λ|
end script
if length of xs > 0 and class of (item 1 of xs) is string then
set unit to ""
else
set unit to {}
end if
foldl(append, unit, xs)
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:
 
 
-- TEST ---------------------------------- TEST -------------------------
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:
 
 
-- GENERIC FUNCTION ---------------------------- GENERIC FUNCTION -------------------
 
-- append :: String -> String -> String
on append(a, b)
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)
set unitlng to {}length of xs
set unitlst to ""{}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
onend |λ|(a, b)repeat
return lst
end |λ|tell
end map
 
 
-- Lift 2nd class handler function into 1st class script wrapper
Line 352 ⟶ 372:
end mReturn</lang>
{{out}}
<pre>{55, 3628800, "1098765432112345678910"}</pre>
 
=={{header|Arturo}}==
9,659

edits