奥卡姆连续传球风格
我是 ocaml 的新手,尝试编写一个连续传递样式函数,但很困惑我需要将什么值传递给 k 上的附加参数,
例如,我可以编写一个递归函数,如果列表中的所有元素都是偶数,则返回 true,否则为假。
所以就像在 CPS 上一样
let rec even list = ....
,我知道我需要添加一个参数来传递函数 很喜欢
let rec evenk list k = ....
,但我不知道如何处理这个 k 以及它到底是如何工作的,
例如对于这个偶数函数,环境看起来像
val evenk : int list -> (bool -> ’a) -> ’a = <fun>
evenk [4; 2; 12; 5; 6] (fun x -> x) (* output should give false *)
I'm new to ocaml and tryin to write a continuation passing style function but quite confused what value i need to pass into additional argument on k
for example, I can write a recursive function that returns true if all elements of the list is even, otherwise false.
so its like
let rec even list = ....
on CPS, i know i need to add one argument to pass function
so like
let rec evenk list k = ....
but I have no clue how to deal with this k and how does this exactly work
for example for this even function, environment looks like
val evenk : int list -> (bool -> ’a) -> ’a = <fun>
evenk [4; 2; 12; 5; 6] (fun x -> x) (* output should give false *)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
延续
k
是一个函数,它从evenk
获取结果并执行“其余计算”并产生“答案”。答案的类型以及“其余计算”的含义取决于您使用 CPS 的目的。。 CPS 本身通常不是目的,而是出于某种目的而完成的。例如,在 CPS 形式中,很容易实现控制运算符或优化尾部调用。如果不知道你想要完成什么,就很难回答你的问题。无论如何,如果您只是尝试从直接样式转换为延续传递样式,并且您所关心的只是答案的值,则传递恒等函数作为延续是正确的。
下一步最好是使用 CPS 实现
evenk
。我会做一个更简单的例子。如果我有直接式函数
,并且假设 CPS 原语
mulk
和addk
,我可以写你会看到乘法首先完成,然后是“ continue” 与
k'
进行添加,最后continues
与k
返回给调用者。关键思想是,在muladdk
主体中,我分配了一个新的延续k'
,它代表乘加函数中的中间点。为了让你的evenk
工作,你必须分配至少一个这样的延续。我希望这有帮助。
The continuation
k
is a function that takes the result fromevenk
and performs "the rest of the computation" and produces the "answer". What type the answer has and what you mean by "the rest of the computation" depends on what you are using CPS for. CPS is generally not an end in itself but is done with some purpose in mind. For example, in CPS form it is very easy to implement control operators or to optimize tail calls. Without knowing what you are trying to accomplish, it's hard to answer your question.For what it is worth, if you are simply trying to convert from direct style to continuation-passing style, and all you care about is the value of the answer, passing the identity function as the continuation is about right.
A good next step would be to implement
evenk
using CPS. I'll do a simpler example.If I have the direct-style function
and if I assume CPS primitives
mulk
andaddk
, I can writeAnd you'll see that the mulptiplication is done first, then it "continues" with
k'
, which does the add, and finally thatcontinues
withk
, which returns to the caller. The key idea is that within the body ofmuladdk
I allocated a fresh continuationk'
which stands for an intermediate point in the multiply-add function. To make yourevenk
work you will have to allocate at least one such continuation.I hope this helps.
每当我使用 CPS 时,传递给延续的东西就是您通常返回给调用者的东西。在这个简单的例子中,一个很好的“直觉润滑剂”是将延续命名为“return”。
用法示例:“even [2; 4; 6; 8] id;;”。
Whenever I've played with CPS, the thing passed to the continuation is just the thing you would normally return to the caller. In this simple case, a nice "intuition lubricant" is to name the continuation "return".
Example usage: "even [2; 4; 6; 8] id;;".
由于您正确调用了 Evenk(使用恒等函数 - 有效地将连续传递样式转换回正常样式),我认为困难在于定义 Evenk >。
正如诺曼所说,k 是代表其余计算并产生最终值的延续函数。因此,您需要做的是计算
even
的v
结果并将该结果传递给k
,返回k v
code> 而不仅仅是v
。Since you have the invocation of
evenk
correct (with the identity function - effectively converting the continuation-passing-style back to normal style), I assume that the difficulty is in definingevenk
.k
is the continuation function representing the rest of the computation and producing a final value, as Norman said. So, what you need to do is compute the result ofv
ofeven
and pass that result tok
, returningk v
rather than justv
.您希望将函数的结果作为输入,就好像它不是用连续传递样式编写的一样。
这是您的函数,用于测试列表是否仅包含偶数:
现在让我们用延续
cont
编写它:您计算函数的结果,并将
结果
传递给延续...You want to give as input the result of your function as if it were not written with continuation passing style.
Here is your function which tests whether a list has only even integers:
Now let's write it with a continuation
cont
:You compute the result your function, and pass
result
to the continuation ...