Website message overhaul

bearophile bearophileHUGS at lycos.com
Sun Nov 20 15:05:20 PST 2011


Walter:

> Saying Scala supports functional is also a stretch, because it supports only one 
> of the three defining characteristics of functional programming. It doesn't even 
> support the other two in a half-assed manner, it doesn't support them at all.
> 
> I didn't invent my own definition of functional programming. I am using the 
> standard definition that anyone can look up. I don't see that as being 
> unreasonable, binary, or misusing the term.

This is an interesting discussion topic. Scala is widely regarded as a rather functional language (more than C#, less than F# and OCaML and Haskell). Why is Scala perceived as quite functional?

1) It has pattern matching, lazy stream lists similar to Haskell lists, its standard library is rather functional oriented, its has better type inference, is has a very powerful type system that allows to express lot of static invariants, and generally Scala encourages to use immutability over mutability, there are immutable collections too in its standard library, and they are preferred. All this makes Scala rather handy for functional programming despite it lacks "true" immutability and true purity.

2) It's a matter of syntax too. Scala has optional parenthesys, and other sylistic features that are pleasant for functional programming. While if you try to use a functional-style programming in D you often produce unreadable lines of code. I have shown this several times in the D newsgroup but nearly nobody seemed to care.

3) It's a bit a matter of perception too. People think of it as partially functional because lot of people do it, and it is advertised for this. Around it there are functional programmers too, even academic ones.

Regarding D immutability and purity, often you can't put a map() in a pure function, and many Phobos functions don't work with const/immutable data. This makes D almost a joke for functional programming. This side of D will improve, but Scala is there already.


import std.algorithm: map, filter;
int foo(immutable int x) pure nothrow {
    return 1;
}
void main() pure {
    immutable data = [1, 2, 3];
    auto m1 = map!((x){ return 1; })(data);     // OK
    auto m2 = map!((int x){ return 1; })(data); // OK
    auto m3 = map!q{ 1 }(data); // error, map is not pure
    auto m4 = map!foo(data);    // error, map is not pure
    //------------
    auto f1 = filter!((x){ return 1; })(data);     // OK
    auto f2 = filter!((int x){ return 1; })(data); // OK
    auto f3 = filter!q{ 1 }(data); // error, filter is not pure
    auto f4 = filter!foo(data);    // error, filter is not pure
}

Bye,
bearophile


More information about the Digitalmars-d mailing list