I want to refine the raw text by using regular expression, given a list of (patten,replacement) tuple.
I tried to use the patten matching on the list element but failed, the error showed that "This expression was expected to have type string * string list but here has type 'a list".
How开发者_如何学JAVA can I fix this problem? Thanks a lot.
Codes are as follows:
let rec refine (raw:string) (rules:string*string list) =
match rules with
| (pattern,replacement) :: rest ->
refine <| Regex.Replace(raw,pattern,replacement) rest
| [] -> raw
The problem is that a string * string list
is a pair consisting of a string and a list of strings, whereas you want a (string * string) list
:
let rec refine (raw:string) (rules:(string*string) list) =
match rules with
| (pattern,replacement) :: rest ->
refine (Regex.Replace(raw,pattern,replacement)) rest
| [] -> raw
Alternatively, the only reason you need that particular annotation is because Regex.Replace
is overloaded. This is why your other solution works, but there are other (more minimal) places you can put an annotation that will work:
let rec refine (raw:string) rules =
match rules with
| (pattern,replacement:string) :: rest ->
refine (Regex.Replace(raw,pattern,replacement)) rest
| [] -> raw
Finally it works when I try this:
let rec refine (raw:string) rules =
match rules with
| rule :: rest ->
//get tuple values beyond the patten matching
let (pattern:string,replacement:string) = rule
refine (Regex.Replace(raw,pattern,replacement)) rest
| [] -> raw
精彩评论