Floyd's triangle: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added/changed comments and whitespace, changed indentations, used a template for the outputs.)
(→‎{{header|AppleScript}}: Updated primitives)
Line 193:
{{Trans|JavaScript}}
{{Trans|Haskell}} (mapAccumL versions)
<lang AppleScript>-- FLOYDs TRIANGLE ------------------------------------------------------------
 
-- floyd :: Int -> [[Int]]
on floyd(n)
script floydRow
on lambda|λ|(start, row)
{start + row + 1, enumFromTo(start, start + row)}
end lambda|λ|
end script
Line 211:
script aligned
on lambda|λ|(xs)
script pad
on lambda|λ|(w, x)
justifyRight(w, space, show(x))
end lambda|λ|
end script
concat(zipWith(pad, ws, xs))
end lambda|λ|
end script
Line 226:
 
 
-- TEST -----------------------------------------------------------------------
on run
script test
on lambda|λ|(n)
showFloyd(floyd(n)) & linefeed
end lambda|λ|
end script
Line 238:
 
 
-- GENERIC FUNCTIONS ----------------------------------------------------------
 
-- compose :: [(a -> a)] -> (a -> a)
on compose(fs)
script
on lambda|λ|(x)
script
on lambda|λ|(af, fa)
mReturn(f)'s lambda|λ|(a)
end lambda|λ|
end script
foldr(result, x, fs)
end lambda|λ|
end script
end compose
Line 257:
-- concat :: [[a]] -> [a] | [String] -> String
on concat(xs)
script append
on lambda(a, b)
a & b
end lambda
end script
if length of xs > 0 and class of (item 1 of xs) is string then
set unitacc to ""
else
set unitacc to {}
end if
repeat with i from 1 to lnglength of xs
foldl(append, unit, xs)
set lstacc to {}acc & item i of xs
end lambdarepeat
acc
end concat
 
Line 291 ⟶ 288:
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
Line 297 ⟶ 294:
end foldl
 
-- foldr :: (ab -> ba -> a) -> a -> [b] -> a
on foldr(f, startValue, xs)
tell mReturn(f)
Line 303 ⟶ 300:
set lng to length of xs
repeat with i from lng to 1 by -1
set v to lambda|λ|(v, item i of xs, v, i, xs)
end repeat
return v
Line 346 ⟶ 343:
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda|λ|(item i of xs, i, xs)
end repeat
return lst
Line 360 ⟶ 357:
on mapAccumL(f, acc, xs)
script
on lambda|λ|(a, x)
tell mReturn(f) to set pair to lambda|λ|(item 1 of a, x)
[item 1 of pair, (item 2 of a) & {item 2 of pair}]
end lambda|λ|
end script
foldl(result, [acc, []], xs)
end mapAccumL
 
-- min :: Ord a => a -> a -> a
on min(x, y)
if nxy < 1 or ny < 1x then
a & by
else
end ifx
end scriptif
end min
 
-- Lift 2nd class handler function into 1st class script wrapper
Line 376 ⟶ 382:
else
script
property lambda|λ| : f
end script
end if
Line 413 ⟶ 419:
if c = list then
script serialized
on lambda|λ|(v)
show(v)
end lambda|λ|
end script
Line 421 ⟶ 427:
else if c = record then
script showField
on lambda|λ|(kv)
set {k, v} to kv
k & ":" & show(v)
end lambda|λ|
end script
Line 454 ⟶ 460:
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
on zipWith(f, xs, ys)
set nxlng to min(length of xs, length of ys)
set nylst to length of ys{}
tell mReturn(f)
if nx < 1 or ny < 1 then
{}repeat with i from 1 to lng
set end of lst to lambda|λ|(item i of xs, item i of ys)
else
ifend nx < ny thenrepeat
return set lng to nxlst
end elsetell
set lng to ny
end if
set lst to {}
tell mReturn(f)
repeat with i from 1 to lng
set end of lst to lambda(item i of xs, item i of ys)
end repeat
return lst
end tell
end if
end zipWith</lang>
{{Out}}