Pascal's triangle: Difference between revisions

Line 273:
1 3 3 1
</pre>
 
 
=={{header|AppleScript}}==
 
 
<lang AppleScript>on run
intercalate(linefeed, ¬
map(tabSpaced, pascal(7)))
end run
 
 
-- Argument: number of Pascal's triangle rows
 
-- pascal :: Int -> [[Int]]
on pascal(n)
foldl(my addRow, {{1}}, range(1, n - 1))
end pascal
 
-- addRow [[Int]] -> _ -> Int -> [[Int]]
on addRow(lst, _, i)
lst & {nextRow(item -1 of lst)}
end addRow
 
-- nextRow :: [Int] -> [Int]
on nextRow(row)
zipWith(my add, [0] & row, row & [0])
end nextRow
 
-- add :: Int -> Int -> Int
on add(a, b)
a + b
end add
 
 
 
-- TESTING
 
-- [Text | Number] -> Text
on tabSpaced(lstWords)
intercalate(tab, lstWords)
end tabSpaced
 
 
-- GENERIC LIBRARY FUNCTIONS
 
 
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
set mf to mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to mf's lambda(v, item i of xs, i, xs)
end repeat
return v
end foldl
 
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
on zipWith(f, xs, ys)
set lng to length of xs
if lng is not length of ys then return missing value
set mf to mReturn(f)
set lst to {}
repeat with i from 1 to lng
set end of lst to mf's lambda(item i of xs, item i of ys)
end repeat
return lst
end zipWith
 
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
set mf to mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to mf's lambda(item i of xs, i, xs)
end repeat
return lst
end map
 
 
-- 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
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then return f
script
property lambda : f
end script
end mReturn
 
</lang>
 
 
{{Out}}
<pre>1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1</pre>
 
=={{header|AutoHotkey}}==
9,659

edits