Is eta reduction possible?

社会主义新天地 提交于 2019-12-01 05:22:52

Your solution doesn't work, because (||) works on Bool values, and Data.Char.isLetter and Data.Char.isSpace are of type Char -> Bool.

pl gives you:

$ pl "f x = a x || b x"
f = liftM2 (||) a b

Explanation: liftM2 lifts (||) to the (->) r monad, so it's new type is (r -> Bool) -> (r -> Bool) -> (r -> Bool).

So in your case we'll get:

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

Another solution worth looking at involves arrows!

import Control.Arrow

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

&&& takes two functions (really arrows) and zips together their results into one tuple. We then just uncurry || so it's time becomes (Bool, Bool) -> Bool and we're all done!

You could take advantage of the Any monoid and the monoid instance for functions returning monoid values:

import Data.Monoid
import Data.Char

let normalise = filter (getAny . ((Any . isLetter) `mappend` (Any . isSpace)))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!