News for hackers. Stuff that matters.
Re:Which Programming Languages Are Functional? by mightybyte on Wednesday December 30, 2015 @10:17AM (#10812340)
I'm not sure if I like this definition of "functional". Don't get me wrong, I'm a Haskell guy and think Haskell is fantastic for production use, but this definition of "functional" almost seems to rule out everything except Haskell. Even if I try to maximize my Haskell fanboyism I can't with a straight face choose a definition for "functional programming language" that of the today's production-viable languages only includes Haskell. A definition that narrow just isn't very useful. I don't have super strong opinions about how it should be defined, but I tend to be drawn towards a more fuzzy definition. One that seems attractive is: "functional programming languages are languages that focus on functions as the basic unit of abstraction".
Re:Which Programming Languages Are Functional? by jarcane on Wednesday December 30, 2015 @11:11AM (#10812608)
I tend to think of it in that way as well.

To me, "functional programming" taken back to origins in Lisp and the lambda calculus is a style of programming that focuses on the composition of functions.

The "purity" element is merely the consequence of this focus: in order to be composable, our functions largely need to be free of side effects, and take and return values as mathematical functions do.

I think the emphasis on function composition also makes for a better, more believable sales pitch, once you take into account first-class functions as values and the kinds of abstractions and patterns they allow. I have a version of this pitch which goes like this:

Imagine a function that, given the right inputs, could produce any output in the known universe. To which you might rightly answer "that's pure nonsense". The sheer volume of internal logic would naturally be more complicated than everything within that universe.

But if that function can itself receive other functions as inputs, then the potential outputs expand almost infinitely.

And indeed, this is why the lambda calculus itself has proved Turing-complete: simply by composing functions within functions in this way, you can literally recreate the whole of computing and mathematics.

In practice though, people have their limits, and the day to day operation of a computer does require occasionally breaking the perfect mathematical puzzle for the sake of getting work done, and the vast majority of languages in existence allow for exactly that. A definition that doesn't allow for them might well be "true" in the "pure" sense of the word, but it's not very useful for people other than mathematicians.

And hell, even the Turing-machine uses mutable state.

Re:Which Programming Languages Are Functional? by Avshalom on Wednesday December 30, 2015 @01:12PM (#10813362)
Yeah I mean I for one am over ten years old so I remember when 'functional' pretty much just meant functions-as-first-class-values and every-thing-returns-a-value.

Then because those are pretty reasonable language features all the languages that had been written in the late 80s-mid 90s and became popular around 2000 had those features.

So then for a while 'functional' was just sort of way of saying 'not java'.

Now 'functional' seems to mean strong algebraic typing for some reason.

Re:Which Programming Languages Are Functional? by cygx on Wednesday December 30, 2015 @10:23AM (#10812376)
I explained it like this elsewhere:

The functional programming paradigm models computation as a relation between sets, and is thus inherently declarative. However, in practice, we often think of functions as imperative, ie you put in an input value and get out an output value, same as with a procedure. From this point of view, the characteristic property of a function is that it has no side-effects. Because of ambiguity of the terms, we call such a function pure, and a language which only has pure functions would be a purely functional language.

However, not all functional languages are pure: A functional language is a language with syntax and semantics which allows the programmer to use the functional paradigm efficiently. Some of the concepts which make using the paradigm feasible include - among others - lambda expressions with lexical closure, higher-order functions, variant types and pattern matching, lazy evaluation, type-inference (in case of statically-typed languages).

This is by no means an authorative list, and a language can very well be functional without providing all or even most of them, but if a language does - ie makes them usable without having to jump through major hoops - their presence is a strong indicator that the language should be considered functional.

Re:Which Programming Languages Are Functional? by seanwilson on Wednesday December 30, 2015 @10:25AM (#10812382)
It's a spectrum really. For example, OCaml is much more functional than JavaScript by having strong typing, inductive types, pattern matching etc. but lets you write functions with side effects unlike Haskell.
Re:Which Programming Languages Are Functional? by mightybyte on Wednesday December 30, 2015 @10:35AM (#10812430)
Haskell lets you do side effects. The difference is that the type system carefully controls them.
Re:Which Programming Languages Are Functional? by cygx on Wednesday December 30, 2015 @11:20AM (#10812675)
It's a matter of definition. It's perfectly consistent to say Haskell does not allow side-effects as they stop being such once they have been reified.
Re:Which Programming Languages Are Functional? by tome on Wednesday December 30, 2015 @02:16PM (#10813719)
But then it's equally consistent to say that C doesn't allow side-effects because the whole program is reified.
Re:Which Programming Languages Are Functional? by seanwilson on Wednesday December 30, 2015 @10:42AM (#10812460)
I was more referring to how OCaml allows mutable references and arrays, and allows IO similar to imperative languages whereas Haskell isn't so generous.
Re:Which Programming Languages Are Functional? by mightybyte on Wednesday December 30, 2015 @04:05PM (#10814361)
Haskell absolutely allows mutable references and arrays. See IORef [1] and MVector [2]. Notice how the functions that perform mutation are monadic. That's Haskell controlling the side effects and making sure that they don't pollute pure code. The ST monad stuff [3] that wyager mentioned is even cooler because it allows you to bundle up side effecting stuff and actually use it in pure code in a completely safe way, which is something that you can't do (without the dubious usafePerformIO) with the IO monad.

[1] https://downloads.haskell.org/~ghc/latest/docs/html/librarie...

[2] http://hackage.haskell.org/package/vector-0.11.0.0/docs/Data...

[3] https://downloads.haskell.org/~ghc/latest/docs/html/librarie...

Re:Which Programming Languages Are Functional? by wyager on Wednesday December 30, 2015 @12:28PM (#10813111)
Haskell actually does allow mutability in pure code using the ST monad.

It's pretty cool; you can use ST to have mutable variables and arrays and such, but the mutable values aren't allowed to escape the ST "context". So you're still guaranteed immutability outside of the ST monad, because the type system locks all mutable values inside the monad.

This is mostly only used in very-high-performance libraries that use algorithms based on array access.

Re:Which Programming Languages Are Functional? by typon on Wednesday December 30, 2015 @10:20AM (#10812361)
The wiki definition is on point in my opinion: "a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions".
Re:Which Programming Languages Are Functional? by jberryman on Wednesday December 30, 2015 @01:44PM (#10813529)
Where "function" means "like in math", right? Not "arbitrary callable block of code". I think it makes sense to think of "functional" as more or less synonymous with "approaching Haskell", even if you want to include many other languages under the umbrella.
Re:Which Programming Languages Are Functional? by jacquesm on Wednesday December 30, 2015 @01:05PM (#10813325)
> I'm a Haskell guy

Don't ever be a 'programming language 'x' guy or girl'. Be yourself, know some tools and skills and be prepared to switch those out for whatever works best in that particular application. If you limit yourself to a certain programming language, no matter how great it seems to be today you've built in your own obsolescence.

Better to be a guy or a girl with x years of Haskell experience and a whole bunch of other stuff besides.

Re:Which Programming Languages Are Functional? by draw_down on Wednesday December 30, 2015 @01:30PM (#10813452)
On the one hand I think that's reading too much into the phrase "Haskell guy", but on the other I disagree in the sense that being a dilettante is no good either. I think people should be "T-shaped", deep on one to a few languages/platforms and some experience with many others. Spending a long time with one language/platform will teach you deeper lessons over time than switching every 6 months.
Re:Which Programming Languages Are Functional? by mightybyte on Wednesday December 30, 2015 @04:13PM (#10814398)
I used that phrase as a more concise way of communicating pretty much what you are suggesting. I assumed that since my choice of X was Haskell it was pretty much a given that I haven't limited myself to a certain programming language and aggressively investigate promising new technologies.