Haskell code zipWith using Maybe
haskell
Solution
You are specifying an `Integral` constraint on your function, but using `(/)` inside the function which signifies fractional division.
You probably want to use `div`, which is integral division, e.g. `3 `div` 2 == 1`. Otherwise, change the constraint from `Integral` to `Fractional` (which is what the error message is telling you to do).
Problem
I'm having trouble with this code. I'm trying to write a simple function that takes two lists and tries to divide each element of list A by the corresponding element of list B. If the element in list B is 0, it should return `Nothing`, otherwise it should return `Just (a / b)`. Here is the code: ``` divlist :: Integral a => [a] -> [a] -> [Maybe a] divlist = zipWith (\x y -> if (y /= 0) then Just (x / y) else Nothing) ``` It's probably something stupid, but I simply can't find it. EDIT: This is what ghci is reporting: ``` C:\Users\spravce\Desktop\Haskell\6.hs:16:51: error: • Could not deduce (Fractional a) arising from a use of ‘/’ from the context: Integral a bound by the type signature for: divlist :: Integral a => [a] -> [a] -> [Maybe a] at C:\Users\spravce\Desktop\Haskell\6.hs:14:1-48 Possible fix: add (Fractional a) to the context of the type signature for: divlist :: Integral a => [a] -> [a] -> [Maybe a] • In the first argument of ‘Just’, namely ‘(x / y)’ In the expression: Just (x / y) In the expression: if (y /= 0) then Just (x / y) else Nothing Failed, modules loaded: none. ``` EDIT 2: Using `div x y` instead of `x / y` just did it. :) Thanks.