[Haskell-beginners] How to remove leading and trailing non-alpha characters, and multiple consecutive spaces?

Denis Kasak denis.kasak at gmail.com
Fri Jun 7 01:31:45 CEST 2013


On 7 June 2013 01:21, Denis Kasak <denis.kasak at gmail.com> wrote:
> On 7 June 2013 01:14, Tim Perry <tim.v2.0 at gmail.com> wrote:
>> Denis' version doesn't work for names containing hyphens or apostrophes. The
>> original works for both....  However, the original explicitly assumes there
>> are always at least two names and gives erroneous data if there are more
>> than two. Output below shows the failure on hyphenated names.
>
> Well, yes, I explicitly did not want to dwell on my (potential)
> implicit assumptions and just handled the cases that where visible in
> the problem example or were made explicit by the original poster.
> Adding additional special behaviour for hypens and apostrophes would
> be trivial, though, by further modifying the isAlphaOrSpace predicate
> to include the new special characters.

For instance, from a ghci session:
> let s = "    \"   John Doe-Smith   \"  "
> let (|||) = liftM2 (||)
> let predicate = isAlpha ||| isSpace ||| (== '-') ||| (== '\'')
> let fs = unwords . words . takeWhile predicate . dropWhile (not . isAlpha) $ s
> fs
"John Doe-Smith"

-- 
Denis Kasak



More information about the Beginners mailing list