在列表中查找重复的字符

时间:2015-11-17 23:56:10

标签: f#

我遇到的情况是找到一系列字符串,其格式类似#include <stdio.h> #include <unistd.h> struct NewBuiltIn{ char *CommandName[64]; char *FunctionName[64]; char *AnalyzerName[64]; }; struct NewBuiltIn pluggin_method = {{"cd", "cd", ""}}; void cd(char *argv[]) { if(chdir(argv[1]) < 0){ printf("There was an error in changing your directory. Please check the path name and retry.\n"); } } XXXX或“CCCC”。我尝试了以下代码,但它不起作用

IIII

错误是: let rec checkSequence roman= let r=List.ofSeq roman match r with | [] -> true | a::b::c::d::tail when (a="I" || a="X" || a="C") && a=b && a=c && a=d -> false | head::tail -> checkSequence tail checkSequence "CCC"

1 - 如何解决此错误?

2 - 有没有更简单的方法来找到这种模式?

2 个答案:

答案 0 :(得分:1)

如果您需要在列表中使用递归,您可以执行以下操作:

let checkSequenceStr str =
    let rec checkSequence roman =
        match roman with
        | [] -> true   
        | 'I'::'I'::'I'::'I'::tail -> false
        | 'X'::'X'::'X'::'X'::tail -> false
        | 'C'::'C'::'C'::'C'::tail -> false
        | head::tail  -> checkSequence tail
    checkSequence (str |> List.ofSeq)

或者您可以使用.NET字符串方法直接检查模式(这更容易):

let checkPattern (str : string) =
    ["IIII";"CCCC";"XXXX"] |> List.exists str.Contains |> not

答案 1 :(得分:0)

您正在使用List.ofSeq,这将键入强制罗马参数为类型列表。

https://msdn.microsoft.com/en-us/library/ee340325.aspx

因此,您的错误This expression was expected to have type string list but here has type string是由于错误地调用了函数,然后是逻辑错误。因此改变:

checkSequence "CCC"

分为:

checkSequence ["C"; "C";"C"]