Problem 14

99 Haskell Problems

Problem 14

(*) Duplicate the elements of a list.

Example:

* (dupli '(a b c c d))
(A A B B C C C C D D)

Example in Haskell:

> dupli [1, 2, 3]
[1,1,2,2,3,3]

Solution:

dupli :: [a] -> [a]
dupli [] = []
dupli (x:xs) = [x,x]++(dupli xs)

This three lines of code really show how powerful functional language like Haskell can do to help thinking on of the strategy and algorithm.

Advertisements