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 ---------------------------------------------------------------------
 
<lang AppleScript>-- pascal :: Int -> [[Int]]
on pascal(intRows)
Line 315 ⟶ 316:
on nextRow(row)
script add
on lambda|λ|(a, b)
a + b
end lambda|λ|
end script
Line 323 ⟶ 324:
end nextRow
on lambda|λ|(xs)
xs & {nextRow(item -1 of xs)}
end lambda|λ|
end script
foldr(addRow, {{1}}, rangeenumFromTo(1, intRows - 1))
end pascal
 
-- TEST -----------------------------------------------------------------------
 
-- TEST
 
on run
set lstTriangle to pascal(7)
script spaced
on lambda|λ|(xs)
script rightAlign
on lambda|λ|(x)
text -4 thru -1 of (" " & x)
end lambda|λ|
end script
intercalate("", map(rightAlign, xs))
end lambda|λ|
end script
script indented
on lambda|λ|(a, x)
set strIndent to leftSpace of a
{rows:strIndent & x & linefeed & rows of a, leftSpace:leftSpace of a & " "}¬
strIndent & x & linefeed & rows of a, leftSpace:¬
end lambda
leftSpace of a & " "} ¬
end repeat
end lambda|λ|
end script
rows of foldr(indented, {rows:"", leftSpace:""}, map(spaced, lstTriangle))¬
{rows:"", leftSpace:""}, map(spaced, lstTriangle))
end run
 
-- GENERIC FUNCTIONS ----------------------------------------------------------
 
-- rangeenumFromTo :: Int -> Int -> [Int]
 
on rangeenumFromTo(m, n)
-- GENERIC LIBRARY FUNCTIONS
if booln < m then
set lstd to {}-1
else
set lstd to {}1
end if
set baselst to m - 1{}
repeat with i from 1m to lngn by d
set end of lst to i + base
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 lambda|λ|(v, item i of xs, i, xs)
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 lambda|λ|(item i of xs, i, xs)
end repeat
return lst
Line 388 ⟶ 411:
end map
 
-- zipWithmin :: (Ord a -=> ba -> c)a -> [a] -> [b] -> [c]
on zipWithmin(fx, xs, ysy)
setif nxy to< lengthx of xsthen
set ny to length of ysy
if nx < 1 or ny < 1 then
{}
else
{}x
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 zipWithmin
 
-- cond :: Bool -> (a -> b) -> (a -> b) -> (a -> b)
on cond(bool, f, g)
if bool then
f
else
g
end if
end cond
 
-- 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 lng to (n - m) + 1
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
end range
 
-- Lift 2nd class handler function into 1st class script wrapper
Line 441 ⟶ 427:
else
script
property lambda|λ| : f
end script
end if
end mReturn</lang>
 
-- condzipWith :: Bool(a -> (ab -> bc) -> ([a] -> [b)] -> (a -> b)[c]
on zipWith(f, xs, ys)
set lng to condmin(nxlength <of nyxs, nx,length of nyys)
set lnglst to (n - m) + 1{}
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 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>
</pre>
 
=={{header|AutoHotkey}}==
9,659

edits