1
votes

I am looking for a function which breaks a string (based on own conditions for example "ab", "c", "3", "21") "abc321c" into this string ("ab","c","3","21","c").

The break function in haskell takes only one condition(and only takes a char and not a string) and only apply it to the first char in the list.

*Main> break ('-'==) "teasd-das-d"
("teasd","-das-d")
2
It's unclear how you want this function to operate. Why was the string "abc321" split the way it was?ApproachingDarknessFish

2 Answers

2
votes

You can write it explicitly like this:

break (\c -> (isUpper c || isDigit c)) ...

or this way:

break (\c -> or $ map ($c) [isUpper, isDigit]) ...

which means if you had this helper function:

orF :: [a -> Bool] -> a -> Bool
orF fs a = or $ map ($a) fs

then the break call becomes:

break (orF [isUpper,isDigit, (=='-')]) ...

There are probably some category-theory operators which could be used here, but this is a direct approach.

Update

If you want to get fancy, you can do this:

import Control.Applicative

break (liftA2 (||) isUpper isDigit) ...

To chain more than one condition together, perhaps define an operator:

(|||) = liftA2 (||)

and then:

break (isUpper ||| isDigit ||| (=='-')) ...
0
votes

You might be looking for splitRegex in the regex-compat library

import Text.Regex

> splitRegex (mkRegex "-") "eat-more-noodles"
["eat", "more", "noodles"]
> splitReges (mkRegex "(-|\\+)") "also+eat-barley"
["also", "eat", "barley"]

As you can see, regular expressions can allow for richer separators....