Pascal's triangle: Difference between revisions
Content added Content deleted
m (→{{header|Nim}}) |
m (→{{header|AppleScript}}: Tidied) |
||
Line 351: | Line 351: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
Drawing n rows from a generator: |
Drawing n rows from a generator: |
||
<lang AppleScript>-------------------- PASCAL'S TRIANGLE ------------------- |
|||
<lang AppleScript> |
|||
-- pascal :: Generator [[Int]] |
-- pascal :: Generator [[Int]] |
||
on pascal() |
on pascal() |
||
Line 366: | Line 365: | ||
--------------------------- TEST ------------------------- |
|||
on run |
on run |
||
showPascal(take(7, pascal())) |
showPascal(take(7, pascal())) |
||
end run |
end run |
||
------------------------ FORMATTING ---------------------- |
|||
-- showPascal :: [[Int]] -> String |
-- showPascal :: [[Int]] -> String |
||
Line 383: | Line 385: | ||
------------------------- GENERIC ------------------------ |
|||
-- center :: Int -> Char -> String -> String |
-- center :: Int -> Char -> String -> String |
||
Line 400: | Line 402: | ||
end if |
end if |
||
end |center| |
end |center| |
||
-- intercalate :: String -> [String] -> String |
-- intercalate :: String -> [String] -> String |
||
on intercalate(sep, xs) |
on intercalate(sep, xs) |
||
set {dlm, my text item delimiters} to |
set {dlm, my text item delimiters} to ¬ |
||
{my text item delimiters, sep} |
|||
set s to xs as text |
set s to xs as text |
||
set my text item delimiters to dlm |
set my text item delimiters to dlm |
||
return s |
return s |
||
end intercalate |
end intercalate |
||
-- iterate :: (a -> a) -> a -> Generator [a] |
-- iterate :: (a -> a) -> a -> Generator [a] |
||
Line 424: | Line 429: | ||
end script |
end script |
||
end iterate |
end iterate |
||
-- length :: [a] -> Int |
-- length :: [a] -> Int |
||
Line 434: | Line 440: | ||
end if |
end if |
||
end |length| |
end |length| |
||
-- map :: (a -> b) -> [a] -> [b] |
-- map :: (a -> b) -> [a] -> [b] |
||
Line 446: | Line 453: | ||
end tell |
end tell |
||
end map |
end map |
||
-- min :: Ord a => a -> a -> a |
-- min :: Ord a => a -> a -> a |
||
Line 456: | Line 464: | ||
end min |
end min |
||
-- Lift 2nd class handler function into 1st class script wrapper |
|||
-- mReturn :: First-class m => (a -> b) -> m (a -> b) |
-- mReturn :: First-class m => (a -> b) -> m (a -> b) |
||
on mReturn(f) |
on mReturn(f) |
||
-- 2nd class handler function lifted into 1st class script wrapper. |
|||
if script is class of f then |
|||
f |
f |
||
else |
else |
||
Line 467: | Line 476: | ||
end if |
end if |
||
end mReturn |
end mReturn |
||
-- plus :: Num -> Num -> Num |
-- plus :: Num -> Num -> Num |
||
Line 472: | Line 483: | ||
a + b |
a + b |
||
end plus |
end plus |
||
-- Egyptian multiplication - progressively doubling a list, appending |
-- Egyptian multiplication - progressively doubling a list, appending |
||
Line 489: | Line 501: | ||
return out & dbl |
return out & dbl |
||
end replicate |
end replicate |
||
-- take :: Int -> [a] -> [a] |
-- take :: Int -> [a] -> [a] |
||
Line 516: | Line 529: | ||
end if |
end if |
||
end take |
end take |
||
-- unlines :: [String] -> String |
-- unlines :: [String] -> String |
||
Line 525: | Line 539: | ||
str |
str |
||
end unlines |
end unlines |
||
-- unwords :: [String] -> String |
-- unwords :: [String] -> String |
||
on unwords(xs) |
on unwords(xs) |
||
set {dlm, my text item delimiters} to |
set {dlm, my text item delimiters} to ¬ |
||
{my text item delimiters, space} |
|||
set s to xs as text |
set s to xs as text |
||
set my text item delimiters to dlm |
set my text item delimiters to dlm |
||
return s |
return s |
||
end unwords |
end unwords |
||
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] |
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] |