Multiplication tables: Difference between revisions
→{{header|AppleScript}}: Subheaders: 1. Iteration 2. Functional composition – (and some updated primitives)
(Added FreeBASIC) |
(→{{header|AppleScript}}: Subheaders: 1. Iteration 2. Functional composition – (and some updated primitives)) |
||
Line 291:
=={{header|AppleScript}}==
===Iteration===
<lang AppleScript >set n to 12 -- Size of table.
repeat with x from 0 to n
Line 329 ⟶ 330:
"</pre>
===Functional composition===
As an alternative to iteration, we could also write the top level more declaratively, drawing on a library of generic functions.
{{trans|JavaScript}} (ES5 functional version)
<lang AppleScript>-- MULTIPLICATION TABLE FOR INTEGERS M TO N
-- table :: Int -> Int -> [[String]]
Line 403 ⟶ 401:
end concatMap
-- foldl :: (a -> b -> a) -> a -> [b] -> a▼
on foldl(f, startValue, xs)▼
set v to startValue
set lng to length of xs▼
repeat with i from 1 to lng
set v to lambda(v, item i of xs, i, xs)
end tell
end foldl
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to
end repeat
return lst
end tell
end map
-- Lift 2nd class handler function into 1st class script wrapper ▼
▲-- foldl :: (a -> b -> a) -> a -> [b] -> a
-- mReturn :: Handler -> Script▼
▲on foldl(f, startValue, xs)
▲ else
▲ set lng to length of xs
end
end
-- 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▼
-- range :: Int -> Int -> [Int]
on range(m, n)
else
set d to 1
end if
set lst to {}
repeat with i from m to n by d
Line 446 ⟶ 451:
end range
▲-- intercalate :: Text -> [Text] -> Text
▲-- Lift 2nd class handler function into 1st class script wrapper
▲on intercalate(strText, lstText)
▲-- mReturn :: Handler -> Script
▲ set {dlm, my text item delimiters} to {my text item delimiters, strText}
▲on mReturn(f)
▲ set strJoined to lstText as text
▲ if class of f is script then return f
▲ set my text item delimiters to dlm
return strJoined
▲end intercalate</lang>
▲ end script
{{Out}}
|