2

i'm trying to parse an input in haskell so that i can transform it to a custom type.

The input consists of a name variable, followed by a list of values and their abbreviations like this

colour:red=r,blue=b,green=g 

I defined my type as Var = (string,[value])

I'm fairly new to haskell so my first instinct was to (mistakenly) try and pattern match like this

parseVar :: String -> Var
parseVar (y:':':v) = (y, parseValueList v)

the problem is i cant do that because y has to be a string. My question is: is there a way to pattern match with an indefinite length string as long as i know the separator symbol? (in this case ':' )

I know this could be done in a different way but i just want to know if that is a tool i can use

Will Ness
  • 70,110
  • 9
  • 98
  • 181
javi8
  • 33
  • 3
  • Pattern matching works on constructors. From the sound of it you would like to match on `++` which is a regular function, which is not supported. The match would be ambiguous too since there is nothing in the definition of `++` that prohibits the first argument containing colons - or in other words which colon are we matching if there are multiple? The runtime would have to probably make some opinionated choices. – user2847643 Nov 21 '20 at 17:33
  • Prior art: [Why can't I pattern match on the concatenation function (++) in Haskell?](https://stackoverflow.com/questions/34217515/why-cant-i-pattern-match-on-the-concatenation-function-in-haskell), [Haskell pattern matching char in a string](https://stackoverflow.com/questions/13055770/haskell-pattern-matching-char-in-a-string). – amalloy Nov 21 '20 at 22:18
  • 1
    @user2847643 the OP is in [good company](https://rkrishnan.org/files/wadler-1985.pdf), calling parsing "pattern matching". and, "if a pattern matches a string in more than one way, a list of more than one pair may be returned". and, [this comment](https://stackoverflow.com/questions/34217515/why-cant-i-pattern-match-on-the-concatenation-function-in-haskell#comment56183415_34217515). and [this one](https://stackoverflow.com/questions/34217515/why-cant-i-pattern-match-on-the-concatenation-function-in-haskell#comment56220139_34218050). (yes, appeal to authority :) ) – Will Ness Nov 22 '20 at 06:20
  • @amalloy thanks for finding the duplicate. I had no time for searching, and answering in a comment with simple link to span and break was a no-no, as I was told. :) – Will Ness Nov 22 '20 at 06:26
  • This question was wrongly closed because the OP mentioned "Pattern match" and someone misinterpreted that as "pattern matching in case expressions". The OP clearly stated that this question is about "trying to parse an input in haskell." In Haskell we use monadic parsers for textual pattern matching, I recommend the OP start here https://markkarpov.com/tutorial/megaparsec.html – James Brock Nov 24 '20 at 03:39
  • This also looks good https://jakewheat.github.io/intro_to_parsing/ – James Brock Nov 24 '20 at 03:46
  • @JamesBrock or they can just repeatedly use `break`, as shown in the duplicate's accepted answer. which is why it looked to me indeed as a dup. --- the OP here wanted to parse a string and asked whether pattern matching is the tool for this. exactly like in the dup. – Will Ness Nov 26 '20 at 05:57

0 Answers0