Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple parameters of a function

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.

like image 382
Dimitar Avatar asked Dec 04 '22 03:12

Dimitar


2 Answers

Is this what you are looking for?

break (\x -> x == ' ' || x == '\t' || x == '\n') xss
like image 150
Matt Avatar answered Jan 03 '23 02:01

Matt


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

like image 23
Zeta Avatar answered Jan 03 '23 04:01

Zeta