Problem 15
Repeat each element of a list a given number of times.
Example
repeatElements 3 [1, 2, 3, 3] == [1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3]
Unit Test
import Html
import List
repeatElements : Int -> List a -> List a
repeatElements count list =
-- your implementation goes here
[]
main : Html.Html a
main =
Html.text
<| case test of
0 ->
"Your implementation passed all tests."
1 ->
"Your implementation failed one test."
x ->
"Your implementation failed " ++ (toString x) ++ " tests."
test : Int
test =
List.length
<| List.filter ((==) False)
[ repeatElements 2 [ 1, 2, 5, 5, 2, 1 ] == [ 1, 1, 2, 2, 5, 5, 5, 5, 2, 2, 1, 1 ]
, repeatElements 4 [ 1, 2 ] == [ 1, 1, 1, 1, 2, 2, 2, 2 ]
, repeatElements 4 [] == []
, repeatElements 0 [ 1, 2 ] == []
, repeatElements (-1) [ 1, 2 ] == []
, repeatElements 40 [ 1 ] == [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]
, repeatElements 4 [ "1", "2" ] == [ "1", "1", "1", "1", "2", "2", "2", "2" ]
]
Hints
- Do I need to repeat myself? "Recurse!"
- Another solution uses List.concatMap
- Fold!
Solutions
Solutions