Lists and List Comprehensions
- Using either Kapulet's or Haskell's list comprehension syntax, write an expression that transforms
[3, 1, 0, 2]
into[3, 3, 3, 1, 2, 2]
.
Here is a hint
Define a function dup (n, x)
that creates a list of n copies of x
. Then use list comprehensions to transform [3, 1, 0, 2]
into [[3, 3, 3], [1], [], [2, 2]]
. Then use join
to "flatten" the result.