Jump to content

Sum of first n cubes: Difference between revisions

→‎{{header|AppleScript}}: Replaced a map-accumulation with a scanning accumulation.
(→‎Python :: Functional: Replaced map-accumulation with scanning accumulation)
(→‎{{header|AppleScript}}: Replaced a map-accumulation with a scanning accumulation.)
Line 63:
script go
on |λ|(a, x)
set v to a + (x ^ 3) as integer
{v, v}
end |λ|
end script
item 2 of mapAccumLscanl(go, 0, enumFromTo(01, n - 1))
item 2 of mapAccumL(go, 0, enumFromTo(0, n - 1))
end sumsOfFirstNCubes
 
Line 98 ⟶ 95:
 
 
-- foldlscanl :: (ab -> ba -> ab) -> ab -> [ba] -> a[b]
on foldlscanl(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)
{v,set end of lst to v}
end repeat
return vlst
end tell
end foldlscanl
 
 
-- mapAccumL :: (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y])
on mapAccumL(f, acc, xs)
-- 'The mapAccumL function behaves like a combination of map and foldl;
-- it applies a function to each element of a list, passing an
-- accumulating parameter from |Left| to |Right|, and returning a final
-- value of this accumulator together with the new list.' (see Hoogle)
script go
on |λ|(a, x, i)
tell mReturn(f) to set pair to |λ|(item 1 of a, x, i)
{item 1 of pair, (item 2 of a) & {item 2 of pair}}
end |λ|
end script
foldl(go, {acc, []}, xs)
end mapAccumL
 
 
9,655

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.