RosettaCodeData/Task/Repeat-a-string/AppleScript/repeat-a-string-2.applescript

21 lines
517 B
AppleScript
Raw Permalink Normal View History

2017-09-23 10:01:46 +02:00
replicate(5000, "ha")
2016-12-05 22:15:40 +01:00
2017-09-23 10:01:46 +02:00
-- Repetition by 'Egyptian multiplication' -
-- progressively doubling a list, appending
-- stages of doubling to an accumulator where needed for
-- binary assembly of a target length.
2016-12-05 22:15:40 +01:00
2017-09-23 10:01:46 +02:00
-- replicate :: Int -> String -> String
on replicate(n, s)
set out to ""
if n < 1 then return out
set dbl to s
2016-12-05 22:15:40 +01:00
repeat while (n > 1)
2017-09-23 10:01:46 +02:00
if (n mod 2) > 0 then set out to out & dbl
2016-12-05 22:15:40 +01:00
set n to (n div 2)
2017-09-23 10:01:46 +02:00
set dbl to (dbl & dbl)
2016-12-05 22:15:40 +01:00
end repeat
2017-09-23 10:01:46 +02:00
return out & dbl
end replicate