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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s