Reputation: 1
replicatee :: [a] -> Int -> [a]
replicatee [] _ = []
replicatee xs 0 = []
replicatee (x:xs) n = x:replicatee (x:xs) (n-1): replicatee xs n
So this is my code for replicating a an element in a list n times, the compler keeps showing an error :
Couldnt match type 'a'with [a], I'm seriously confused, please help out.
Edit : what i want my function to do is this: replicatee [1,2,3,4] 2
[1,1,2,2,3,3,4,4]
Upvotes: 0
Views: 76
Reputation: 531868
replicatee :: [a] -> Int -> [a]
replicatee [] _ = []
replicatee xs 0 = []
replicatee (x:xs) n = x:replicatee (x:xs) (n-1): replicatee xs n
The problem is that replicatee
returns a value of type [a]
, but you try to add that to another list of type [a]
using (:) :: a -> [a] -> [a]
. From a type-checking perspective, you need to use (++)
, not (:)
:
replicatee xs'@(x:xs) n = x : (replicatee xs' (n-1) ++ replicatee xs n)
Whether it does what you intended is another matter. Based on your description, Mikkel provides the right answer.
Upvotes: 0
Reputation: 782
I might have misunderstood your intention, but maybe you meant something like this:
replicatee :: a -> Int -> [a]
replicatee _ 0 = []
replicatee x n = x:replicatee x (n-1)
Upvotes: 1