[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:44:31 CEST 2013


That would certainly work. I'd be tempted to just use:

unwords . words . dropWhile (not . isAlpha) $ "unclean-first unclean-last"

Tim


On Thu, Jun 6, 2013 at 4:31 PM, Denis Kasak <denis.kasak at gmail.com> wrote:

> 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
>
> _______________________________________________
> 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/18d9fc34/attachment.htm>


More information about the Beginners mailing list