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
{start + row + 1, enumFromTo(start, start + row)}
end
end script
Line 211:
script aligned
on
script pad
on
justifyRight(w, space, show(x))
end
end script
concat(zipWith(pad, ws, xs))
end
end script
Line 226:
-- TEST
on run
script test
on
showFloyd(floyd(n)) & linefeed
end
end script
Line 238:
-- GENERIC FUNCTIONS
-- compose :: [(a -> a)] -> (a -> a)
on compose(fs)
script
on
script
on
mReturn(f)'s
end
end script
foldr(result, x, fs)
end
end script
end compose
Line 257:
-- concat :: [[a]] -> [a] | [String] -> String
on concat(xs)
a & b▼
end lambda▼
end script▼
if length of xs > 0 and class of (item 1 of xs) is string then
set
else
set
end if
▲ acc
end concat
Line 291 ⟶ 288:
set lng to length of xs
repeat with i from 1 to lng
set v to
end repeat
return v
Line 297 ⟶ 294:
end foldl
-- foldr :: (
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
end repeat
return v
Line 346 ⟶ 343:
set lst to {}
repeat with i from 1 to lng
set end of lst to
end repeat
return lst
Line 360 ⟶ 357:
on mapAccumL(f, acc, xs)
script
on
tell mReturn(f) to set pair to
[item 1 of pair, (item 2 of a) & {item 2 of pair}]
end
end script
foldl(result, [acc, []], xs)
end mapAccumL
-- min :: Ord a => a -> a -> a
on min(x, y)
else▼
end min
-- Lift 2nd class handler function into 1st class script wrapper
Line 376 ⟶ 382:
else
script
property
end script
end if
Line 413 ⟶ 419:
if c = list then
script serialized
on
show(v)
end
end script
Line 421 ⟶ 427:
else if c = record then
script showField
on
set {k, v} to kv
k & ":" & show(v)
end
end script
Line 454 ⟶ 460:
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
on zipWith(f, xs, ys)
set
set
▲ if nx < 1 or ny < 1 then
▲ else
return
end
▲ 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 zipWith</lang>
{{Out}}
|