Pascal's triangle: Difference between revisions

Content deleted Content added
Hout (talk | contribs)
→‎{{header|AppleScript}}: Updated primitives and formatting
Line 277:
=={{header|AppleScript}}==
 
<lang AppleScript>-- pascal :: Int -> [[Int]]
 
<lang AppleScript>
-- pascal :: Int -> [[Int]]
on pascal(intRows)
-- addRow [[Int]] -> [[Int]]
script addRow
-- nextRow :: [Int] -> [Int]
on nextRow(row)
-- add :: Int -> Int -> Int
script add
on lambda(a, b)
Line 300 ⟶ 296:
end script
foldlfoldr(addRow, {{1}}, range(1, intRows - 1))
end pascal
 
Line 309 ⟶ 305:
set lstTriangle to pascal(7)
script spacedAndIndentedspaced
on lambda(xs)
property width : (length of intercalate(" ", (item -1 of lstTriangle)))
script rightAlign
-- [Text | Number] -> Text on lambda(x)
on lambda text -4 thru -1 of (lst" " & x)
set strLine to intercalate(" end ", lst)lambda
set n to (n divend 2)script
nreps(space, width - ((length of strLine) / 2)) & strLine
set s to (s & s)
intercalate("", map(rightAlign, xs))
end lambda
end script
script indented
intercalate(linefeed, map(spacedAndIndented, lstTriangle))
on lambda(a, x)
set strIndent to leftSpace of a
{rows:strIndent & x & linefeed & rows of a, leftSpace:leftSpace of a & " "}
end repeatlambda
end repeatscript
rows of foldr(indented, {rows:"", leftSpace:""}, map(spaced, lstTriangle))
end run
 
Line 326 ⟶ 332:
-- GENERIC LIBRARY FUNCTIONS
 
-- foldlfoldr :: (a -> b -> a) -> a -> [b] -> a
on foldlfoldr(f, startValue, xs)
set mf totell mReturn(f)
set ov to ""startValue
set vlng to startValuelength of xs
set repeat with i from lng to length1 ofby xs-1
set v to mf's lambda(v, item i of xs, i, xs)
repeat with i from 1 to lng
end repeat
set v to mf's lambda(v, item i of xs, i, xs)
end repeat return v
returnend vtell
end foldlfoldr
 
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
set mf totell 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 tell
end map
 
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
Line 350 ⟶ 368:
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
 
-- String -> Int -> String
on nreps(s, n)
set o to ""
if n < 1 then return o
repeat while (n > 1)
if (n mod 2) > 0 then set o to o & s
set n to (n div 2)
set s to (s & s)
end repeat
return o & s
end nreps
 
 
Line 406 ⟶ 400:
end if
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 1 6 6 15 20 15 6 1</pre>
</pre>
 
=={{header|AutoHotkey}}==