Repeat a string: Difference between revisions

Content added Content deleted
(→‎{{header|JavaScript}}: Updated primitives)
(→‎{{header|AppleScript}}: Updated primitives)
Line 98: Line 98:


{{trans|JavaScript}}
{{trans|JavaScript}}
<lang AppleScript>on run
<lang AppleScript>replicate(5000, "ha")
nreps("ha", 50000)
end run


-- Repetition by 'Egyptian multiplication' -
-- progressively doubling a list, appending
-- stages of doubling to an accumulator where needed for
-- binary assembly of a target length.


-- String -> Int -> String
-- replicate :: Int -> String -> String
on nreps(s, n)
on replicate(n, s)
set o to ""
set out to ""
if n < 1 then return o
if n < 1 then return out
set dbl to s
repeat while (n > 1)
repeat while (n > 1)
if (n mod 2) > 0 then set o to o & s
if (n mod 2) > 0 then set out to out & dbl
set n to (n div 2)
set n to (n div 2)
set s to (s & s)
set dbl to (dbl & dbl)
end repeat
end repeat
return o & s
return out & dbl
end replicate</lang>
end nreps
</lang>


=={{header|Applesoft BASIC}}==
=={{header|Applesoft BASIC}}==