[Haskell-beginners] pattern matching on strings ?

Mike Meyer mwm at mired.org
Fri Feb 20 08:00:48 UTC 2015


On Fri, Feb 20, 2015 at 1:32 AM, Roelof Wobben <r.wobben at home.nl> wrote:

> Hello,
>
> Im try to the solve the exercise which does the following :
>
> "E 2 562 help he
>
> and convert it into
>
> (Error 2) 562 "help help
>
> First I thought to split the string on the " " but according to google
> Haskell do not have a split function.
>
> Can I use pattern matching on some way ?
>

You can do pattern matching on lists, and strings are lists of chars, so
you can always do it that way.

I'm not sure what you mean by "Haskell do not have a split function". It
may not have one by that name, but it has a bunch of functions for
splitting up lists and strings. If you want a list of words, then words
will give it to you. If you want to split it into two halves, then either
span or break will do that for you. Neither discards the dividing
character, since they take a predicate to determine the split rather than a
value, which means the dividing character may not be obvious after the
split. It's common to find that where procedural languages take a value to
test against, functional ones take a predicate that does the test, as it's
trivial to create a predicate to test for equality with a specific value.

You might also want to look through Data.Char for interesting predicates
(like isSpace) to use with those functions.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150220/d2b2a1d7/attachment.html>


More information about the Beginners mailing list