【发布时间】:2018-01-02 21:44:40
【问题描述】:
我一直在尝试创建一个过滤器,该过滤器使用递归函数和匹配模式检查文本中是否存在短语列表中的短语,但不知何故这似乎不起作用......也许有人可以给我提示原因?
let rec matchTails (tail1 : string list) (tail2 : string list) =
match tail1, tail2 with
| h1::t1 , h2::t2 ->
if (h1=h2) then
matchTails t1 t2
else
false
| _, [] -> false
| [],_-> true
let rec check2 (textH: string) (textT: string list) (phrases: string list list) =
match phrases with
|[] -> ()
| h :: t ->
printfn "%s -- %s" (h.Head) (textH)
match h with
|x when x.Length = 1 && x.Head = textH ->
()
|x when x.Head = textH && (matchTails (textT) (x)) ->
printfn "%s" (x.Head)
| _ -> ()
check2 (textH) (textT) (t)
let rec check (phrases : string list list) (text:string list) =
match text with
| [] -> ()
| h :: t ->
check2 (h) (t) (phrases)
check phrases t
let p = [["rolex"]; ["free"; "spins"; "everyday"]; ["free"; "cash"]]
let t = ["hello";"and";"welcome";"to";"our";"annual";"free";"cash";"and";"rolex";"giveaway"]
函数调用:检查 p t
我已经编辑了我的问题并修复了一些错误,但是,通过这些列表,程序将 unit() 作为输出...
【问题讨论】:
-
你的匹配表达式中有
h2 = h2。这将始终评估为真。你是说h1 = h2吗?