0

这就是上下文。假设我有一个函数需要 2 个实验的元组并根据规则列表对其进行测试。只要实验的元组被某个规则正确验证,该函数就应该停止。

type exp = A | B | Mix of exp * exp | Var of string

type sufficency = exp * exp  

type rule = Rule of sufficency * (sufficency list) 


let rec findout rules (exp1, exp2) = // return a boolean value
            match rules with
            | [] -> true
            | thisRule::remaining ->
                match thisRule with
                | (suff, condition) ->
                    match suff with
                    | (fstExp, sndExp) ->
                        let map1 = unify Map.empty exp1 fstExp // I don't mention this function in here, but it is defined in my code
                        let map2 = unify Map.empty exp2 sndExp
                        true
                findout remaining (exp1, exp2)

问题是,我不知道如何使用这样的函数式编程来完成。使用命令式编程,循环遍历规则列表会更容易,而不是使用递归遍历列表。

那么函数在递归的每个阶段应该返回什么?

我收到了上面代码的警告

警告 FS0020:此表达式的类型应为“单位”,但类型为“布尔”。使用 'ignore' 丢弃表达式的结果,或使用 'let' 将结果绑定到名称。

4

1 回答 1

2

所以问题出在这部分代码

            match thisRule with
            | (suff, condition) ->
                match suff with
                | (fstExp, sndExp) ->
                    let map1 = unify Map.empty exp1 fstExp // I don't mention this function in here, but it is defined in my code
                    let map2 = unify Map.empty exp2 sndExp
                    true
            findout remaining (exp1, exp2)

第一个match返回 true,因此您会收到警告。你可能想要

            match thisRule with
            | (suff, condition) ->
                match suff with
                | (fstExp, sndExp) ->
                    let map1 = unify Map.empty exp1 fstExp // I don't mention this function in here, but it is defined in my code
                    let map2 = unify Map.empty exp2 sndExp
                    true && findout remaining (exp1, exp2)

您在哪里进行true计算。List.*但是,如果您使用各种功能,这可能会更简单。

于 2013-10-02T08:39:20.123 回答