Repeat a string: Difference between revisions

m
Line 812:
<pre>"hahahahaha"</pre>
 
As the number of repetitions increasesgrows, however, it may become more efficient to repeat by progressive duplication (mappend to self), mappending to an accumulator only where required for binary composition of the target length. (i.e. Rhind Papyrus 'Egyptian' or 'Ethiopian' multiplication):
 
<lang haskell>import Data.Tuple (swap)
Line 823:
foldr
(\(d, x) a ->
if d > 0 -- Is this power of 2 needed for the binary compositionrecomposition ?
then mappend a x
else a)
Line 831:
(\h ->
if h > 0
then Just $ swap (quotRem h 2) -- Binary decomposition of n
else Nothing)
n)
9,659

edits