开发者

C++0x (C++11) as functional language?

i'm wondering if C++0x (C++11) (with lambdas and perfect forwarding) is (a superset of) a functional language. is there any feature of functional l开发者_如何学编程anguages, that C++ doesn't have?


The functional programming paradigm models computation as a relation between sets, and is thus inherently declarative. However, in practice, we often think of functions as imperative, ie you put in an input value and get out an output value, same as with a procedure. From this point of view, the characteristic property of a function is that it has no side-effects. Because of ambiguity of the terms, we call such a function pure, and a language which only has pure functions would be a purely functional language.

However, not all functional languages are pure: A functional language is a language with syntax and semantics which allows the programmer to use the functional paradigm efficiently. Some of the concepts which make using the paradigm feasible include - among others - lambda expressions with lexical closure, higher-order functions, variant types and pattern matching, lazy evaluation, type-inference (in case of statically-typed languages).

This is by no means an authorative list, and a language can very well be functional without providing all or even most of them, but if a language does - ie makes them usable without having to jump through major hoops - their presence is a strong indicator that the language should be considered functional.

I don't know enough about Boost to decide whether or not C++03 + Boost is a viable functional language, but C++0x definitely makes C++ more functional, perhaps even pushing it over the subjective boundary of the realm of functional languages.

As an aside, the same considerations apply to other programming paradigms: C++ is also not a purely object-oriented language (indeed, it's very hard - perhaps even theoretically impossible - to design a language which is both purely functional and purely object-oriented), and most features one commonly associates with OO-languages (classes, inheritance, encapsulation) are actually in no way authorative as well...


Check out the list of Functional Programming Languages definitions and discussion on the C2 wiki.

Some of the most common (and least disputed features) are:

  • First class functions - function class represents first class functions.
  • Higher Order Functions - Can be emulated with function objects.
  • Lexical Closures - Can be emulated with classes.
  • Single Assignment - More of a convention. You can do this by declaring all variables const.
  • Lazy Evaluation - Can be achieved with TMP
  • Garbage Collection - still missing. Pretty much necessary in a functional language, since lifetime and scope are not the same, as @Pascal pointed out in the comments above.
  • Type Inference - auto
  • Tail Call Optimization - Not strictly necessary for a functional language, but compiler dependent in C++.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜