Pascal's triangle: Difference between revisions
→{{header|AppleScript}}: Slight simplification - closure by direct nesting
m (→using a lazy sequence generator: add newlines) |
(→{{header|AppleScript}}: Slight simplification - closure by direct nesting) |
||
Line 279:
<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)
a + b
end lambda
end script
zipWith(add, [0] & row, row & [0])
end nextRow
on lambda(xs)
xs & {nextRow(item -1 of xs)}
end lambda
end script
foldl(addRow, {{1}}, range(1, intRows - 1))
end pascal
-- TEST
on run
set lstTriangle to pascal(7)
script spacedAndIndented
property width : (length of intercalate(" ", (item -1 of lstTriangle)))
-- [Text | Number] -> Text
on lambda(lst)
set strLine to intercalate(" ", lst)
nreps(space, width - ((length of strLine) / 2)) & strLine
end lambda
end script
intercalate(linefeed, map(spacedAndIndented, lstTriangle))
end run
-- GENERIC LIBRARY FUNCTIONS
-- foldl :: (a -> b -> a) -> a -> [b] -> a
Line 377 ⟶ 361:
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 401 ⟶ 398:
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
else
property lambda : f
end script
end if
end mReturn</lang>
|