“Functional programming” has a clear meaning, but

2019-03-09 17:05发布

I understand very clearly the difference between functional and imperative programming techniques. But there's a widespread tendency to talk of "functional languages", and this really confuses me.

Of course some languages like Haskell are more hospitable to functional programming than other languages like C. But even the former does I/O (it just keeps it in a ghetto). And you can write functional programs in C (it's just absurdly harder). So maybe it's just a matter of degree.

Still, even as a matter of degree, what does it mean when someone calls Scheme a "functional language"? Most Scheme code I see is imperative. Is it just that Scheme makes it easy to write in a functional style if you want to? So too do Lua and Python. Are they "functional languages" too?

I'm (really) not trying to be a language cop. If this is just a loose way of talking, that's fine. I'm just trying to figure out whether it does have some definite meaning (even if it's a matter-of-degree meaning) that I'm not seeing.

10条回答
淡お忘
2楼-- · 2019-03-09 17:58

A language (and platform) that promotes Functional Programming as a means of fully leveraging the capabilities of the said platform.

查看更多
【Aperson】
3楼-- · 2019-03-09 17:58

You can do functional style programming in any language. I try as much as possible.

Python, Linq all promote functional style programming.

A pure functional language like Haskell requires you to do all your computations using mathematical functions, functions that do not modify anything, they just return values.

In addition, functional languages typically allow you to write higher order functions, functions that take functions as arguments and/or return types.

查看更多
霸刀☆藐视天下
4楼-- · 2019-03-09 18:03

I like @Randolpho's answer. With regards to features, I might cite the list here:

Defining point of functional programming

namely

  • Purity (a.k.a. immutability, eschewing side-effects, referential transparency)
  • Higher-order functions (e.g. pass a function as a parameter, return it as a result, define anonymous function on the fly as a lambda expression)
  • Laziness (a.k.a. non-strict evaluation, most useful/usable when coupled with purity)
  • Algebraic data types and pattern matching
  • Closures
  • Currying / partial application
  • Parametric polymorphism (a.k.a. generics)
  • Recursion (more prominent as a result of purity)
  • Programming with expressions rather than statements (again, from purity)

The more a particular programming language has syntax and constructs tailored to making the various FP features listed above easy/painless to express & implement, the more likely someone will label it a "functional language".

查看更多
爷的心禁止访问
5楼-- · 2019-03-09 18:04

Haskell for one have different types for functions with side-effects and those without.

That's a pretty good discriminating property for being a 100% functional language, at least IMHO.

查看更多
登录 后发表回答