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, drawing on a library of generic functions.
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>-- MULTIPLICATION TABLE FOR INTEGERS M TO N
<lang AppleScript>tableText(multTable(1, 12))


-- table :: Int -> Int -> [[String]]
-- multTable :: Int -> [[String]]
on table(m, n)
on multTable(m, n)
set axis to range(m, n)
set axis to enumFromTo(m, n)
script column
script column
on lambda(x)
on |λ|(x)
script row
script row
on lambda(y)
on |λ|(y)
if y < x then
if y < x then
{""}
""
else
else
{(x * y) as text}
(x * y) as string
end if
end if
end lambda
end |λ|
end script
end script
{x & map(row, axis)}
{x & map(row, axis)}
end lambda
end |λ|
end script
end script
{{"x"} & axis} & concatMap(column, axis)
{{"x"} & axis} & concatMap(column, axis)
end table
end multTable


-- TABLE DISPLAY --------------------------------------------------------------


-- tableText :: [[Int]] -> String
on run
tableText(table(1, 12))
end run


-- TABLE DISPLAY

-- tableText :: [[String]] -> String
on tableText(lstTable)
on tableText(lstTable)
script tableLine
script tableLine
on lambda(lstLine)
on |λ|(lstLine)
script tableCell
script tableCell
on lambda(cell)
on |λ|(int)
(characters -4 thru -1 of (" " & cell)) as string
(characters -4 thru -1 of (" " & int)) as string
end lambda
end |λ|
end script
end script
intercalate(" ", map(tableCell, lstLine))
intercalate(" ", map(tableCell, lstLine))
end lambda
end |λ|
end script
end script
Line 423: Line 415:




-- GENERIC LIBRARY FUNCTIONS
-- GENERIC FUNCTIONS ----------------------------------------------------------


-- concatMap :: (a -> [b]) -> [a] -> [b]
-- concatMap :: (a -> [b]) -> [a] -> [b]
on concatMap(f, xs)
on concatMap(f, xs)
script append
set lst to {}
on lambda(a, b)
set lng to length of xs
a & b
tell mReturn(f)
end lambda
repeat with i from 1 to lng
set lst to (lst & |λ|(item i of xs, i, xs))
end script
end repeat
end tell
foldl(append, {}, map(f, xs))
return lst
end concatMap
end concatMap

-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if m > n then
set d to -1
else
set d to 1
end if
set lst to {}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
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 lambda(v, item i of xs, i, xs)
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

-- 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

-- 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)
else
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 lambda(item i of xs, i, xs)
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 lambda : f
property |λ| : f
end script
end script
end if
end if
end mReturn
end mReturn</lang>

-- range :: Int -> Int -> [Int]
on range(m, n)
if n < m then
set d to -1
else
set d to 1
end if
set lst to {}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
end range

-- 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</lang>

{{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