Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I started seriously taking a look at Lisp this weekend (by which I mean I have only been learning Lisp and not reverting back to projects in C#) and must say I love it. I have dabbled with other functional languages (F#, Haskell, Erlang) but haven't felt the draw that Lisp has given me.

Now as I continue to learn Lisp, I have started wondering why non-functional languages don't support first-class functions. I know that languages such as C# can do similar things with delegates and to an extent you can use pointers to functions in C/C++ but is there a reason why this would never become a feature in those languages? Is there a drawback to making functions first-class? To me, it is extremely useful, so I am lost as to why more languages (outside the functional paradigm) don't implement it.

[Edit]I appreciate the responses so far. Since I have been shown that many languages now support first-class functions now, I'll re-phrase the question to be why would it take so long for languages to implement them? [/Edit]

share|improve this question
3  
I disagree that C# can do "similar things". I would say it has first-class functions for all intents and purposes (which sort of ruins your premise). It has a syntax for function literals, you can stuff functions into variables, etc. –  Logan Capaldo Feb 27 '11 at 16:58
    
@Logan - I was debating on whether including C# examples but decided to based on this wikipedia entry. According to the entry, there aren't true first-class functions since "since the objects which hold the function's dynamic state must be constructed manually". However, if that is an incorrect statement, I would love to know why! (After all, I'm here to learn and not be stuck in my ways!) –  Jetti Feb 27 '11 at 17:05
2  
that wikipedia entry is outdated. A modern C# provides a proper lambda. –  SK-logic Feb 27 '11 at 17:19
    
I think that paragraph is poorly written. It seems to be saying C++ functors aren't first class, and neither are C# delegates? While I might agree with the argument for functors, I'm not sure I would for C# delegates. It also says "(see lambda lifting)", which is a statement about these languages supporting lexical closures rather than "functions as values". You can have one without the other. Even if that is the case, it is not true for C#, it does have lexical closures. Part of the problem is "first-class function" can be a vague term. –  Logan Capaldo Feb 27 '11 at 17:28
1  
@Logan & SK-Logic - I appreciate your feedback and staying constructive. I am still learning and it is very nice not to be flamed, so I appreciate that. Thanks very much for the comments and answers! –  Jetti Feb 27 '11 at 17:58

5 Answers 5

C#, VB.NET, Python, JavaScript, now even C++0x provides first-class functions.

Update:

An implementation of closures requires lambda-lifting - a technique originally quite alien for the imperative programmers. Proper first-class functions (which includes closures) requires at least some support from the runtime and VM. It also took some time to adopt the old functional techniques into the imperative world.

And, the most important part - first-class functions are barely usable if there is no garbage collection present. It was introduced into a mass programming only recently, with Java and, consequently, .NET. And the original Java approach was to oversimplify the language in order to keep it comprehandable by the average coders. .NET first followed, and only recently parted from that (IMHO, quite justified and appropriate) direction.

All such concepts takes time to be digested by the industry.

share|improve this answer
    
I have edited the original question based on the responses. –  Jetti Feb 27 '11 at 17:09
    
First-class functions != closures (and GC is vastly more necessary for the latter). Although, yes, they are closely related and you rarely, if ever, see them seperated. –  delnan Feb 27 '11 at 17:31
    
@delnan, without at least some form of lexical closures functions are not composable and not constructable in run-time, which is mandatory for a first-class functions definition. –  SK-logic Feb 27 '11 at 18:44
    
No. You could disallow refering to variables of enclosing scopes (or copy the values of the referred variable at the time of function creation - dunno if this counts as closure). The result is not particular useful, but functions can still be constructed at runtime without being closures and therefore you'd have (weird) first-class functions. –  delnan Feb 27 '11 at 18:49
1  
@SK-logic: C++0x provides closures without garbage collection with the usual trick --> if the variable goes out of scope and you're still clinging to a reference, using the reference is undefined behavior. So it's possible... it just puts the honus on the developer. –  Matthieu M. Feb 27 '11 at 20:10

It's not really impossible. But it's yet another feature, and the complexity budget is limited. It may be considered unnecessary by (or not even occur to) the language designer - "why the heck would we need to pass functions around? This language is for programming an OS!". It may also take significant effort to merge with the rest of the language. For instance, a function type may require serious additions to the type system (e.g. in Java), even more if you have overloading (thanks @Renesis for pointing that out). You also need to provide some library code to make use of the viable - nobody wants to define map themselves.

It could also make other goals of the language harder to achive:

  • It becomes "harder" to optimize (not actually harder in many cases, but requires different approaches than the ones you're used to). For instance, if global functions may be reassigned, you have to prove that f is never reassigned before you inline it.
  • In a similar vein, if you make functions fully-featured objects, you need a smart compiler and JIT to remove the overhead associated with that and make calling as efficient (speed- and space-wise) as pushing arguments and return address and jumping to some address.
  • As already mentioned, it's another full feature and the first steps towards supporting yet another paradigm - if you want a simple language, perhaps you can't afford to add first-class functions without throwing other stuff (which you may consider much more important) out.
  • Perhaps it simply doesn't blend well with the rest of the language. If you design the language to e.g. be the best incarnation of OOP since Smalltalk, you may want to focus on that instead of offering another, very different, paradigm. Especially if it is hard to integrate the two (see above). N paradigms done well is more pleasant to program in than N+1 paradigms done badly.

In the same vein, one can ask why any programming language L1 doesn't have feature F of the very different language L2.

share|improve this answer

First-class functions are a lot less interesting without closures.

Closures aren't really possible without some kind of dynamic management of scope (garbage collection). One of the things that makes C/C++ so highly performant is the fact that it's a lot easier to compile a language where you manage memory manually, so that kinda takes C/C++ out of the equation.

And then yes, there is the matter of OOP impact. You no longer have a separation of methods and properties. Methods are themselves properties that accept functions as values. In that context, what does it really mean to overload methods since you can just swap the silly things out at any time. Can you really add that to Java, for instance, without introducing a major paradigm shift to the entire language? Where's the contract or that sense of (IMO false) security people get from knowing the construct guarantees it will always work the same way every time? It might make sense to treat functions tied to objects as methods as a different organism in languages that allowed functions to exist independently in the first place but in Java that's not really an option.

In JavaScript, OOP and functional are very much intertwined and I would personally find it hard to see first-class funcs as anything but bolted-on in languages where they weren't. But that requires a number of other paradigm-shifting changes including high levels of mutability in objects and their methods themselves as well as being able to call functions as if they were members of any object on the fly.

Languages aren't just tool-sets full of do-hickeys for getting things done for the most part. They're paradigms. Bundles of ideas, strategies, and opinions that all kind of go together in a way that's connected (or seem connected). In a lot of cases functions as noun and verb really just don't fit the paradigm at all or at best are an imperfect fit.

That said, I feel like I'm missing an arm when forced to code without them.

share|improve this answer
    
You can either treat all methods as sealed or just seal the ones you care most about, and you'll be fine. –  Alexei Averchenko Apr 30 '13 at 18:54
    
@AlexeiAverchenko I'm delighted to say I'm not 100% certain what you mean by that. Sorry I missed your comment 'til now. Googling "sealed methods." –  Erik Reppen Jun 23 '13 at 5:11

I think the first problem is a misunderstanding that Object-Oriented and Functional cannot be mixed. A quick list of languages described, at least by Wikipedia, as both: JavaScript, ActionScript, Python, C#, F#.

The second problem seems to be a definition of first-class functions - why do you not consider C# to have them, for example?

I'm not a functional language expert, so please correct me in the comments if I'm wrong, but my understanding is that the inclusion of first-class functions itself more or less defines whether a language supports a functional paradigm.

What's the real question?

So, understanding that object-oriented languages can also be functional, and these languages contain first-class functions, it sounds like the question you are looking for is why do some object-oriented languages not contain first-class functions?

Function overloading

One of the primary reasons for this is the difficulty of defining first class functions when the language has also chosen to support function overloading (example in Java):

public int dispatchEvent(Event event);
public int dispatchEvent(String event, Object data);

Which dispatchEvent function would a variable refer to?

Class-based programming

Class-based programming doesn't prevent a languages from supporting first-class objects, but it can make it more confusing or add questions to be answered.

ActionScript, for example, as an ECMAScript language started out in a much more functional paradigm like JavaScript. Functions weren't inherently bound to objects, they could essentially be called with any object as it's scope at the time of execution.

When you add (non-dynamic) classes, you have functions that are inherently bound to an instance, not just the class itself. This throws a few wrinkles into the specification of Function.apply that I honestly haven't dug in to find out how they have dealt with it.

share|improve this answer
3  
"The inclusion of first-class functions itself more or less makes a language functional." - Wrong. It's a prequisite, yes. But there's much more to it, like (to name just three) avoiding mutable state, the focus on expressions and function application rather than sequential statements and the emphasis on referential transparency. –  delnan Feb 27 '11 at 17:36
    
@delnan You ought to go correct Wikipedia then... Unless a "functional language" and "language supporting a functional paradigm" are two different things. –  NickC Feb 27 '11 at 17:40
1  
@Rensis: They are different things. You can create something like objects (even including vtables) in C, but it ain't pretty. Going from "has first-class functions" to "is a functional langauge" is not quite that bad, but it's still a difference. Also, wiki (correctly) states on the page you linked to "These features are a necessity for the functional programming style [...]." (i.e.: not "the defining feature") and lists several other features on the page on FP. –  delnan Feb 27 '11 at 17:44
    
@delnan, Alright, I corrected my wording. I just meant the condition for a language to be considered as supporting a functional paradigm - as does the Wikipedia page for each of those languages I listed. I didn't mean to say that's all that is a part of the functional programming style. –  NickC Feb 27 '11 at 17:47

I have wondered the same thing myself. Once I am in a language with lambdas I use them a LOT. Even PHP gets better when you realize that you can do a closure with php 5.3 (its not as nice as lisp, but still better)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.