函数式编程中cps(continuation-passing style )是什么意思?
有没有不基于lisp、c++的例子,最好是python的,也有lamdba expression嘛。
回复内容:
CPS把函数调用完之后接下来要执行的代码通过闭包包裹并作为函数参数调用要执行的函数。
Continuation Passing Style Revisited Part Five: CPS and AsynchronyCPS变换本质上就是调用一个函数的时候,给它传入另一个函数(所以,语言必须得支持高阶函数和闭包才行),被调函数不把结果返回调用者,而是将结果返回给通过参数传进来的那个函数。
我不清楚这个概念在别的语言里有没有实现,或者叫不同的名字。
这里有个 scheme 的例子:call/cc 探秘从另一个角度回答下吧。
带 callcc 的 Lambda 演算(叫演算)可以经 Curry-Howard 同构到经典逻辑,而普通的
演算只能同构到直觉逻辑。但是形式逻辑中有一个 Gilvenko 定理,它声称:
对任何命题和前提
,在经典逻辑中
若且唯若在直觉逻辑中
![]()
在证明这个定理之后哥德尔(对,就是证明存在不确定命题的那个)和根岑(自然演绎和相继式演算的发明人)发明了双否定变换,也叫哥德尔-根岑变换,其规则是:
注意到哥德尔-根岑变换任意命题都和原命题经典等价,但并非直觉等价(直觉逻辑本身否认),但是按照 Gilvenko 定理,双否定命题
若在直觉逻辑体系中可证明为真,则在经典逻辑体系里
必为真,反之亦然。
那么按照 Curry-Howard 同构,演算下的类型指派
可以经过哥德尔-根岑变换得到一个
演算类型指派:
,将表达式(同构于证明过程)
变为
的过程就是 CPS 变换。直接照搬哥德尔-根岑变换里的类型的话,我们有如下结果:
原子
调用
抽象
call/cc 算子
可以证明,,即:CPS 变换不改变语义。
当然这个版本的 CPS 是非常冗长的,市面上见到的那些都是在变换是之后直接做了规约,删掉大堆 Redex 的。
上面这些再一次说明了,逻辑学和编程有多么紧密的联系。
来个简明补充。
这是个简单函数计算输出:
static int Times3(int x) { return x * 3; } Console.WriteLine(Times3(5));
(答案抄袭自http://www.cs.indiana.edu/cgi-pub/lkuper/c311/_media/cps-notes.scm)
定义下面四个函数(为了保持和原答案一致,其实两个就够了)
def f(var0): passdef g(var0, var1): return passdef h(var0): return passdef j(var0): return pass
其实就是把
Fuckee FindFuckee(){return kula;}void Fuck(Fuckee fuckee, int count){ for(int i=0;i<count;i++) fuckee.Fuck();}void Main(){Fuck(FindFuckee(), 100);}
可以补一补逻辑学....
@Belleve的回答太抽象了,没逻辑背景的人看不懂,
我在Quora上看到一个回答写得挺好的,里面从逻辑学的角度解释的一节或许可以作为 @Belleve答案的补充:
What is continuation-passing style in functional programming?
以上就是函数式编程中cps(continuation-passing style )是什么意思?的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In the contemporary programming world, Functional Programming (FP for short) has gradually become a popular programming paradigm. It emphasizes using functions as basic building blocks to build programs, and regards the calculation process as the continuous transfer and conversion between functions. In recent years, Go language (also known as Golang) has gradually been widely used in various fields due to its simplicity, efficiency, concurrency safety and other characteristics. Although the Go language itself is not a purely functional programming language, it provides sufficient functionality.

There are many excellent programming techniques in the C++ language, among which functional programming is a very practical technology. Functional programming emphasizes the reusability and flexibility of functions, which can make the code clearer and maintainable. In this article, we will introduce functional programming techniques in C++. 1. Function object A function object is a callable object, which can be regarded as a function. Function objects in C++ can be class objects or function pointers. Function objects can be used in STL algorithms and can also be used as parameters of other functions. Here is a simple

C++ lambda expressions bring advantages to functional programming, including: Simplicity: Anonymous inline functions improve code readability. Code reuse: Lambda expressions can be passed or stored to facilitate code reuse. Encapsulation: Provides a way to encapsulate a piece of code without creating a separate function. Practical case: filtering odd numbers in the list. Calculate the sum of elements in a list. Lambda expressions achieve the simplicity, reusability, and encapsulation of functional programming.

Lazy evaluation can be implemented in Go by using lazy data structures: creating a wrapper type that encapsulates the actual value and only evaluates it when needed. Optimize the calculation of Fibonacci sequences in functional programs, deferring the calculation of intermediate values until actually needed. This can eliminate unnecessary overhead and improve the performance of functional programs.

There are five common mistakes and pitfalls to be aware of when using functional programming in Go: Avoid accidental modification of references and ensure that newly created variables are returned. To resolve concurrency issues, use synchronization mechanisms or avoid capturing external mutable state. Use partial functionalization sparingly to improve code readability and maintainability. Always handle errors in functions to ensure the robustness of your application. Consider the performance impact and optimize your code using inline functions, flattened data structures, and batching of operations.

pythonLambda expressions are a powerful and flexible tool for creating concise, readable, and easy-to-use code. They are great for quickly creating anonymous functions that can be passed as arguments to other functions or stored in variables. The basic syntax of a Lambda expression is as follows: lambdaarguments:expression For example, the following Lambda expression adds two numbers: lambdax,y:x+y This Lambda expression can be passed to another function as an argument as follows: defsum( x,y):returnx+yresult=sum(lambdax,y:x+y,1,2)In this example

With the widespread application of C++ in the computer field and the continuous exploration of programming paradigms, functional programming has also become a topic of great concern. In C++, functional programming has many special concepts and syntax, so related questions are often involved in interviews. This article summarizes and answers common interview questions about functional programming in C++. 1. Advantages and Disadvantages of Functional Programming The interviewer may ask you about your understanding of the advantages and disadvantages of functional programming. Functional programming has the following advantages: High readability. Functional programming only focuses on the output of functions

Lambda expression in python is another syntax form of anonymous function. It is a small anonymous function that can be defined anywhere in the program. A lambda expression consists of a parameter list and an expression, which can be any valid Python expression. The syntax of a Lambda expression is as follows: lambdaargument_list:expression. For example, the following Lambda expression returns the sum of two numbers: lambdax,y:x+y. This Lambda expression can be passed to other functions, such as the map() function: numbers=[ 1,2,3,4,5]result=map(lambda
