Difference between revisions of "99 questions/Solutions/14"

From HaskellWiki
Jump to: navigation, search
 
Line 17: Line 17:
 
</haskell>
 
</haskell>
  
or, using concatMap:
+
or, using <hask>concatMap</hask>:
 
<haskell>
 
<haskell>
 
dupli = concatMap (\x -> [x,x])
 
dupli = concatMap (\x -> [x,x])
 
</haskell>
 
</haskell>
  
also using concatMap:
+
also using <hask>concatMap</hask>:
 
<haskell>
 
<haskell>
 
dupli = concatMap (replicate 2)
 
dupli = concatMap (replicate 2)
 
</haskell>
 
</haskell>
 
          
 
          
or, using foldr:
+
or, using <hask>foldr</hask>:
 
<haskell>
 
<haskell>
 
dupli = foldr (\ x xs -> x : x : xs) []
 
dupli = foldr (\ x xs -> x : x : xs) []
 
</haskell>
 
</haskell>

Revision as of 03:55, 18 November 2010

(*) Duplicate the elements of a list.

dupli [] = []
dupli (x:xs) = x:x:dupli xs

or, using list comprehension syntax:

dupli list = concat [[x,x] | x <- list]

or, using the list monad:

dupli xs = xs >>= (\x -> [x,x])

or, using concatMap:

dupli = concatMap (\x -> [x,x])

also using concatMap:

dupli = concatMap (replicate 2)

or, using foldr:

dupli = foldr (\ x xs -> x : x : xs) []