How to give
break (== ' ') xxs
multiple boolean parameters without changing the definition? Or that is inevitable. For instance
break (== (' ' || '\t' || '\n')) xss
foldl
and foldr
are one way, but here they are not applicable, or at least I haven't been able to use them.
Is this what you are looking for?
break (\x -> x == ' ' || x == '\t' || x == '\n') xss
Your current question isn't really about multiple parameters, but different predicates for break
. Let's consider break
's type for a second:
break :: (a -> Bool) -> [a] -> ([a], [a])
This type of function (a -> Bool)
is called a predicate. You can use anything that uses the right a
and returns a Bool
. That's why your first code typechecks:
ghci> :t (== ' ')
(== ' ') :: Char -> Bool
Now, if you want to use several characters as possible break symbols, you need to use another Char -> Bool
. If the characters are fixed (and you don't have many of them), you can use a list [Char]
and elem
:
yourCharacters = [' ','\t','\n']
predicate :: Char -> Bool
predicate c = c `elem` yourCharacters
We can use this as predicate for break
:
myBreak = break (`elem` yourCharacters)
At this point, you could also change myBreak
so that it takes a list of characters, at which point you'll get:
myBreak' :: [Char] -> [Char] -> ([Char], [Char])
myBreak' limiters = break (`elem` limiters)
-- myBreak' limiters str = break (`elem` limiters) str
HTH
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With