getMultiWordPortion :: String -> String
getMultiWordPortion (x:':':xs) = xs
getMultiWordPortion _ = ""
The expected result from pattern matching in this code is that everything before the first colon get's assigned to x, and everything afterwards gets assigned to xs, which is the result. If there is no colon, nothing is returned.
What actually happens with a sample string I'm using ("PING :gibson.freenode.net" - it's part of an IRC client) is that I get the blank return value.
What am I doing wrong here?
The pattern x:':':xs
means "The first character is x
, the second character is ':'
the remaining characters are in the list xs
". So this means that the type of x
is Char
, not [Char]
and that the pattern only matches if there's exactly one character before the colon.
There is no way to use pattern matching on lists to say "match one sublist, followed by an element, followed by the remaining list".
To get the substring after the first colon you can use dropWhile (/= ':') theString
. This will include the colon, so use tail
or pattern matching to remove it.
The :
operator conses one element to the head of a list. Your pattern will only match a string where the colon is the second item in the list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With