8 ms·
Which Programming Languages Are Functional?
- DanielBMarkham 11y agoStandard reminder: please remember OCaml and F#. OCaml may be a little obscure in the mainstream programming community, but there are a ton of folks using F# out there.
- deleted 11y ago[deleted]
- drchickensalad 11y agoThere are a ton of folks using f# out there? Would be nice to hear about them.
- DanielBMarkham 11y agoSome testimonials: http://fsharp.org/testimonials/ http://fsharp.org/testimonials/ Don't have time to do the full internet report for you, but if you're interested, the way to tell is simply google various programming problems with F# instead of C#. Five years ago you could mostly find C# code and convert in your head. Now about 80% of the time you can find what you're looking for in F#. That's a ton of people.
- seanwilson 11y agoMaybe I'm missing something, but what does implicit "this" arguments have to do with functional programming? > public String getName() { return this.name; } > How would we purify this call? Well, this is the hidden input, so all we have to do is lift it up to an argument: > public String getName(Person this) { return this.name; } > Now getName is a pure function. Isn't that just syntactic sugar? I would say that a real functional programming language enforces that functions cannot have side effects, not just that you have the ability to write such functions.
- typon 11y agoHow is it syntactic sugar? In the second version the function is now accessing an argument rather than something outside its scope. That's pretty much the definition of a pure function (besides having no side effects).
- seanwilson 11y agoI would agree if getName was accessing a global variable but I don't see how getName(person) is any more functional than person.getName(). OCaml features objects with similar syntax for example: http://caml.inria.fr/pub/docs/manual-ocaml/objectexamples.html http://caml.inria.fr/pub/docs/manual-ocaml/objectexamples.ht...
- typon 11y agoBecause you can write person.getName() like this: def getName(): . . this.setName('foo') . . return this.name There's nothing in principle preventing you from doing that in the "functional" approach but if your data is immutable then you can't do it.
- Someone 11y agodef getName(person): person.setName('foo') return person.name The problem, from a functional viewpoint, isn't the implicit argument, it is the fact that you can write setName.
- mightybyte 11y agoI'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".
- typon 11y agoThe 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".
- cygx 11y agoI 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.
- typon 11y agoIts worth mentioning Elm when talking about making Javascript functional. To me Elm is like the fun sibling in the Haskell family. Signals are way easier to understand (for me personally) than monads. I just wish it had better Javascript interop. Making ports for everything to talk to an existing API (like three.js) is a deal breaker.
- innocentoldguy 11y agoI feel the same way about Elm. I am curious regarding your comment about ports though. I will admit, I haven't used ports for connecting with three.js, but I have used them to tie in with existing Javascript code during our conversion process and I thought they were pretty nice. What makes the combination of three.js and Elm ports a problem. Is it just the number of them you have to write, or is there some other pain point?
- typon 11y agoYea essentially its a matter of scale. three.js is not a small library by any means and I'm using a lot of the features. I tries to use:https://github.com/seliopou/elm-d3 https://github.com/seliopou/elm-d3 as my guide. I think it just comes down to "I don't have time to rewrite bindings for an entire API right now" because ports require you to really understand what's going on. There's no shotgun method of converting an entire API. Another reason I gave up is that Purescript does have three.js bindings.
- innocentoldguy 11y agoYeah, that makes sense. Thank you for the information.
- tripa 11y ago> [Perl] has a magic argument, $_, which means something like, "the return value of the previous call". What?!
- Mithaldu 11y agoI don't know whether you recognize this as wrong, or are surprised at the thought of such a thing (which doesn't actually exist like that). In any case, i've suggested a correction here: http://blog.jenkster.com/2015/12/which-programming-languages-are-functional.html#comment-2432154798 http://blog.jenkster.com/2015/12/which-programming-languages...
- tripa 11y agoI recognize this as wrong, and am surprised by how he could have gotten that idea. Good job with the correction.
- jerf 11y agoWell, try to explain $_ in a short sentence without being wrong. "It's the perl equivalent of the pronoun 'it'." OK, now try explaining it in proper engineering terms that describes exactly what it is, how to use it, and in some sense, how to implement it, in about that many words. At the very least, I suspect honesty will compel you to admit that either A: that took you longer than you expected or B: you left out a lot of corner cases, especially when modifying $_ does or does not change the thing it does or does not reference, and how it interacts with local, and, and, and....
- dozzie 11y agoHere you go: default variable to pass string argument to many built-in functions and operators, which typically return their result in the same variable. Yes, it took a little longer. No, it didn't took that much longer to leave out plainly invalid and misleading description. No, I didn't need to put corner cases there, exactly the same way as you wouldn't specify corner cases for Yacc to describe what it does.
- chvid 11y agoBut why would you want a "functional programming" language when you can have a modern multiparadigm language (JavaScript 6, Swift, Java 8, C#)? The author seems to believe that no side effects is the key property of a functional language. But for me most problems I deal with (user interface, various business domains, integration with other systems over network etc.) are filled with state and procedures. Sure you can deal with that in a purely functional way but to me it is a bit like doing object-orientated programming in C with macros and a lot of conventions. Why not simply use a programming language with suitable primitives? In particular now that multiparadigm languages are the most widely used in our industry.
- seanwilson 11y agoControlling state the way functional languages do makes code significantly easier to test. Besides this, functional languages have many great features such as strong typing, pattern matching and inductive types that have yet to be adopted by any mainstream languages.
- chvid 11y agoDon't get me wrong; I think "no side effects" is a very useful way of programming for a large number of problems. But not all problems. In particular problems related to user interface and system integration. And languages I listed (JavaScript, C#, Swift and Java) have all stolen large chunks of ideas pioneered by functional programming and put it into their latest release.
- tombert 11y agoIf you take a language like Haskell, you can utilize an FRP framework like Netwire to write pure-ish code whilst having reactive things. I'm currently writing an ncurses application that does exactly that.
- davexunit 11y agoStatic typing is not a requirement for a functional programming language.
- konne88 11y agoWhy not just call the post: "Which Programming Languages are Pure?". What's the point of redefining Functional Programming in a way that is at best controversial?
- cygx 11y agoA language can be purely functional, purely object-oriented, purely garbage-collected, ... The sole specifier 'pure' is meaningless without context...
- lmm 11y ago"Functional" is just a word, and I don't think it's particularly helpful to declare "language X is not functional". map/reduce, lambda, types, and control of effects are all valuable, and no single one absolutely characterizes functional vs. not.
- canjobear 11y agoWould Scheme count as a functional programming language by this definition? On one hand, the idiomatic emphasis on tail calls (supported by tail call optimization in the language) makes it easy to avoid stateful for and while loops. On the other hand, most nontrivial code has plenty of `set!`s.
- davexunit 11y agoScheme is a multi-paradigm programming language. Functional is one such paradigm that it supports. It also supports imperative, object-oriented, relational, and probably others that I don't even know about. In Scheme, one can build functional systems on top of imperative building blocks. For example, memoization can be implemented as a higher-order procedure that uses a mutable hash table as the cache. A memoized function is still pure, despite the use of local state, because it retains the crucial property of referential transparency. Anyway, I guess my point is that the use of 'set!' or 'hash-table-set!' or whatever else doesn't mean that a program is no longer using the functional paradigm. To quote from a Clojure web page[0], "if a pure function mutates some local data in order to produce an immutable return value, is that ok?" [0] http://clojure.org/transients http://clojure.org/transients
- pluma 11y agoFriendly reminder that all programming languages are just tools and it's not productive to get worked up about which one is better than others or which arbitrary label makes them more valuable. Except PHP. PHP still sucks.
- noir_lord 11y agoWas with you till the second sentence.
- pluma 11y agoThat was the joke.
- davzie 11y agoPHP Lives Matter!
- deleted 11y ago[deleted]
- actsasbuffoon 11y agoI half agree. A hammer and a screwdriver are both just tools, but they're good at different things. It's good to know about the variety of available tools and the sorts of tasks they're good at.
- pluma 11y agoSure, but in many situations the tool you already have and understand trumps the theoretically better suited tool you have to pick up at the hardware shop and train your team to use. The logical extreme would otherwise be to switch to a new tool whenever one comes out that is objectively better at a given task and retrain every team member that was previously using the old tool and rewrite the code that relies on the old tool to use the new tool. This balance is called pragmatism and it's more important in the real world than the objective quality of tools you don't already have in your toolbelt.
- rdtsc 11y agoErlang currently is probably the most practically used functional language. It has been in production for decades. It manages probably 50% of world's smartphone <-> internet traffic. There are many database built on it, message processing backends, handles hundreds of millions of connected device (WhatsApp). Yet most people forget it about when talking about "functional", and end up comparing Python to Haskell basically. Maybe with F# thrown in there for good measure. Erlang has solid closure support, tail call elimination, immutable data structures and variables, encourages using recursion instead of for loops, in practice folds/maps are common constructs instead of an oddity etc (like say in Python). It even has an optional success typing system: http://learnyousomeerlang.com/dialyzer http://learnyousomeerlang.com/dialyzer EDIT: Elixir is using Erlang's VM as well and almost everything about language features above apply to it. If you don't want to give Erlang a try, check out Elixir. It also has one of the friendliest communities for new-comers.
- im_down_w_otp 11y agoIt's also driving almost all of the non-Google ad networks, has a pretty sizable footprint in high-finance, and increasing numbers of online gaming (mobile and console) backends are built with it (or its younger cousin, Elixir).
- mml 11y agoOne of the most powerful things about Erlang is that there is no state other than what you are passed in locally (of course, there are still side-effect dumps, like databases & files, but those can be well-managed behind gen_server). This property makes Erlang programs incredibly easy to reason about and test.
- rdtsc 11y agoAgree with that. Makes it very nice jumping in understanding a complicated system -- because it is obvious how state is mutated by looking at the current function. In a typical state heavy OO, I found I am lost more often because I have no idea what the current state is and how it got to that state if it is broken when debugging.
- 11y ago
- im_down_w_otp 11y agoAll the ones that aren't dysfunctional.
- AnimalMuppet 11y agoArgh. One second after clicking downvote, I got the joke...
- im_down_w_otp 11y agoJerk :-p
- MichaelGG 11y agoHmm I've always felt the true nature of FP is having higher order functions. That's what starts to make things fun. If you need a long keyword to introduce a lambda, or if partially applying and passing functions around is ugly then you lose a lot of the benefits and cannot truly embrace HOFs. In a typed language, no generics or other type limitations puts a major cramp on this too. Yeah this means C function pointers allow some FP. Except they're clunky and have a hard time bundling info with them (no closures). This makes HOFs sort of a pain. Controlling state, or at least immutability, is just a generally good idea. Even in rather non-FP code I wish I had easy immutability.
- Grue3 11y ago> Lisp is the oldest functional programming language, and the oldest dynamic language. But Lisp is not a functional language! It's based on the concept of CONS which is mutable and allows for shared structure. Lisp is #1 language that everyone thinks is functional when it's actually not. I'm surprised this article out of all made this mistake.
- davexunit 11y agoWell, Lisp is based on the Lambda calculus, which is what we've come to know as the basis for functional programming. Things are much more overtly functional when you look at certain members of the Lisp family such as Scheme.
- chchrutuch 11y agoI can find points I disagree with on every language here, but this author seems to have quite the bias against Java, so I'll make a few counterarguments. "If you write Java code that has no side-effects, that doesn't read or change the local object's state, you'll be called a Bad Programmer." No you won't. "Java thinks that localised side-effects are cornerstone of good code" Java doesn't think anything. It's a tool. "Unfortunately, in practice Java doesn't just try to encapsulate side-effects; it mandates them" No, it doesn't. I will say I've worked in Java shops and seen the language used in this way. I've also worked in Java shops and seen people who have read and internalized the messages in Effective Java, especially with regard to handling state, and the codebase looked nothing like what is described in this article. I recommend the author take some time to read that book and keep reminding himself that Java is a generalized tool that can be used in many ways.
- mark_l_watson 11y agoI take issue with the critique of Java not being functional. Old style Java uses mutable objects and yes code is not functional. However, it is now (a good) standard) practice to make immutable objects.
- pklausler 11y agoIMO the essence of programming is the definition of abstract interfaces and the composition of components within a context according to those interfaces. The essence of functional programming is the composition of components that create their results without modifying their execution context in any way that could be detected by a failure of referential transparency. With that definition, functional programming is a style, not a class of languages. An assembly routine that computes a cosine is a functional program. I typically write C/C++ code in a functional style, constructing and returning new data structures rather than modifying arguments in place, because it's so much easier to test, use in parallel code, and reuse later. So I don't think that strong typing, higher kinds, higher order functions, algebraic types, pattern matching, anonymous closures, polymorphism, or garbage collection, e.g., are necessary to functional programming (although they're all wonderful and Haskell seems to have collected just the right mix of features together into a language that I love using whenever I can). It's about whether or not your components have interfaces that are like mathematical functions.