0

I've never programmed in a "pure" functional language. I earned my stripes on C and C++, tried Java, C#, PHP etc... but always I found myself going back to C++. Perhaps I'm a bit of a masochist, but I love the low level stuff.

I also find that I can accomplish rapid development quickly through the embedding of LUA, Python or other scripting languages (along with their focus on rapid development).

Long story short, I'm not quitting C/C++ so don't talk me out of it. However I've had little time to learn C++11 and I'm starting to feel the acceleration of the curve towards functional programming happening in the future.

My question is twofold. What language was C++11's concept of lambda functionality "borrowed" from, and what language would be the ideal one, if not that one, or if any to get a feel for "the way" to use C++11's new lambda functionality (no pun intended).

PS: I'm honestly not too happy about the new "bloated" additions to C++. I liked C++ how it was, it's starting to feel like the language is becoming bloated. I won't clam that to be a fact; I hear you have to have experienced a functional language to "get it".

It honestly seems like there is a new heavyweight in town. First it was just "procedural" programming, then came the OOP paradigm shift, while now it seems like things are heading towards the "functional" way of doing things.

Of course procedural programming is still alive and well (inside classes), I have to wonder where the lambda way will fit in (properly used) to class/oop design. Will it just be a replacement for the procedural part? Make OOP a thing of the past (pfft)? Or something else entirely (say, a functional event system generating events for objects encapsulating procedural code)?

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
  • You might want to read a few chapters of [SICP](http://mitpress.mit.edu/sicp/full-text/book/book.html). That starts from the very basics of computer science, so it might seem a rehash of what you already know, but it treats them in an FP style, only later showing how imperative programming can be implemented in FP (!). That said, this question is off-topic for SO. – Fred Foo Sep 17 '13 at 09:00
  • I should reframe my question then. Legend has it that C++ borrowed "classes" from Simula. I'd ask, what language did it "borrow" lambda functions from, if any at all and short of that, what languages somewhat resemble the C++11 implementation. – Trae Barlow Sep 17 '13 at 11:23
  • Ultimately Lisp, but anonymous functions appear in lots of languages in various guises. Not Python, because that has a very restricted form of them compared to all the others. – Fred Foo Sep 17 '13 at 11:31
  • 2
    There are no lambda functions in C++11 only lambda expressions. It is a difference. It is just a convenient way to write functors. – knivil Sep 17 '13 at 13:55

2 Answers2

2

I would try to curtail your opinions until you have more rigorous experience of the issues involved.

To paraphrase Bjarne Stroustrup: Functional programming has had a lot of airtime in academia over the last several decades, yet the number of deployed functional systems in industry remains about zero.

More concretely to your question, a lambda is just a short-hand syntactic way to declare a singleton functor object (a class with an operator() function) that captures variables from its enclosing scope as member variables. I wouldn't consider it a "functional programming" concept, any more so than any other entity in C++.

Functional programming generally involves immutable data types (objects that dont change once constructed) and pure functions (functions that have output that depends purely on their input, and nothing else).

If you are interested in functional programming there is a free online course (MOOC) starting right now called Functional Programming Principles in Scala, that serves as a very good and highly regarded introduction to the subject from one of the top Swiss universities.

Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
  • Seems a lot like LUA's "closures" http://www.lua.org/pil/6.1.html In that it captures variables from the enclosing scope as member variables. Difference being that in C++11 you'd have to declare it as a lambda function(?) while in LUA it's done standard. – Trae Barlow Sep 17 '13 at 11:27
  • @TraeBarlow: Two points. (1) Capturing just means "taking a copy of" or "taking a reference to", it is no different than normal variable assignment. (2) There are no local functions in C++ (a function defined in the body of another function), so there is no opportunity for it to be "done as standard". – Andrew Tomazos Sep 17 '13 at 11:46
  • 1
    Functional programming does not involve immutable data types. It is a feature not a requirement (e.g. in Haskell but not in Scheme). Functional programming requires that functions are first class objects. Functional programming enhance your program or what do you think where the header or comes from? And Scala is not a good functional language to start with, I would recommend Scheme. – knivil Sep 17 '13 at 13:52
  • @knivil: _"Functional programming does not involve immutable data types"_: From first line of functional programming article on wikipedia: "functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and __avoids state and mutable data__". If you disagree, then please provide your definition of what functional programming is. – Andrew Tomazos Sep 17 '13 at 13:54
  • 1
    Today the problem is when speaking about FP everyone thinks Haskell. That is wrong. I also provided a definition: programming language with first-class functions. Great point: job offers vs topic, topic wins you lose. This is not about Scala vs Scheme. – knivil Sep 17 '13 at 14:08
  • 1
    @knivil: Can you cite a source for your definition of "functional programming"? – Andrew Tomazos Sep 17 '13 at 14:15
  • @knivil: From the `functional-programming` tag description on this site: "[In functional programming] functions take arguments and return results, but typically __don't mutate state__. This in contrast to imperative programming, which primarily revolves around statements that change state. The advantage of __avoiding mutable state__ is that you can safely compose functions, and you can use algebraic laws and "substitution of equals for equals" to simplify programs or improve their performance." Sorry but your definition is incorrect. – Andrew Tomazos Sep 17 '13 at 14:40
  • 1
    Why is Scheme a functional language, what does typical mean, why is it important to distinguish FP by adding 'pure'? Btw. I know about advantages and disadvantages of avoiding mutable state. I am typically avoiding it in C++ also. – knivil Sep 17 '13 at 15:03
  • 1
    @knivil Scheme (and Lisp) is a multiparadigm language and is not indicative of purely functional programming. Referential transparency is a core principle of FP and implies immutability. – Cat Plus Plus Sep 17 '13 at 15:34
  • 1.) Haskell is also a multiparadigm language. What's the point? 2.) You are moving from functional to pure functional. 3.) I do not say that Scheme is a pure functional language. – knivil Sep 18 '13 at 12:20
  • That quote didn't age well for Stroustroup as the company he is affiliated with (Morgan Stanley) uses Scala. International investment firm Jane Street uses OCaml. Standard Chartered bank has its own Haskell variant. Barclays (massive banking group) hired lots of Haskell developers recently. Facebook's content filters are written in Haskell. These are some big examples. – madgen Sep 23 '18 at 23:59
0

I can't speak about lambdas in C++11, but I know that part of the rationale for adding lambdas to Java 8 is to enable transparent concurrency support out of the box. How? It provides a (lazy) Stream interface where you can switch between parallel and sequential processing simply by calling parallel and sequential (these methods return new streams, and do not have side effects on existing streams).

If you look at the methods in Stream, you'll quickly notice that without a lambda facility, they would be an extreme pain to use. Have a look at some examples of what you can do with streams in combination with lambdas.

It should be possible to implement a similar library for C++11, if there isn't already such a library.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • Unfortunately, I'd figure such a parallelism thing could run into plenty of memory-related issues in C++ that you don't have in Haskell, nor apparently in Java (though I suppose you still need to be much more careful there, what with mutable state etc.). – leftaroundabout Sep 17 '13 at 11:40