Multiplication tables: Difference between revisions
Content added Content deleted
(→{{header|Haskell}}: (Rearranged table function)) |
(→AppleScript: updated primitives) |
||
Line 366: | Line 366: | ||
===Functional composition=== |
===Functional composition=== |
||
As an alternative to iteration, we could also write the top level more declaratively, |
As an alternative to iteration, we could also write the top level more declaratively, composing a solution from a set of generic functions. |
||
{{trans|JavaScript}} (ES5 functional version) |
{{trans|JavaScript}} (ES5 functional version) |
||
<lang AppleScript> |
<lang AppleScript>tableText(multTable(1, 12)) |
||
-- |
-- multTable :: Int -> [[String]] |
||
on |
on multTable(m, n) |
||
set axis to |
set axis to enumFromTo(m, n) |
||
script column |
script column |
||
on |
on |λ|(x) |
||
script row |
script row |
||
on |
on |λ|(y) |
||
if y < x then |
if y < x then |
||
"" |
|||
else |
else |
||
(x * y) as string |
|||
end if |
end if |
||
end |
end |λ| |
||
end script |
end script |
||
{x & map(row, axis)} |
{x & map(row, axis)} |
||
end |
end |λ| |
||
end script |
end script |
||
{{"x"} & axis} & concatMap(column, axis) |
{{"x"} & axis} & concatMap(column, axis) |
||
end |
end multTable |
||
-- TABLE DISPLAY -------------------------------------------------------------- |
|||
⚫ | |||
on run |
|||
⚫ | |||
tableText(table(1, 12)) |
|||
⚫ | |||
end run |
|||
-- TABLE DISPLAY |
|||
⚫ | |||
on tableText(lstTable) |
on tableText(lstTable) |
||
script tableLine |
script tableLine |
||
on |
on |λ|(lstLine) |
||
script tableCell |
script tableCell |
||
on |
on |λ|(int) |
||
(characters -4 thru -1 of (" " & |
(characters -4 thru -1 of (" " & int)) as string |
||
end |
end |λ| |
||
end script |
end script |
||
intercalate(" ", map(tableCell, lstLine)) |
intercalate(" ", map(tableCell, lstLine)) |
||
end |
end |λ| |
||
end script |
end script |
||
Line 423: | Line 415: | ||
-- GENERIC |
-- GENERIC FUNCTIONS ---------------------------------------------------------- |
||
-- concatMap :: (a -> [b]) -> [a] -> [b] |
-- concatMap :: (a -> [b]) -> [a] -> [b] |
||
on concatMap(f, xs) |
on concatMap(f, xs) |
||
set lst to {} |
|||
set lng to length of xs |
|||
tell mReturn(f) |
|||
repeat with i from 1 to lng |
|||
set lst to (lst & |λ|(item i of xs, i, xs)) |
|||
⚫ | |||
end repeat |
|||
⚫ | |||
foldl(append, {}, map(f, xs)) |
|||
⚫ | |||
end concatMap |
end concatMap |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end enumFromTo |
|||
-- foldl :: (a -> b -> a) -> a -> [b] -> a |
-- foldl :: (a -> b -> a) -> a -> [b] -> a |
||
Line 442: | Line 449: | ||
set lng to length of xs |
set lng to length of xs |
||
repeat with i from 1 to lng |
repeat with i from 1 to lng |
||
set v to |
set v to |λ|(v, item i of xs, i, xs) |
||
end repeat |
end repeat |
||
return v |
return v |
||
end tell |
end tell |
||
end foldl |
end foldl |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
return strJoined |
|||
⚫ | |||
-- justifyRight :: Int -> Char -> Text -> Text |
|||
on justifyRight(n, cFiller, strText) |
|||
if n > length of strText then |
|||
text -n thru -1 of ((replicate(n, cFiller) as text) & strText) |
|||
⚫ | |||
strText |
|||
end if |
|||
end justifyRight |
|||
-- map :: (a -> b) -> [a] -> [b] |
-- map :: (a -> b) -> [a] -> [b] |
||
Line 454: | Line 478: | ||
set lst to {} |
set lst to {} |
||
repeat with i from 1 to lng |
repeat with i from 1 to lng |
||
set end of lst to |
set end of lst to |λ|(item i of xs, i, xs) |
||
end repeat |
end repeat |
||
return lst |
return lst |
||
Line 467: | Line 491: | ||
else |
else |
||
script |
script |
||
property |
property |λ| : f |
||
end script |
end script |
||
end if |
end if |
||
end mReturn |
end mReturn</lang> |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
else |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end range |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
{{Out}} |
{{Out}} |
||
<pre> x 1 2 3 4 5 6 7 8 9 10 11 12 |
<pre> x 1 2 3 4 5 6 7 8 9 10 11 12 |
||
1 1 2 3 4 5 6 7 8 9 10 11 12 |
1 1 2 3 4 5 6 7 8 9 10 11 12 |