Website message overhaul
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Sun Nov 20 15:31:05 PST 2011
On 11/20/11 5:05 PM, bearophile wrote:
> 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?
Marketing. Scala is most often contrasted with Java, so since it has
lambdas, higher-order functions, and free functions, it is "more
functional" than Java.
> 1) It has pattern matching, lazy stream lists similar to Haskell
> lists, its standard library is rather functional oriented, its has
> better type inference,
Not a functional thing.
> is has a very powerful type system that
> allows to express lot of static invariants,
Not a functional thing.
> and generally Scala
> encourages to use immutability over mutability,
There is no data immutability beyond "final". I am surprised that I get
to destroy with this even programmers who pretend to know Scala quite well.
> there are immutable
> collections too in its standard library,
Most languages allow defining a collection interface that disallows
change to individual elements.
> and they are preferred.
We're talking about support, not preference.
> All
> this makes Scala rather handy for functional programming despite it
> lacks "true" immutability and true purity.
For someone coming from Java, sure.
> 2) It's a matter of syntax too. Scala has optional parenthesys, and
> other sylistic features that are pleasant for functional
> programming.
Optional parens are not a functional things. In fact it's supremely
ironic to sustain otherwise because Lisp is the forefather of all
functional languages...
> 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.
In D you have a bit more of a difficult time producing compelling
examples. This is because D is multi-paradigm so your D functional
example must compete with a variety of other possible approaches, of
which often a different style leads to a simpler approach. In contrast,
in e.g. Haskell getting something done in a functional manner is pretty
much the only way to do it so there's no more discussion.
From what I saw there is a pattern of debate that goes like this:
bearophile: "I took this functional example in language X and translated
in D. The original X code is this: [weird mess]. The translation is
this: [weirder mess]. The D version looks bad and is slower, too!"
someone else: "They why the hell don't you solve the problem like this:
[simple code mixing paradigms]? It's smaller, simpler, and faster!"
> 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.
So let me make sure I understand: you find D's support for pure and
immutable insufficient (something I agree with), but Scala is "there
already" because it doesn't have any notion of pure and immutability?
> 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
> }
>
This would make a solid bug report.
Thanks,
Andrei
More information about the Digitalmars-d
mailing list