Pascal's triangle: Difference between revisions
→{{header|AppleScript}}: Updated primitives
m (→{{header|Sidef}}: updated code) |
(→{{header|AppleScript}}: Updated primitives) |
||
Line 308:
=={{header|AppleScript}}==
<lang AppleScript>-- PASCAL ---------------------------------------------------------------------
on pascal(intRows)
Line 315 ⟶ 316:
on nextRow(row)
script add
on
a + b
end
end script
Line 323 ⟶ 324:
end nextRow
on
xs & {nextRow(item -1 of xs)}
end
end script
foldr(addRow, {{1}},
end pascal
-- TEST -----------------------------------------------------------------------
on run
set lstTriangle to pascal(7)
script spaced
on
script rightAlign
on
text -4 thru -1 of (" " & x)
end
end script
intercalate("", map(rightAlign, xs))
end
end script
script indented
on
set strIndent to leftSpace of a
{rows:
strIndent & x & linefeed & rows of a, leftSpace:¬
end lambda▼
leftSpace of a & " "} ¬
end script
rows of foldr(indented,
{rows:"", leftSpace:""}, map(spaced, lstTriangle))
end run
-- GENERIC FUNCTIONS ----------------------------------------------------------
else▼
end if▼
end repeat▼
return lst▼
end enumFromTo
-- foldr :: (a -> b -> a) -> a -> [b] -> a
Line 370 ⟶ 385:
set lng to length of xs
repeat with i from lng to 1 by -1
set v to
end repeat
return v
end tell
end foldr
-- intercalate :: Text -> [Text] -> Text▼
on intercalate(strText, lstText)▼
set {dlm, my text item delimiters} to {my text item delimiters, strText}▼
set strJoined to lstText as text▼
set my text item delimiters to dlm▼
return strJoined▼
end intercalate▼
-- map :: (a -> b) -> [a] -> [b]
Line 382 ⟶ 405:
set lst to {}
repeat with i from 1 to lng
set end of lst to
end repeat
return lst
Line 388 ⟶ 411:
end map
--
on
{}▼
else
set lng to cond(nx < ny, nx, ny)▼
▲ 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
-- cond :: Bool -> (a -> b) -> (a -> b) -> (a -> b)▼
▲ if bool then
▲ else
▲ end if
▲-- intercalate :: Text -> [Text] -> Text
▲on intercalate(strText, lstText)
▲ set {dlm, my text item delimiters} to {my text item delimiters, strText}
▲ set strJoined to lstText as text
▲ set my text item delimiters to dlm
▲ return strJoined
▲end intercalate
▲-- range :: Int -> Int -> [Int]
▲on range(m, n)
▲ set base to m - 1
▲ set lst to {}
▲ repeat with i from 1 to lng
▲ set end of lst to i + base
▲ end repeat
▲ return lst
-- Lift 2nd class handler function into 1st class script wrapper
Line 441 ⟶ 427:
else
script
property
end script
end if
end mReturn
on zipWith(f, xs, ys)
end repeat
end zipWith</lang>
{{Out}}
<pre> 1
Line 453 ⟶ 450:
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1</pre>
=={{header|AutoHotkey}}==
|