Redundancy warning if warning in haskell

So, I'm following the tutorial. Teach you Haskell for Great Good! , and so far I absolutely love Haskell. But in one of the functions mentioned in the tutorial, I get a warning that the if statement is redundant.

Edit: Let me understand that the purpose of the function is to act in exactly the same way as the elem function (the one provided by default by Haskell).

Here's the original function:

elem' :: (Eq a) => a -> [a] -> Bool elem' y ys = foldl (\acc x -> if x == y then True else acc) False ys 

Initially, there were two warnings: one was a decrease in eta, so I removed ys from the beginning and end of the function name to get:

 elem' :: (Eq a) => a -> [a] -> Bool elem' y = foldl (\acc x -> if x == y then True else acc) False 

Now I tried to reduce the function to the following, and this leads to an error:

 elem' :: (Eq a) => a -> [a] -> Bool elem' y = foldl (\acc x -> x == y) 

I think I'm just very new to Haskell and don't see the obvious solution. Can someone tell me which code change will work correctly and still remove the compiler warning?

+4
source share
2 answers

if x == y then True else acc matches x == y || acc x == y || acc .

+10
source

Entering your last definition in GHCi without type annotation:

 Prelude> let elem' y = foldl (\acc x -> x == y) Prelude> :t elem' elem' :: (Eq b) => b -> Bool -> [b] -> Bool 

It does not match the declared type.

You forgot False at the end! If you add it to:

 Prelude> let elem' y = foldl (\acc x -> x == y) False -- note the False at the end Prelude> :t elem' elem' :: (Eq b) => b -> [b] -> Bool 

He has the right type!

+4
source

Source: https://habr.com/ru/post/1380200/


All Articles