[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:06:55 CEST 2013


On 7 June 2013 00:36, Costello, Roger L. <costello at mitre.org> wrote:
> Hi Folks,
>
> I have a string that contains a person's name.
[snip]
> Here is an example string:
>
> s = "     \"    John     Doe    \"    "
>
> After processing, I should have:
>
>         John Doe
>
> Below is my implementation. Is there is a shorter and more efficient implementation?
[snip code]

How about this?

import Data.Char
import Control.Monad.Reader

isAlphaOrSpace = liftM2 (||) isAlpha isSpace

-- alternatively,
-- isAlphaOrSpace x = isAlpha x || isSpace x
-- if you find this more readable

s = "     \"    John     Doe    \"    "

fs = unwords
   . words
   . takeWhile isAlphaOrSpace
   . dropWhile (not . isAlpha) $ s

--
Denis Kasak



More information about the Beginners mailing list