Is this reduction possible?

Can eta-reduction be applied in the lower case?

let normalise = filter (\x -> Data.Char.isLetter x || Data.Char.isSpace x ) 

I was expecting something like this:

 let normalise = filter (Data.Char.isLetter || Data.Char.isSpace) 

... But it is not

+6
source share
4 answers

Your solution does not work because (||) works with Bool values, and Data.Char.isLetter and Data.Char.isSpace are of type Char -> Bool .

pl gives you:

 $ pl "fx = ax || bx" f = liftM2 (||) ab 

Explanation: liftM2 raises (||) into the monad (->) r , so the new type is (r -> Bool) -> (r -> Bool) -> (r -> Bool) .

So, in your case, we get:

 import Control.Monad let normalise = filter (liftM2 (||) Data.Char.isLetter Data.Char.isSpace) 
+10
source
 import Control.Applicative let normalise = filter ((||) <$> Data.Char.isLetter <*> Data.Char.isSpace) 
+7
source

Another solution worth paying attention to is arrows!

 import Control.Arrow normalize = filter $ uncurry (||) . (isLetter &&& isSpace) 

&&& performs two functions (really arrows) and plugs their results into one tuple. Then we just unbreak || , so time becomes (Bool, Bool) -> Bool , and we are all done!

+5
source

You can use the monoid Any and the monoid instance for functions that return monoid values:

 import Data.Monoid import Data.Char let normalise = filter (getAny . ((Any . isLetter) `mappend` (Any . isSpace))) 
+2
source

All Articles