Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining two functions to apply them to the same input

I'm new to Haskell and I'm still getting my head around it. I'm trying to combine two functions (isMark and isAlpha from Data.Char module in base package) as a first argument to Data.Text.filter function. What I've tried so far was:

import qualified Data.Char as C
import qualified Data.Text as T
import           Data.Text (Text)

strippedInput :: Text -> Text
strippedInput input = T.filter (C.isMark || C.isAlpha) input

which doesn't work, or

strippedInput input = T.filter (C.isMark . C.isAlpha) input

but obviously it doesn't work either, as the type of C.isAlpha is Char -> Bool which then becomes an input to C.isMark which is also of type Char -> Bool so types don't match.

I'd like to achieve the "C.isMark OR C.isAlpha" logic in the predicate but because of my very limited knowledge I've run out of ideas on how to search for the solution.

like image 766
mjarosie Avatar asked Mar 04 '23 05:03

mjarosie


1 Answers

The simplest is to make use of a lambda-expression:

strippedInput :: Text -> Text
strippedInput input = T.filter (\x -> C.isMark x || C.isAlpha x) input

You can furthermore make use of the fact that a function is an applicative functor, and thus work with:

import Control.Applicative(liftA2)

strippedInput :: Text -> Text
strippedInput input = T.filter (liftA2 (||) C.isMark C.isAlpha) input
like image 70
Willem Van Onsem Avatar answered Mar 11 '23 08:03

Willem Van Onsem