haskellzipwith

How to use in Haskell zipWith correctly to create a [ [ int int ] ] list?


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?


Solution

  • 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!