-1

How can i use zipWith with my own functions? I want to add square of numbers from two lists. Until now i have this:

zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]  
zipWith' _ [] _ = []  
zipWith' _ _ [] = []  
zipWith' f (x:xs) (y:ys) = f x y : zipWith' f xs ys  

func3 :: (Int, Int) -> Int 
func3 (x, y) = x^2 + y^2

I'm not really sure how this should be implemented, but when i write this zipWith' (func3) [12, 22] [12, 20] this shows up Variable not in scope: func3 :: a0 -> b0 -> c

Enlico
  • 23,259
  • 6
  • 48
  • 102
  • 3
    `func3` should have as signature `Int -> Int -> Int`. – Willem Van Onsem Jan 17 '22 at 13:31
  • 3
    Your `func3` function takes a pair (2-tuple) of Int values, while it is expected to take two *separate* Int values.That is, type `a->b->c` is one thing, and type `(a,b)->c` entirely another thing. Function `zipWith` wants the first one. – jpmarinier Jan 17 '22 at 13:33
  • Thanks, it solved. Would you post that as an answer or should i edit the post including it? – Dragos Polifronie Jan 17 '22 at 13:34
  • @DragosPolifronie: you can answer your own question in the textbox below: one is allowed to post information in a Q/A style. – Willem Van Onsem Jan 17 '22 at 13:35
  • 1
    If `func3` is a given function, and not something you can rewrite, you can use `curry func3` to get a function of the correct type. – chepner Jan 17 '22 at 13:38
  • 1
    The variable-not-in-scope error, though, looks odd. With the code above, `func3` is in scope, but it just has the wrong type. I suspect you somehow defined `zipWith'` without defining `func3`. – chepner Jan 17 '22 at 13:41

1 Answers1

2

It seems my signature was wrong. A simple Int -> Int -> Int at signature instead of (Int, Int)->Int and func3 x y instead of func3 (x, y) inside the function solved the problem