Multiplication tables: Difference between revisions

Content added Content deleted
(→‎AppleScript :: Functional composition: generalised formatting a little to allow for higher ranges)
Line 543: Line 543:
<lang AppleScript>------------------- MULTIPLICATION TABLE -----------------
<lang AppleScript>------------------- MULTIPLICATION TABLE -----------------


-- multiplicationTable :: Int -> String
-- multiplicationTable :: Int -> Int -> String
on multiplicationTable(n)
on multiplicationTable(lower, upper)
tableText(mulTable(enumFromTo(1, n)))
set axis to enumFromTo(lower, upper)
tableText(axis, mulTable(axis))
end multiplicationTable
end multiplicationTable


Line 575: Line 576:


-- tableText :: [[Int]] -> String
-- tableText :: [[Int]] -> String
on tableText(rows)
on tableText(axis, rows)
set colWidth to 1 + (length of (|last|(|last|(rows)) as string))
set cell to replicate(colWidth, space)
script tableLine
script tableLine
Line 581: Line 584:
script tableCell
script tableCell
on |λ|(int)
on |λ|(int)
(characters -4 thru -1 of (" " & int)) as string
(characters (-colWidth) thru -1 of (cell & int)) as string
end |λ|
end |λ|
end script
end script
Line 589: Line 592:
end script
end script
set axis to {{"x"}} & rest of (item 1 of rows)
set legend to {{{"x"}} & axis}
intercalate(linefeed, map(tableLine, {axis} & rows))
intercalate(linefeed, map(tableLine, legend & rows))
end tableText
end tableText


Line 596: Line 599:
--------------------------- TEST -------------------------
--------------------------- TEST -------------------------
on run
on run
multiplicationTable(12)
multiplicationTable(1, 12) & linefeed & linefeed & ¬
multiplicationTable(30, 40)
end run
end run


Line 660: Line 664:
end if
end if
end justifyRight
end justifyRight


-- last :: [a] -> a
on |last|(xs)
item -1 of xs
end |last|




Line 685: Line 695:
end script
end script
end if
end if
end mReturn</lang>
end mReturn


-- replicate :: Int -> String -> String
on replicate(n, s)
set out to ""
if n < 1 then return out
set dbl to s
repeat while (n > 1)
if (n mod 2) > 0 then set out to out & dbl
set n to (n div 2)
set dbl to (dbl & dbl)
end repeat
return out & dbl
end replicate</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
Line 699: Line 724:
10 100 110 120
10 100 110 120
11 121 132
11 121 132
12 144</pre>
12 144

x 30 31 32 33 34 35 36 37 38 39 40
30 900 930 960 990 1020 1050 1080 1110 1140 1170 1200
31 961 992 1023 1054 1085 1116 1147 1178 1209 1240
32 1024 1056 1088 1120 1152 1184 1216 1248 1280
33 1089 1122 1155 1188 1221 1254 1287 1320
34 1156 1190 1224 1258 1292 1326 1360
35 1225 1260 1295 1330 1365 1400
36 1296 1332 1368 1404 1440
37 1369 1406 1443 1480
38 1444 1482 1520
39 1521 1560
40 1600</pre>


=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==