What is the bare minimum needed to implement a functional language?
A Lambda. That's all. (See the lambda calculus.)
From there, you can construct functions to perform every task and represent many values. You can define new (named) functions with the Y-combinator form (PDF), and represent several values (like true and false) as functions. (Taking the idea to the extreme, you can represent all integral values as Church numerals.)
You don't even really need a special if
form. It can be constructed as a collection of functions. JavaScript example:
true = function(x,y){ return x; }
false = function(x,y){ return y; }
ifelse = function(x,a,b){ x(a,b)() }
not = function(x){ return x(false,true) }
and = function(x,y){ return x(y,false) }
or = function(x,y){ return x(true,y) }
bool = and(not(true),or(true,false))
ifelse(bool,
function(){ alert("true!") },
function(){ alert("false!") }
)
Unlambda is a real language that takes this idea to the logical extreme. It has only one type (lambda functions), and a handful of special convenience forms such as the identity function, K- and S-combinators, and a print function (which, except for print, can all be expressed as a collection of lambdas).
Is Javascript a functional language? I know it has objects & you can do OOP with it also, but is it also a functional language, can it be used in that way?
Sometimes, people will say functional programming, when what they mean is imperative programming or procedural programming. Strictly speaking, functional programming is:
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state. Functional programming has its roots in lambda calculus, a formal system developed in the 1930s to investigate function definition, function application, and recursion. Many functional programming languages can be viewed as elaborations on the lambda calculus.
Although Javascript is not widely known or used as a functional language, it does have some functional elements:
JavaScript has much in common with Scheme. It is a dynamic language. It has a flexible datatype (arrays) that can easily simulate s-expressions. And most importantly, functions are lambdas.
Scheme is a dialect of Lisp, and probably one of the languages most programmers think of when they discuss functional programming. When it comes to object orientation, Javascript is an object oriented language. But its object orientation is prototype based:
Prototype-based programming is a style of object-oriented programming in which classes are not present, and behavior reuse (known as inheritance in class-based languages) is performed via a process of cloning existing objects that serve as prototypes. This model can also be known as classless, prototype-oriented or instance-based programming. Delegation is the language feature that supports prototype-based programming.
So although Javascript is object oriented, it doesn't follow the more common class based model, as do languages as C++, C#, Java and PHP (and quite a few others). And of course it's also an imperative language, which leads to the confusion with functional programming I described above.
You know how OOP became/seems like the next evolution in programming, does that mean that 'Functional Programming' is the next evolution
Object orientation and functional programming are just two of the many different programming paradigms, they are different styles of programming with different concepts and abstractions. The key word is "different". There isn't a single paradigm that's better than others or more evolved than others, each and every one fits some scenarios better than the others. Some may be quite older in origin than others, but in evolutionary terms that makes them better, as they have survided longer. But that's not a very smart way of looking at it.
Javascript, as I described above and as quite a few other languages, is multi-paradigm. It allows you to write code in imperative, prototype based object oriented and functional style. It's up to you to choose which one best fits whatever you are building. There are also several single paradigm languages, the canonical example being Java, which only allows for class based object oriented programming1.
You should really resist any urge to treat languages & paradigms as fashion statements. There's an abudance of crap out there, mostly written by fanboys / fangirls or marketing people, with little (if any) knowledge and understanding of programming. Terms like "better", "more evolved" etc, simply don't apply.
I learn best through examples, maybe someone could show performing the same task in a OOP way & then in a Functional Programming way for myself to understand & compare what functional programming does/is.
That would be a terrible way to learn. Functional and object orientation are quite different styles, and any example other than terribly simple ones would not fit one or the other style.
1 But lately tries to expand its scope to generic programming, let's see how that goes.
In conclusion:
- Concentrate on learning Javascript, it's a beautiful and extremly useful language. Learn the language, not the hype.
- Quite a few different paradigms, all equally useful. Up to you to choose which one you prefer and which one fits best whatever you're building.
- If you want to learn functional programming, choose a more suited language, like Scheme or Clojure. But you'll first need to understand the mathematical concepts involved.
- Do some research before you ask. Most of your questions are answered by the relevant Wikipedia articles. Knowing how to research and how to ask is an extremely important skill for any programmer.
Best Answer
A function pointer (e.g. in C or C++) is only the address of some machine code (computing some function). It does know about any data except the constants embedded in the code, and the
static
data referenced by it.Conceptually (e.g. in lambda-calculus) a function needs both some code to be computed, and some data to be accessed. So a function usually has bound variables, that is closed variables. Hence, a function is a mixture of code and data, that is, a closure. Practically speaking, a closure contains not only code (some "function pointer" à la C) but also data.
Notice that C++11 introduced closures, anonymous functions (a.k.a. lambda-expressions), and
std::function
.Most (but not all) functional languages (e.g. Ocaml) prohibit comparing functions (that is, closures), because conceptually that would mean comparing the behavior of functions (and that is undecidable, read about Rice's theorem).
So closures are more expressive than function pointers. In this answer I claim that every function is conceptually a closure (in C, the closed values being the
static
data).Because C does not have genuine closures (but n2030 is a proposal to add them to some future C2x standard), most libraries wanting "closures" are actually taking the convention that function pointers are passed as callbacks with some explicit client data (mimicking closed values); for a simple example look into GNU libc qsort_r(3). For a more complex example look into the source code and coding conventions of the GTK graphical toolkit.
From both an implementation and a theoretical point of view, closures are mixing (like objects) data and code. In the RefPerSys system this property is used, since it aims to generate more and more of its code.
Books like the Pi-calculus (by Davide Sangiori and David walker) ISBN 0-521-78177-9, or a Theory of objects (by Martin Abadi and Luca Cardelli) ISBN 0-387-94775-2 or Artificial Beings - the conscience of a conscious machine (by Jacques Pitrat) ISBN 9781848211018 or Types and Programming languages (by Benjamin Pierce) ISBN 9-780282-162098 or Theories of programming languages (by John Reynolds) ISBN 0-521-59414-8 or Christian Queinnec's Lisp in Small Pieces are explaining all that much better than I could do.
If you can afford going to some university library or buying books, I recommend reading them.