News for hackers. Stuff that matters.
Re:Programming Paradigms for Dummies: What Every Programmer ... by atilaneves on Monday November 05, 2018 @10:08AM (#18382828)
"Popular mainstream languages such as Java or C++ support just one or two separate paradigms"

Java, ok. But C++??? That language has everything and the kitchen sink, including pure functional programming (templates).

Re:Programming Paradigms for Dummies: What Every Programmer ... by mcguire on Monday November 05, 2018 @10:33AM (#18383043)
Keep in mind that the author is also one of the authors of Concepts, Techniques, and Models and the Mozart language, which supports logic programming anda variety of concurrent programming natively.

Edit: Oz is the language. Sorry.

Re:Programming Paradigms for Dummies: What Every Programmer ... by rq1 on Monday November 05, 2018 @10:55AM (#18383267)
Re:Programming Paradigms for Dummies: What Every Programmer ... by pjmlp on Monday November 05, 2018 @10:20AM (#18382926)
Even Java, after lambdas and immutable data structures introduction it is quite debatable.
Re:Programming Paradigms for Dummies: What Every Programmer ... by Fellshard on Monday November 05, 2018 @10:49AM (#18383207)
Higher-order functions are extremely constrained given that lambdas boil down to fixed interfaces, and there's very few tools for e.g. partial application and converting between function types. A lot of that is probably due to primitive variants and checked exceptions causing genericity to be extremely limited, so they capped the utility of lambdas in their design.
Re:Programming Paradigms for Dummies: What Every Programmer ... by pjmlp on Monday November 05, 2018 @10:54AM (#18383264)
What is relevant is that lambda calculus can be expressed.

Everything else are just variations on "What is FP" and sugar coating.

Re:Programming Paradigms for Dummies: What Every Programmer ... by bunderbunder on Monday November 05, 2018 @11:03AM (#18383357)
> What is relevant is that lambda calculus can be expressed.

That's a criterion that's so broad as to be almost meaningless, though. The lambda calculus can be expressed in any Turing-complete language.

I wouldn't personally consider a language to support a paradigm unless programming in that paradigm feels natural in that language. Java supports using a few functional techniques. The experience of trying to write in a truly functional style, though, is painful.

Re:Programming Paradigms for Dummies: What Every Programmer ... by pjmlp on Monday November 05, 2018 @11:12AM (#18383438)
Thing is, what does actually "The experience of trying to write in a truly functional style" mean in practice.

FP Lisp/Scheme, FP ML, FP Haskell/Miranda, FP Idris, FP Scala, FP Kotlin, FP OCaml, FP ATS, FP .... ?

All of them express different views of what Functional Programming is supposed to be like.

Re:Programming Paradigms for Dummies: What Every Programmer ... by bunderbunder on Monday November 05, 2018 @11:44AM (#18383738)
True. But I think that you can generally break them down into a few broad categories of functional style, and none of them can be followed comfortably in Java.
Re:Programming Paradigms for Dummies: What Every Programmer ... by pjmlp on Monday November 05, 2018 @12:39PM (#18384206)
Comfort is not a programming paradigm.

Just because only might need a bit more boilerplate for currying or partial applications, doesn't prevent writing FP libraries in modern Java.

Re:Programming Paradigms for Dummies: What Every Programmer ... by setr on Monday November 05, 2018 @03:11PM (#18385493)
But since its all turing complete... this can be said of anything. There’s nothing stopping you from doing OOP in haskell, or functional in C, or procedural in prolog, if you’re willing to put in the effort.

Disregarding “comfort”, or rather, how much the language lends itself to a style, makes the notion of programming paradigms meaningless, and we’re still left with something we all know exists, but have no way to express.

Re:Programming Paradigms for Dummies: What Every Programmer ... by Jtsummers on Monday November 05, 2018 @03:44PM (#18385784)
Different programming paradigms are not about what can be computed it is about what can be expressed in the syntax and semantics of the language. This is an important distinction. Brainfuck is capable, technically, of computing anything that Haskell can compute. However, it would be foolish to call it a functional (paradigm-sense) language. It lacks higher order functions and many other elements. You could construct a lambda calculus interpreter in brainfuck, but then that language would be functional, but not the underlying implementation language. By the same measure, Haskell implemented in C does not make C a functional (paradigm-sense) language.
Re:Programming Paradigms for Dummies: What Every Programmer ... by bunderbunder on Monday November 05, 2018 @04:12PM (#18386023)
There's a whole lot of stuff you can do, short of writing interpreters, to construct library-level support for a paradigm in a language that lacks language-level support for that paradigm.

For example, I would say that GTK+ is written in an object-oriented style. But, despite that, I would not say that OOP is a member of C's repertoire of language paradigms.

Re:Programming Paradigms for Dummies: What Every Programmer ... by Jtsummers on Monday November 05, 2018 @04:22PM (#18386090)
That is true, some languages are sufficiently expressive to allow other paradigms to be used within them even though they aren't baked in. But it's not true that all languages can support all paradigms (either directly in the language as designed, or indirectly via library support).

OO languages and functional languages support each other's paradigms (more easily) than a pure imperative language would support either. And both OO and FP languages support declarative styles (like relational or logic languages) better than C would.

EDIT: It's also worth pointing out that C++ really achieved OO (initially) by using macros on top of C. So having a sufficiently expressive meta-language is also important to this. Via such a meta-language, you can achieve many more paradigms in a language than using the language alone.

But many languages lack a meta-language or don't have a standard meta-language which people can rely on.

Re:Programming Paradigms for Dummies: What Every Programmer ... by jdmichal on Monday November 05, 2018 @05:20PM (#18386523)
> It's also worth pointing out that C++ really achieved OO (initially) by using macros on top of C.

And this is extremely relevant to the point. If the primary complaint against function programming in Java is "comfort" and "boilerplate"... Macros address both those problems very well. If Java had macros, it would be very simple to isolate and minimize that boilerplate.

Re:Programming Paradigms for Dummies: What Every Programmer ... by pjmlp on Monday November 05, 2018 @05:42PM (#18386682)
Java emulates macros via annotations and compiler plugins.
Re:Programming Paradigms for Dummies: What Every Programmer ... by jdmichal on Monday November 05, 2018 @06:03PM (#18386808)
Annotation-based methods are crippled by the limitations of annotation placement. For instance, you can't "annotate" an expression, which is where you will tend to see boilerplate of the type needed for partial application. (Because partial application is easiest emulated as a lambda which routes to the target function while providing fixed values to parameters.)
Re:Programming Paradigms for Dummies: What Every Programmer ... by rydel on Monday November 05, 2018 @10:35AM (#18383065)
Having lambda doesn’t mean to be FP. One of the core features missing in Java (JavaScript also) is TCO(tail call optimization)
Re:Programming Paradigms for Dummies: What Every Programmer ... by pjmlp on Monday November 05, 2018 @10:52AM (#18383246)
First of all not all FP languages have TCO, Scheme is probably the only one that actually requires it on its language specification.

Secondly stuff like LINQ was already available in Smalltalk.

So all those map/filter/fold/.... constructs from lambda calculus, which Java now enjoys.

Then if we apply the modern concept of only Haskell is FP, then there are a couple of FP languages that won't meet the classification.

Ah, and Haskell does not require TCO on their language specification, so it isn't an FP language according to your arbitrary definition.

Re:Programming Paradigms for Dummies: What Every Programmer ... by rydel on Tuesday November 06, 2018 @06:54AM (#18390148)
There are many definitions of FP. IMHO the “no side effects” is the best one. Even Clojure is functional, but partially IMHO, bc it is for JVM which has not been design for FP. And my definition is not an arbitrary one. This is the most broadly one I think. But when you use same word in different contexts, then the word might have different meaning.
Re:Programming Paradigms for Dummies: What Every Programmer ... by pjmlp on Tuesday November 06, 2018 @01:58PM (#18393893)
Which means OCaml, Common Lisp, Scheme, F#, SML are out of the game as FP languages according to you.
Re:Programming Paradigms for Dummies: What Every Programmer ... by rydel on Wednesday November 07, 2018 @04:40PM (#18402638)
Is I wrote: IMO these language are not fully functional. TCO/TCE is crucial for operations on tree like data structures. And also as I wrote: it all depends in which context we are talking to. Scheme specification makes it clear that TCO/TCE is required. If I am not wrong, F# is the same thing as Scala, but in .Net world(?) - Scala cannot be treaded as clear functional, because it is for JVM which was not designed for functional programming. Surely, many languages can have more or less functional functionalities, but having a subset of properties which defines what is functional, cannot be treated as functional in the full sense.
Re:Programming Paradigms for Dummies: What Every Programmer ... by rydel on Tuesday November 06, 2018 @06:57AM (#18390161)
Java class can be functional also. Depends how you look. If a class doesn’t operates on side effects, but keeps the mutation only inside the class, then the class can be defined as functional. But on the method level it might be not.
Re:Programming Paradigms for Dummies: What Every Programmer ... by rydel on Tuesday November 06, 2018 @06:31AM (#18390049)
It depends. As we know, FP is all about not having side effects. Having “for” loop requires to mutate the pointer of given iteration.
Re:Programming Paradigms for Dummies: What Every Programmer ... by pjmlp on Tuesday November 06, 2018 @01:50PM (#18393815)
Oh, and I thought all these years that OCaml was a FP language, go figure!

    open Printf;;

    printf "After all OCaml isn't a FP language\n";
    for idx = 1 to 10 do
        printf "%d\n" idx
    done
Same goes to Common Lisp, F#, Scala, Clojure.
Re:Programming Paradigms for Dummies: What Every Programmer ... by charlysl on Monday November 05, 2018 @11:57AM (#18383847)
According to the pdf's author, the crucial feature of FP is that there is no visible non-determinism. This means that every time you call a function with the same arguments it is guaranteed that you will get the same result. The other key feature is that there are no visible side-effects when calling a function.

Tail recursion of course is great to have, but you can certainly FP without it, even in a language that supports it. I mean, what if you don't put the recursive call in tail position in a function written in a language that supports tail recursion? It would still be FP.

Re:Programming Paradigms for Dummies: What Every Programmer ... by rydel on Wednesday November 07, 2018 @04:48PM (#18402696)
The no non-determinism feature is very good example why TCO/TCE is crucial - each time you call a recursive function the state is changing- another stack is created. Therefore the same function which operates on unknown argument size(list, tree) can have different behavior - stack overflow might happen or not and we don’t know when.
Re:Programming Paradigms for Dummies: What Every Programmer ... by pjmlp on Monday November 05, 2018 @12:45PM (#18384249)
You can get that in Java as well.

Just create a static class without member fields where the class plays the role of a poor man's ML module, with all static functions only interacting with their parameters.

Then static import it into the client package.

Re:Programming Paradigms for Dummies: What Every Programmer ... by charlysl on Monday November 05, 2018 @10:57PM (#18388229)
This is not the way to do FP in Java though. Even before lambdas were introduced, you could do FP in Java. This is actually the whole point of GoF design patterns such as Interpreter and Visitor, which together with Composite are the way to write recursive data definitions (and functions that operate on them) in Java (or C++). The whole idea of "Little Languages" is FP, representing operations as data, as an AST.

How to do FP in Java is very well explained in the MIT OCW course 6.005 "Elements of Software Construction", 2008 [1], in particular in lectures 10, 11, 13, 14 & 15.

Remember that you can create closures with inner classes.

As for the fact that in Java you could have state inside, say, a Visitor, the program can still be FP, if you know what you are doing. The point is that when doing FP in a language like Java, you are adopting a definitional approach to FP, which is totally legit: any operation is FP if it is deterministic and has no visible side-effects (a corollary is that then it wouldn't have any observable state of its own, it would be reactive). This is sort of a "if it walks like a duck ..." approach to FP.

In fact, this is exactly what is going on when you are doing reactive programming in a non FP language like Javascript, where there are no restrictions to doing destructive assignment anywhere. This doesn't stop you from doing reactive programming, as long as you follow certain guidelines (because the language won't stop you from infringing them and having state).

How is this possible? The reason is that the OO computation model subsumes FP: anything you can express in an FP language, you can express in an OO language, and then some (although not as naturally, there is more plumbing in the way).

The reverse is not true, and this is a good thing, it is exactly what allows FP to have all those desirable properties.

[1] https://ocw.mit.edu/courses/electrical-engineering-and-compu...

Re:Programming Paradigms for Dummies: What Every Programmer ... by Too on Monday November 05, 2018 @11:52PM (#18388467)
Sounds good in theory but is never going to work in practice. Even static functions can store state and access global singletons, disk or the internet. Java doesn't have a way to specify constness of arguments either so preventing mutating an argument is very difficult.

Is time and memory usage a side effect btw? :)

Re:Programming Paradigms for Dummies: What Every Programmer ... by zbuttram on Monday November 05, 2018 @03:22PM (#18385581)
Just wanted to point out that JS has TCO coming, just a question of if/when engines have it implemented. Looks like Safari/JSCore is pretty much the only one so far though: https://kangax.github.io/compat-table/es6/#test-proper_tail_...
Re:Programming Paradigms for Dummies: What Every Programmer ... by rydel on Tuesday November 06, 2018 @06:33AM (#18390055)
Yes. I am well aware about that. I look forward to TCO/TCE be supported on other platforms.
Re:Programming Paradigms for Dummies: What Every Programmer ... by lysium on Tuesday November 06, 2018 @01:16AM (#18388801)
I think the concept you describe is better named tail call eliminiation.

“Optimization” makes it sound as if you could turn it off or on for performance reasons. Instead, programs rely on tail call elimination being in place or otherwise they would not work.

Re:Programming Paradigms for Dummies: What Every Programmer ... by dnautics on Monday November 05, 2018 @10:41AM (#18383130)
Julia is missing TCO and it is for sure functional.
Re:Programming Paradigms for Dummies: What Every Programmer ... by 13415 on Monday November 05, 2018 @01:38PM (#18384705)
Tail call optimization is more or less unimportant, because you can express any recursion with iteration and most of the time the explicitly iterative version is even safer and better. TCO only adds zero-cost for recursions based on tail calls, that's nice to have but recursion is a bit of a hobby-horse of CS professors anyway. It only makes sense in languages that have their own stack, i.e., have no hard stack limit except for your main memory, otherwise you will run out of stack space soon. Iterative versions of functions are often easier to understand, too.
Re:Programming Paradigms for Dummies: What Every Programmer ... by nybble41 on Monday November 05, 2018 @05:29PM (#18386568)
> TCO only adds zero-cost for recursions based on tail calls

TCO isn't about recursion; it applies to any function call in tail position. Some toolchains only manage to avoid over-allocating stack frames for self-recursive tail calls, but that isn't full TCO, and it doesn't help for other interesting case like mutual recursion, state machines, or continuation-passing style. Compilers which compromise on full support for TCO emit programs with built-in memory leaks; they fail to free data which is no longer needed, and thus unnecessarily exhaust their stack allocation.

Programs which use built-in iterative constructs are still recursive; all that "recursion" means is that the control flow folds back on itself. A traditional "while" loop looks like: (1) stop if a condition is false; (2) otherwise do something; (3) do it again. The "it" in (3) is a recursive reference to the loop.

Now, unstructured explicit recursion is barely better than unstructured "goto", so I'm not advocating that everyone start using tail calls in place of loops. However, bare loops are in much the same position with respect to higher-order primitives such as non-strict folds—and those higher-order primitives are much easier to implement in environments which properly support TCO. Languages where TCO is not customary tend to suffer from a proliferation of built-in constructs—iteration, generators, list comprehensions, coroutines, and the like are all implemented as language features requiring custom code generation, where another language where TCO is customary might relegate such primitives to a library.

Re:Programming Paradigms for Dummies: What Every Programmer ... by Jtsummers on Monday November 05, 2018 @04:28PM (#18386145)
Recursion is great, IMHO, as a non-professor type. It allows for much clearer expressions of intent for my methods and functions than the iterative version often achieves.

There are some recursive structures that are just much more natural than their iterative counterparts. Parsing (as lysium suggests in the sibling post), for example.

But also things like graph and tree traversals, and many search algorithms related to those same structures. If you attempt a tree traversal iteratively, you have to maintain the return stack manually, rather than permitting the language to do it for you (assuming a full traversal and not a search, a search could be done iteratively without much trouble).

Re:Programming Paradigms for Dummies: What Every Programmer ... by 13415 on Monday November 05, 2018 @06:04PM (#18386814)
I'd say the opposite. Mutually recursive functions are notoriously hard to get right and debug, and many languages have stack limits. Often it's better to maintain the stack manually.
Re:Programming Paradigms for Dummies: What Every Programmer ... by Jtsummers on Monday November 05, 2018 @10:23PM (#18388073)
That has not been my experience, but I know many people who agree with you. IME, the difference has been that I came into CS with a more mathematical (formal) approach to programming, and they tended to come at it with a more mechanistic approach (especially when I hear it from non-CS developers, often EEs).
Re:Programming Paradigms for Dummies: What Every Programmer ... by rydel on Tuesday November 06, 2018 @08:15AM (#18390656)
It depends. As we know, FP is all about not having side effects. Having “for” loop requires to mutate the pointer of given iteration.
Re:Programming Paradigms for Dummies: What Every Programmer ... by pjmlp on Tuesday November 06, 2018 @01:52PM (#18393835)
As replied on another thread, you just killed a couple of FP languages with that definition.
Re:Programming Paradigms for Dummies: What Every Programmer ... by nybble41 on Tuesday November 06, 2018 @02:14PM (#18394034)
Those are not FP languages. They are, at best, "functional-first" multi-paradigm languages. (Common Lisp and Scheme are in this category.) Unfortunately, FP is more about what is excluded (side effects) than what is included (closures, continuations, sum types, etc.), so mixing FP with any amount of imperative/procedural code tends to result in an awkward and less efficient form of imperative programming without the primary benefit of FP (referential transparency).

The "function" in "functional programming" refers to mathematical functions, which are fixed mappings from inputs to results with no side effects. If your "functions" can have side-effects then they're not functions, they're procedures. Programs composed of effectful procedures are imperative, not functional.

Re:Programming Paradigms for Dummies: What Every Programmer ... by lysium on Monday November 05, 2018 @04:17PM (#18386063)
Did you consider mutual recursive functions in your answer? For example implementations of parsers?
Re:Programming Paradigms for Dummies: What Every Programmer ... by rydel on Tuesday November 06, 2018 @01:41PM (#18393706)
Another thing about recursion is that it is really powerful for operation on tree like data structures/objects
Re:Programming Paradigms for Dummies: What Every Programmer ... by fjsolwmv on Monday November 05, 2018 @10:25AM (#18382966)
Purely Functional programming via templates is a gimmick, not a productive style. In general you can't write a whole useful program in templates.
Re:Programming Paradigms for Dummies: What Every Programmer ... by tines on Monday November 05, 2018 @10:30AM (#18383010)
Depends. If you define fp as being manipulation of immutable data (i.e. math-like), then it’s quite feasible in C++. Gimmick or no, templates aren’t even necessary.
Re:Programming Paradigms for Dummies: What Every Programmer ... by aikah on Monday November 05, 2018 @11:54AM (#18383816)
Does C++ support lazy evaluation?
Re:Programming Paradigms for Dummies: What Every Programmer ... by jcelerier on Monday November 05, 2018 @12:12PM (#18383946)
templates are lazily evaluated
Re:Programming Paradigms for Dummies: What Every Programmer ... by aikah on Monday November 05, 2018 @12:16PM (#18383981)
> templates are lazily evaluated

At runtime? like Scheme?

http://www.shido.info/lisp/scheme_lazy_e.html

Re:Programming Paradigms for Dummies: What Every Programmer ... by jcelerier on Monday November 05, 2018 @03:57PM (#18385890)
no, at compile time (though there are plenty of ways to implement run-time lazy evaluation, with e.g. expression templates)