https://wiki.haskell.org/api.php?action=feedcontributions&user=Smiley325&feedformat=atom
HaskellWiki - User contributions [en]
2024-03-29T00:47:20Z
User contributions
MediaWiki 1.35.5
https://wiki.haskell.org/index.php?title=99_questions/Solutions/15&diff=37964
99 questions/Solutions/15
2010-12-20T22:28:50Z
<p>Smiley325: </p>
<hr />
<div>(**) Replicate the elements of a list a given number of times.<br />
<br />
<haskell><br />
repli :: [a] -> Int -> [a]<br />
repli xs n = concatMap (replicate n) xs<br />
</haskell><br />
<br />
or, in [[Pointfree]] style:<br />
<haskell><br />
repli = flip $ concatMap . replicate<br />
</haskell><br />
<br />
alternatively, without using the <hask>replicate</hask> function:<br />
<haskell><br />
repli :: [a] -> Int -> [a]<br />
repli xs n = concatMap (take n . repeat) xs<br />
</haskell></div>
Smiley325
https://wiki.haskell.org/index.php?title=99_questions/Solutions/14&diff=37963
99 questions/Solutions/14
2010-12-20T22:26:35Z
<p>Smiley325: </p>
<hr />
<div>(*) Duplicate the elements of a list.<br />
<br />
<haskell><br />
dupli [] = []<br />
dupli (x:xs) = x:x:dupli xs<br />
</haskell><br />
<br />
or, using list comprehension syntax:<br />
<br />
<haskell><br />
dupli list = concat [[x,x] | x <- list]<br />
</haskell><br />
<br />
or, using the list monad:<br />
<haskell><br />
dupli xs = xs >>= (\x -> [x,x])<br />
</haskell><br />
<br />
or, using <hask>concatMap</hask>:<br />
<haskell><br />
dupli = concatMap (\x -> [x,x])<br />
</haskell><br />
<br />
also using <hask>concatMap</hask>:<br />
<haskell><br />
dupli = concatMap (replicate 2)<br />
</haskell><br />
<br />
or, using <hask>foldl</hask>:<br />
<haskell><br />
dupli = foldl (\acc x -> acc ++ [x,x]) []<br />
</haskell><br />
<br />
or, using <hask>foldr</hask>:<br />
<haskell><br />
dupli = foldr (\ x xs -> x : x : xs) []<br />
</haskell></div>
Smiley325