Dair
Dair

Reputation: 16240

Having trouble with H-15

I am doing Problem 15. Which states:

(**) Replicate the elements of a list a given number of times.

    Example:

    * (repli '(a b c) 3)
    (A A A B B B C C C)

    Example in Haskell:

    > repli "abc" 3
    "aaabbbccc"

My plan was to do something like this:

repli :: [a] -> Integer -> [a]

repli [] y = []

repli (x:xs) y | appendNo x y == [] = repli(xs) y
               | otherwise = appendNo x y : (x:xs)
               where
               appendNo :: a -> Integer -> [a]
               appendNo a 0 = []
               appendNo a y = a:appendNo a (y-1)

Where I would make a function called appendNo that returns a list of 1 element y times then append it to the original list. Then take the body of the list and repeat this process until there are no more body elements left. But, I get the error:

H15.hs:6:30:
    Couldn't match type `a' with `[a]'
      `a' is a rigid type variable bound by
          the type signature for repli :: [a] -> Integer -> [a] at H15.hs:3:1
    In the return type of a call of `appendNo'
    In the first argument of `(:)', namely `appendNo x y'
    In the expression: appendNo x y : (x : xs)
Failed, modules loaded: none.

6:30 is at the on the p in appendNo in this line:

           | otherwise = appendNo x y : (x:xs)

Ok thanks dave4420 I was able to figure it out by doing:

repli :: [a] -> Integer -> [a]

repli [] y = []

repli (x:xs) y = appendNo x y ++ repli(xs) y
               where
               appendNo :: a -> Integer -> [a]
               appendNo a 0 = []
               appendNo a y = a:appendNo a (y-1)

Upvotes: 2

Views: 121

Answers (2)

Landei
Landei

Reputation: 54584

As the problem is solved, let me give you a hint: You should try to think in transformations, not in "loops". Start with some concrete values like n = 3 and list = "ABCD". Then you should think along the lines "I need every element three times". There is already a function for doing the replication, which is surprisingly called replicate. So the sentence can be translated to map (replicate 3) "ABCD", which gives you ["AAA","BBB","CCC","DDD"]. That's almost what you want, you just need to concat the elements. This gives:

repli list n = concat (map (replicate n) list)

Because this operation is very common, there is a concatMap function combining concat and map, as well as the operator (>>=) doing the same, just with flipped arguments. So a very short solution would be:

repli list n = list >>= replicate n

This can be translated to the do-notation or a list comprehension as well:

repli list n = do
     x <- list
     y <- replicate n x
     return y  

repli list n = [y | x <- list, y <- replicate n x]

Upvotes: 1

dave4420
dave4420

Reputation: 47052

       | otherwise = appendNo x y : (x:xs)

There is a type error in this line. So ask yourself:

  1. What is the type of appendNo x y?
  2. What is the type of (x:xs)?
  3. What is the type of (:)?

Then you should be able to see why they don't match up.

If you still can't see why they don't match up, ask yourself

  1. What is the type of x?
  2. What is the type of xs?
  3. What is the type of (:)?

Bear in mind that this time the types do match up.

Upvotes: 4

Related Questions