[Haskell-beginners] How to remove leading and trailing non-alpha characters, and multiple consecutive spaces?
Tim Perry
tim.v2.0 at gmail.com
Fri Jun 7 01:14:34 CEST 2013
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.
Prelude Data.Char Control.Monad.Reader> unwords $ words $ takeWhile
isAlphaOrSpace $ dropWhile (not . isAlpha) $ " \" John Doe \" "
"John Doe"
Prelude Data.Char Control.Monad.Reader> unwords $ words $ takeWhile
isAlphaOrSpace $ dropWhile (not . isAlpha) $ " \" John Doe-Smith \"
"
"John Doe"
Tim Perry
(916) 505-3634
On Thu, Jun 6, 2013 at 4:06 PM, Denis Kasak <denis.kasak at gmail.com> wrote:
> 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
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130606/0eaffcf9/attachment.htm>
More information about the Beginners
mailing list