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 lambda(start, row)
on |λ|(start, row)
{start + row + 1, enumFromTo(start, start + row)}
{start + row + 1, enumFromTo(start, start + row)}
end lambda
end |λ|
end script
end script
Line 211: Line 211:
script aligned
script aligned
on lambda(xs)
on |λ|(xs)
script pad
script pad
on lambda(w, x)
on |λ|(w, x)
justifyRight(w, space, show(x))
justifyRight(w, space, show(x))
end lambda
end |λ|
end script
end script
concat(zipWith(pad, ws, xs))
concat(zipWith(pad, ws, xs))
end lambda
end |λ|
end script
end script
Line 226: Line 226:




-- TEST -----------------------------------------------------------------------
-- TEST ----------------------------------------------------------------------
on run
on run
script test
script test
on lambda(n)
on |λ|(n)
showFloyd(floyd(n)) & linefeed
showFloyd(floyd(n)) & linefeed
end lambda
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 lambda(x)
on |λ|(x)
script
script
on lambda(a, f)
on |λ|(f, a)
mReturn(f)'s lambda(a)
mReturn(f)'s |λ|(a)
end lambda
end |λ|
end script
end script
foldr(result, x, fs)
foldr(result, x, fs)
end lambda
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)
a & b
end lambda
end script
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 unit to ""
set acc to ""
else
else
set unit to {}
set acc to {}
end if
end if
repeat with i from 1 to length of xs
foldl(append, unit, xs)
set acc to acc & item i of xs
end repeat
acc
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 lambda(v, item i of xs, i, xs)
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 :: (a -> b -> a) -> a -> [b] -> a
-- 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 lambda(v, item i of xs, i, xs)
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 lambda(item i of xs, i, xs)
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 lambda(a, x)
on |λ|(a, x)
tell mReturn(f) to set pair to lambda(item 1 of a, x)
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 lambda
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)
if y < x then
y
else
x
end if
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 lambda : f
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 lambda(v)
on |λ|(v)
show(v)
show(v)
end lambda
end |λ|
end script
end script
Line 421: Line 427:
else if c = record then
else if c = record then
script showField
script showField
on lambda(kv)
on |λ|(kv)
set {k, v} to kv
set {k, v} to kv
k & ":" & show(v)
k & ":" & show(v)
end lambda
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 nx to length of xs
set lng to min(length of xs, length of ys)
set ny to length of ys
set lst to {}
tell mReturn(f)
if nx < 1 or ny < 1 then
{}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, item i of ys)
else
if nx < ny then
end repeat
set lng to nx
return lst
else
end tell
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>
end zipWith</lang>
{{Out}}
{{Out}}