r/functionalprogramming • u/CemDoruk • Aug 28 '23
Question Which general purpose language has the best functional programming support?
So I just looked into functional programming with C++ and it is super ugly. So I started wondering which languages (that aren't purely functional) have the best support?.
Rust looks like it has good functional patterns but I have not used it.
30
Upvotes
20
u/sideEffffECt Aug 28 '23
Scala isn't purely-functional as such, yet it's very good at Pure FP with the ZIO or TypeLevel family of libraries. And you can even get a job with it.
11
u/WallyMetropolis Aug 28 '23
Clojure (or any Lisp, really, but it seems like Clojure is the mostly likely Lisp you'd find in a company's tech stack) has great support for functional programming. Though it takes a bit of a different approach than FP languages that put the type system front-and-center like Haskell and, to a lesser extent, Scala.
19
u/ops-man Aug 28 '23
Haskell
17
u/gclaramunt Aug 28 '23
Haskell, OCaML, Scala
23
u/jhartikainen Aug 28 '23
In my experience languages that weren't designed "functional first" tend to be, as you described it, super ugly when used with "heavily" functional code. For example while JavaScript is pretty easy to use in a functional style, if you start using FP style code more heavily (eg. say partial application and currying) the syntax becomes very "noisy".
8
u/[deleted] Aug 28 '23
I'm not sure if it counts, but on the .NET platform you can often easily combine languages, meaning you could write most of your program with object oriented C# but use functional F# for just e.g. your tests or backend.
8
u/Sarwen Aug 29 '23
I strongly discourage Rust for functional programming. Rust has indeed features that come from FP but only because they improve the imperative experience.
What all FP languages have in common, what could very well be the main criteria to decide whether or not a language support functional programming is how easy it is to treat function like normal values.
If you look at FP languages: Haskell, O'Caml, Scala, F#, ... The answer for these 4 questions is a strong yes. Even if there are major differences between these languages, manipulating functions as values is just as simple as any other usual values.
In Rust the story is completely different. Lambda have their own singleton type which is only known by the compiler. So you have to rely on dyn traits to store functions like you would store integers. The use of dynamic dispatch in Rust is often discourage because it's slower and prevent some optimisations. Composing functions is also a bit harder in Rust.
To conclude, Rust took in FP the features that made sense for imperative programming. Algebraic data types (Rust enums), lambads, type classes (Rust traits) are useful in procedural style.
O'Caml and Scala are very good FP languages but I would recommend learning Haskell to really deep into FP. The reason is that in O'Caml, Scala and similar multi paradigms languages, the incentive to find an FP solution to your problem is small because you can always solve it with regular OOP/imperative methods while in Haskell, you have to find an FP way to solve your problems.
7
u/jherrlin Aug 28 '23
Clojure is a good one
6
u/it_snow_problem Aug 29 '23
Clojure
15
u/jeenajeena Aug 28 '23
I'm not sure what you mean with "general purpose language" here: would Haskell match that definition? I assume you might mean "the most used", "not-niche" languages.
If you exclude the obvious case of the purely functional languages, I would say the all the languages in the ML family (such as F#) could probably exhibit the best functional support.
Outside that family, Scala has got notably a very good support. I would also add Kotlin, C# and Typescript.
Edit: typos
3
u/TEN-MAJKL Aug 29 '23
Of courshe Haskell, but also Rust has some pretty neat functional stuff
3
u/MrJCraft Aug 30 '23 edited Aug 30 '23
Just depends on how deep down the rabbit hole you want to go.
Many Procedural features are the same as functional just less strict of purity.its effectively a gradient from procedural to functional.literally by definition.
some of these are not general purpose.
C --> D --> Rust -> Common Lisp -> Scheme -> Clojure -> Elixir -> Scala -> Ocaml -> Haskell -> Idris -> Gallina
Lisp is more procedural, however is one of the most general purpose out of the bunch, even specific Lisp dialects specifically for micro controllers for example, also used in a few games, quantum computing, Chip Design, FPGA, and theorem proving. but again really depends on what you want or need.the Lisps in the list are Common Lisp Scheme and Clojure.
if you something that is better than C then D is a good option, has optional purity, and optional liveness checking, contract programming, and ok type level programming, especially if you think about types as predicates, Nim is another option in the same area.
Clojure Elixir Scala Ocaml and Haskell are really good for most server programming but could have some trouble on 3D graphics, and gaming related tasks.
anything Idris and beyond gets into the useful for very specific use case zone
3
u/[deleted] Aug 30 '23
Haskell is the best in FP, without competition. No need to be afraid of its purity.
2
u/Velascu Aug 28 '23
There are general purpose functional languages. They might not be between the most used ones but most of them are. Look at haskell or clojure which are my favorite examples. You can also use ocaml or scala if you want to mix the paradigms but yeah, there are some out there. As pointed out js has some functional capabilities, it was originally conceived as one but somewhat forced to be "more java like" bc it was the trendiest thing atm. However I wouldn't say it's a general purpose as its mostly web-oriented ALTHOUGH it can be used as one. You can build a whole app just with electron and js. I wouldn't recommend this tho bc, well, everything made in electron is going to eat way too much ram. I wish they find a way to fix this. Anyway, there are general purpose functional languages, look for them. Hf <3
2
u/jmhimara Aug 29 '23
I think Javascript makes it pretty easy to do functional-style as long as understand its limitations.
Maybe Kotlin as well, although I have very limited experience with it.
2
u/roguas Aug 29 '23
Javascript/Typescript, can be acceptable with dedicated team. I am interpreting general purpose as "popular".
2
u/hisatanhere Aug 31 '23
Rust.
2
u/UnusualSeaOtter Aug 31 '23
Ruby has surprisingly good functional primitives. ".each do" was basically my gateway drug into functional programming.
2
u/xiaodaireddit Sep 01 '23
C++
2
u/Voxelman Sep 01 '23
Do you really mean "general purpose" or do you mean "mainstream"?
General purpose is any functional language like Haskell, F#, Elm, Scala, Ocaml, just to name a few.
The best mainstream language with functional features is Rust. It is per definition not a functional language, but many ideas and concepts came from functional languages like immutability, first class functions, higher order functions and more. Even the memory management and the borrow checker are based on ideas from functional languages.
Other typical "mainstream" languages just have sparse functional features with ugly syntax.
2
u/cheater00 Sep 17 '23
Haskell
2
u/bullhaddha Nov 06 '23
The Scheme family of Languages has excellent support for functional programming. Also, they are much easier to learn than other languages.
2
u/DorukCem Aug 28 '23
I now realise I meant multiple paradigm
5
u/yeastyboi Aug 28 '23
Javascript has pretty good fp support. Check out Rambda JS. Rust is great too!
3
u/Collaborologist Aug 29 '23
None better than Clojure
Check the 2019 software engineer survey done by stackoverflow and you’ll see the most experienced and best paid engineers prefer Clojure
-2
u/drinkcoffeeandcode Aug 28 '23
Javas Streams library has a very nice API that feels quite natural
7
u/[deleted] Aug 28 '23
[removed] — view removed comment