简体   繁体   English

函数式编程是命令式编程的一个子集吗?

[英]Is functional programming a subset of imperative programming?

One of the main characteristics of functional programming is the use of side-effectless functions.函数式编程的主要特征之一是使用无副作用的函数。 However, this can be done in an imperative language also.但是,这也可以在命令式语言中完成。 The same is true for recursion and lambda functions (for example C++0x).递归和 lambda 函数(例如 C++0x)也是如此。 Therefore I wonder whether imperative programming languages are a superset of functional ones.因此,我想知道命令式编程语言是否是函数式编程语言的超集。

I can't really say whether they are subset of one another. 我不能说它们是否是彼此的子集。 What I can tell, though, that (except for really esoteric languages) they are all Turing-complete , which means that in the end they're all equally powerful, but not neccesarily equally expressive. 然而,我能说的是(除了真正深奥的语言)他们都是图灵完全的 ,这意味着最终他们都是同样强大的,但不一定具有同样的表现力。

Generally speaking, no; 一般来说,没有; functional programming is a subset of declarative programming (which includes logic programming languages, like Prolog). 函数式编程是声明式编程的一个子集(包括逻辑编程语言,如Prolog)。 Many imperative languages borrow elements from functional programming languages, but simply having lambdas or referentially-transparent functions does not make an imperative language functional; 许多命令式语言从函数式编程语言中借用元素,但简单地使用lambdas或referentially-transparent函数并不能使命令式语言起作用; functional programming is about more than just these elements. 函数式编程不仅仅是这些元素。

It is possible to implement a certain programming paradigm in a language which doesn't support the programming paradigm natively. 可以用不支持编程范例的语言实现某种编程范例。 For example its possible to write Object Oriented Code in C while it is not designed for this purpose. 例如,它可以在C中编写面向对象的代码,而不是为此目的而设计的。

Functional programming is a well developed programming paradigm of its own and is best learnt through languages like Haskell, LISP etc. And after you have learnt them well, even though you don't use these languages regularly, you may start using those principles in the day to day language you use on regular basis. 函数式编程是一个完善的编程范例,最好通过Haskell,LISP等语言学习。在学好它们之后,即使你不经常使用这些语言,你也可以开始使用这些原理。您定期使用的日常语言。

Some people may like to Google for Object oriented programming in C 有些人可能喜欢Google 在C中进行面向对象编程

A paradigm is a way of doing things, and there are two main programming paradigms: imperative and declarative. 范式是一种做事方式,有两种主要的编程范式:命令式和声明式。 The fact that some languages allow to mix both paradigms doesn't mean that one is included in the other, but that the languages are multi-paradigm . 某些语言允许混合两种范式的事实并不意味着一种语言包含在另一种语言中,而是语言多范式的

To clarify it a little bit more, let me continue with your analogy: if Lisp and OCaml (for example) are considered functional languages, and both of them allow imperative style... then should imperative be considered a subset of functional? 为了更清楚地说明一点,让我继续你的类比:如果Lisp和OCaml(例如)被认为是函数式语言,并且它们都允许命令式......那么命令式应该被视为功能的一个子集吗?

Most imperative languages don't have functions as first-order types, whereas most functionald o. 大多数命令式语言没有作为一阶类型的函数,而大多数函数式函数都没有。 (As does C++, via boost::function.) (与C ++一样,通过boost :: function。)

By first-order type, this meas a value/variable can be of any type, an int, a bool, a function from int->bool. 通过一阶类型,这个meas值/变量可以是任何类型,int,bool,来自int-> bool的函数。 It usually also includes closures or bound values as well, where you have the same function, but some arguments are already filled in. 它通常还包括闭包或绑定值,您可以使用相同的函数,但是已经填充了一些参数。

Those two are what functional programming is mostly about, IMHO. 这两个是函数式编程主要是关于恕我直言。

I think it might be helpful to draw a distinction between paradigm and language . 我认为区分范式语言可能会有所帮助。

To me, paradigms represent "ways of thinking" (concepts and abstractions such as functions, objects, recursion), whereas languages offer "ways of doing" (syntax, variables, evaluations). 对我来说, 范式代表“思维方式” (概念和抽象,如函数,对象,递归),而语言则提供“做法” (语法,变量,评估)。

All true programming languages are equivalent in the sense that they are Turing-complete and able, in theory, to compute any Turing-computable function as well as simulate or be simulated by a universal Turing machine. 所有真正的编程语言都是等同的 ,因为它们是图灵完备的,并且理论上能够计算任何图灵可计算函数以及通用图灵机模拟或模拟。

The interesting thing is how difficult it is to accomplish certain tasks in certain languages or paradigms, how appropriate the tool is to the task. 有趣的是,在某些语言或范例中完成某些任务是多么困难,这个工具对任务的适当程度。 Even Conway's Game of Life is Turing-complete, but that does not make me want to program with it. 即使康威的生命游戏也是图灵完成的,但这并不能让我想用它编程。

Many languages support a number of paradigms. 许多语言支持许多范例。 C++ was designed as an object-oriented extension for C, but it is possible to write purely procedural code in it. C ++被设计为C的面向对象的扩展,但是可以在其中编写纯粹的过程代码。

Some languages borrow/acquire features from other languages or paradigms over time (just look at the evolution of Java). 有些语言随着时间的推移借用/获取其他语言或范例的功能(只看Java的演变)。

A few languages, like Common Lisp, are impressively multi-paradigm languages. 一些语言,如Common Lisp,是令人印象深刻的多范式语言。 It is possible to write code that is functional, object oriented or procedural in Lisp. 可以在Lisp中编写功能,面向对象或过程的代码。 Arguably, aspect-orientation is already part of the common lisp object system, and therefore "nothing special". 可以说,面向方面已经是常见的lisp对象系统的一部分,因此“没什么特别的”。 In Lisp, it is easy to extend the language itself to do whatever you need it to do, thus it is sometimes called the "programmable programming language". 在Lisp中,很容易扩展语言本身来做你需要做的事情,因此它有时被称为“可编程编程语言”。 (I'll point out here that Lisp describes a family of languages of which Common Lisp is only one dialect). (我将在这里指出Lisp描述的是一系列语言,其中Common Lisp只有一种方言)。

I think it doesn't matter which of the terms, declarative, imperative, functional or procedural, is a subset of which. 我认为哪个术语,声明性,命令性,功能性或程序性是其中的一个子集并不重要。 What matters more is to understand the tools languages you're working with, and how those are different from other tools. 更重要的是了解您正在使用的工具语言,以及它们与其他工具的不同之处。 Even more important is to understand the different ways of thinking that the paradigms represent, since those are your thought-tools. 更重要的是理解范式所代表的不同思维方式,因为这些是你的思维工具。 As with most other things in life, the more you understand, the more effective you become. 与生活中的大多数其他事物一样,您理解的越多,您变得越有效。

One way to look at it (not saying it's the right way 'cos I'm not a lang designer or theorist by any means) is that if the language is essentially converted to something else then that 'something else' must be the superset of the source. 一种看待它的方式(不是说它是正确的方式'因为我不是一个lang设计师或任何方式的理论家)是如果语言基本上转换成其他东西那么'别的'必须是超集的来源。 So bytecode is necessarily a superset of Java. 所以字节码必然是Java的超集。 .NET IL is a superset of C# and of F#. .NET IL是C#和F#的超集。 The functional constructs in C# (ie LINQ) are thus a subset of the imperative constructs of IL. 因此,C#中的功能构建体(即LINQ)是IL的命令性构建体的子集。

Since machine language is imperative, you could take the position that, therefore, all languages are imperative, because they are just abstractions useful for humans that are then boiled away by the compiler to procedural, imperative machine code. 由于机器语言是必不可少的,因此您可以采取这样的立场:因此,所有语言都是必不可少的,因为它们只是对人类有用的抽象,然后被编译器煮沸为程序性的,命令式的机器代码。

Pattern mapping like 模式映射就好

f:: [int] -> int
f [] = 0
f (x:xs) = 1 + f(xs)

is something that is for instance one thing that is not available in imperative languages. 例如,在命令式语言中没有的东西是一件事。 Also constructs like curried functions: 还有类似curried函数的构造:

add2 :: int -> int
add2 = (2 +)

is not available in most imperative languages 在大多数命令式语言中不可用

Yes, functional programming is a subset of imperative programming, but...是的,函数式编程命令式编程的一个子集但是......

Yes , because there is nothing in functional programming that you can't do in imperative programming (syntax differences notwithstanding).是的,因为没有函数式编程,你不能在命令式编程做(尽管语法差异)。 You can "do" functional programming in an imperative language.您可以使用命令式语言“进行”函数式编程。

But... The things you can't do are the key features of functional programming.但是......不能做的事情是函数式编程的关键特性。 By limiting what you can do,通过限制你可以做的事情,

  1. you make certain mistakes impossible,你犯了一些不可能的错误,
  2. you enable features (such as program analysis, simpler concurrency, simpler testing, etc).您启用功能(例如程序分析、更简单的并发、更简单的测试等)。

Other benefits of functional programming are more subjective.函数式编程的其他好处更加主观。 You often hear these arguments.你经常听到这些论点。

  1. State allows side effects.状态允许副作用。 Side effects are bad.副作用是不好的。 Functional programming has no state.函数式编程没有状态。 You can't have side effects without state.没有状态就不可能有副作用。

This is a dubious benefit.这是一个可疑的好处。 First, only unintended side effects are bad.首先,只有意想不到的副作用是不好的。 Proper programming practices, such as limiting modification of state to privileged code, alleviate side effect issues.适当的编程实践,例如限制对特权代码的状态修改,可以缓解副作用问题。 Second, functional programming only has no internal state .其次,函数式编程没有内部状态 If the program has IO (accessing files, network, hardware) you have external state , thus the potential of side effects.如果程序有 IO(访问文件、网络、硬件),则您有外部状态,因此可能会产生副作用。

  1. Functional programs are easier to debug.函数式程序更容易调试。

In some ways yes, like knowing the explicit path to an exception.在某些方面是的,比如知道异常的显式路径。 But having state to examine is a debugging benefit that functional programming does not have.但是有状态检查是函数式编程没有的调试好处。

  1. Functional programs are easier to understand.函数式程序更容易理解。

This is only true if you are fluent in functional programming and not fluent in imperative programming.只有当您精通函数式编程而不精通命令式编程时,这才是正确的。 I for one, being more fluent in imperative programming, find this argument to be false .一方面,我对命令式编程更加流利,认为这个论点是错误的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM