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