1
data Tegel = Teg Int Int
type RijTegels = [Tegel]


volleRijTegels :: RijTegels
volleRijTegels = ziptwee [21..36] (replicate 4 1 ++ replicate 4 2 ++ replicate 4 3 ++ replicate 4 4)

ziptwee :: [Int] -> [Int] -> RijTegels
ziptwee []      []    = []
ziptwee (x:xs) (y:ys) = Teg x y: ziptwee xs ys 

Now I use two functions, but I want to do it with one. I thought I could use zipWith, but I can't seem to figure out how.

volleRijTegels :: RijTegels
volleRijTegels = [zipWith (++) [1,2,3] [4,5,6]] -- here it is going wrong

I am placing the outer brackets wrong I guess, but I don't know where they should go.

Anyone who could show me how the work is done?

  • your `ziptwee` is already almost the same as `zipWith Teg` - but it won't work because you haven't singled out the case where either input list is empty, just when both are. You need to have `ziptwee [] _ = []` and `ziptwee _ [] = []`, as well as the recursive case. – Robin Zigmond Apr 03 '19 at 12:57
  • `concatMap (replicate 4) [1..4]` or `[1..4] >>= replicate 4` would be more efficient. – chepner Apr 03 '19 at 13:26

1 Answers1

1

The problem here is that you want the type RijTegels but actually the type you're creating with zipWith (++) is [[a]]. So to change that you could use the type constructor instead of the (++):

data Tegel = Teg Int Int deriving (Show)
type RijTegels = [Tegel]

volleRijTegels :: RijTegels
volleRijTegels = zipWith Teg [1,2,3] [4,5,6] -- here it is going wrong

main = print volleRijTegels

Try it online!

flawr
  • 10,814
  • 3
  • 41
  • 71