Quantcast
Channel: StackExchange Replication Questions
Viewing all articles
Browse latest Browse all 17268

Haskell concat replicate inside list comprehension

$
0
0

I'm having trouble with the following exercise :

Utilizing list comprehension, define a function with the following signature:

reproduce :: [Int] -> [Int]

This function exchanges the every number in a list by x copies of itself.

eg:

input: reproduce[3,5,1]
output: [3,3,3,5,5,5,5,5,1]

I have done the following:

reproduce :: [Int] -> [Int]
reproduce xs = [ x | x <- [1,2..10 ] , x `elem` xs , replicate x x ]

My line of thought was making x belong to a small interval, x had to be an element of the original list so using elemx would only be added if it did belong to the original list, and replicating x , x times, to get the x copies of himself.

When i try to load this function i get the following error message:

Couldn't match expected type `Bool' with actual type `[Int]'
In the return type of a call of `replicate'
In the expression: replicate x x
In a stmt of a list comprehension: replicate x x

I have no idea how to fix this error, I'm assuming it has something to do with the elem part but I don't know what.

NOTE: This is a beginners exercise and thus should be solved in a simple manner.


Viewing all articles
Browse latest Browse all 17268

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>