4 ms·
This is what, attempt number seven hundred and fifty two to create a mainstream language that encourages functional programming? Incidentally, what's the most
by WilliamLP 16y ago
This is what, attempt number seven hundred and fifty two to create a mainstream language that encourages functional programming?
Incidentally, what's the most successful recent language that tries to make functional programming terser by not having an explicit "return" keyword (everything's an expression)? I'm genuinely curious, and don't think any significantly great number of programmers will ever find this style easier.
- rufo 16y agoRuby. (Technically Ruby does have a return keyword, but it's optional. Same goes for CoffeeScript, I believe.)
- draegtun 16y agoditto for Perl.
- Andys 16y agoNot really: for example, if/else statements are not expressions whose results can be assigned to a variable like in Ruby.
- draegtun 16y agoThis thread is about needing an implicit return. So my "ditto" is 100% correct! But you are quite right that Perl if/else are statements and not expressions. Instead you can use the ternary operator: my $value = $cond == 1 ? 'true' : 'false'; And because of ditto for Perl you can also do following: my $value = do { if ($cond == 1) { 'true' } else { 'false' } };
- endtime 16y agoCoffeeScript isn't an attempt to encourage functional programming so much as an attempt to combine the nice features of JS with a more modern syntax.
- WilliamLP 16y agoIs "square: (x) -> x*x" more modern syntax, or simply more functional? It surely isn't a new idea, maybe as old as Lisp, and a number of other new languages are explicitly imperative.
- rbxbx 16y agoPerhaps even the syntax JS should have had from the beginning, with it's heavy Lisp inspiration.
- jashkenas 16y agoI'm glad you picked out "everything is an expression" as the characteristic feature. I think that's completely correct -- everything else in the language is carefully compiled to make its use as part of a larger expression possible. I don't, however, think that it's about functional programming. It's about flexibility: if everything is an expression, you can use any bit of code in relation to any other, and not worry about what parts of the languages are merely statements ... a JavaScript example being "var a = b", which cannot be used as part of a larger computation. Having every function return a value is something that seems extraneous at first look, but ends up encouraging good API design. From the point of the caller of the function, returning a meaningful value is always appreciated -- even if it's as simple as returning "true" to acknowledge that the operation was completed successfully.
- tomjen3 16y agoint[] a = int[]{2,3,3,4} int s = a.Sum(i => i) Thats C# by the way - lambda statements have implicit return statements. Let me see, Ruby, Haskell and Scala uses this syntac.