Ocaml,理解一个函数
我在理解这个函数如何工作时遇到一些问题, 特别是我不明白它的最后一行的控制流程。 有人可以用伪代码向我解释它的步骤吗?
let traduit_pair a b =
let a = traduit mark a in let b = traduit mark b in (a, b) in
let (teq1, teq2, lneq) =
let rec f l1 l2 l3 =
(function
| [] -> ((Uplet l1), (Uplet l2), l3)
| EqualIF (a, b) :: fin ->
let (a, b) = traduit_pair a b
in f (a :: l1) (b :: l2) l3 fin
| NotEqualIF (a, b) :: fin ->
let (a, b) = traduit_pair a b
in f l1 l2 ((a, b) :: l3) fin)
in f [] [] [] (List.rev condlst)
I have some problems in understanding how this function works,
in particular I' don't understand the control flow of itregarding the last line.
Can someone explain me the steps it does, maybe with a pseudocode?
let traduit_pair a b =
let a = traduit mark a in let b = traduit mark b in (a, b) in
let (teq1, teq2, lneq) =
let rec f l1 l2 l3 =
(function
| [] -> ((Uplet l1), (Uplet l2), l3)
| EqualIF (a, b) :: fin ->
let (a, b) = traduit_pair a b
in f (a :: l1) (b :: l2) l3 fin
| NotEqualIF (a, b) :: fin ->
let (a, b) = traduit_pair a b
in f l1 l2 ((a, b) :: l3) fin)
in f [] [] [] (List.rev condlst)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代码的一般流程如下:
首先定义函数
traduit_pair
。它接受两个参数a
和b
并返回一对,其中包含对每个参数应用传统标记
的结果。然后定义变量
teq1
、teq2
和lneq
,分别包含f [] [] [ 返回的三元组中的一个元素] (List.rev condlst)
,其中f
定义如下:首先我们看看
f
定义时为什么可以用四个参数来调用仅命名三个参数:正如您可能知道的,ML 允许柯里化函数定义和定义let fxy = blabla
实际上只是let f = fun x =>; 的快捷方式。有趣的y =>布拉布拉。
因此,当我们谈论一个带有两个参数的函数时,我们实际上是在谈论一个带有一个参数并返回另一个带有另一个参数的函数。同样,一个接受三个参数然后返回另一个接受另一个参数的函数的函数与接受四个参数的函数是一样的。
f
定义中使用的function
关键字是创建采用参数和模式匹配的函数的语法快捷方式。也就是说function | p1-> e1 | p2-> e2 是 fun x => 的快捷方式案例 x 的 | p1-> e1 | p2-> e2
。所以让rec f l1 l2 l3 = function | p1-> e1 | p2-> e2 与 let rec f l1 l2 l3 = fun l4 => 相同案例 l4 | p1-> e1 | p2-> e2,与let rec f l1 l2 l3 l4 = case l4 of | 相同p1-> e1 | p2-> e2
,很容易将其识别为带有四个参数的函数。The general flow of your code is like this:
First the function
traduit_pair
is defined. It takes two argumentsa
andb
and returns a pair containing the result of applyingtraduit mark
to each of them.Then the variables
teq1
,teq2
andlneq
are defined to each contain one element of the triple returned byf [] [] [] (List.rev condlst)
, wheref
is defined as follows:First of all let's look at why
f
can be called with four arguments when its definition only names three arguments: As you probably know ML allows curried function definitions and the definitionlet f x y = blabla
is really just a shortcut forlet f = fun x => fun y => blabla
.So when we talk about a function taking two arguments, we're really talking about a function taking one argument and returning another function which takes another argument. Likewise a function which takes three arguments and then returns another function taking another argument, is the same thing as a function taking four arguments.
The
function
keyword which is used in the definition off
is a syntactic shortcut to create a function taking an argument and pattern matching on it. That is to sayfunction | p1 -> e1 | p2 -> e2
is a shortcut forfun x => case x of | p1 -> e1 | p2 -> e2
. Solet rec f l1 l2 l3 = function | p1 -> e1 | p2 -> e2
is the same aslet rec f l1 l2 l3 = fun l4 => case l4 of | p1 -> e1 | p2 -> e2
, which is the same aslet rec f l1 l2 l3 l4 = case l4 of | p1 -> e1 | p2 -> e2
, which is easily identifiable as a function taking four arguments.