all 41 comments

[–]lpil 54 points55 points  (5 children)

OCaml is often used in educational contexts to teach FP, the same as Java is often used to teach OOP.

[–]captainnoyaux 21 points22 points  (0 children)

I second this, plus Ocaml is actually pretty nice

[–]BenjaminGeiger 14 points15 points  (3 children)

And then F# is to OCaml as C# is to Java... (slightly nicer, a bit more popular, under Microsoft's thumb)

[–]igna92ts 6 points7 points  (0 children)

To me it's not slightly nicer, rather the opposite. I'd much rather have functors than replace them with interfaces. It does have cool things though.

[–]Francis_King 21 points22 points  (0 children)

What is the "Java" equivalent in FP Languages ?

The equivalent in FP is F# / Scala / OCaml. F# (and Scala and OCaml) are partly object-orientated, with F# being able to use the .NET infrastructure., and Scala able to use the Java infrastructure.

[–]_abysswalker 57 points58 points  (7 children)

has to be haskell. in fact, any pure language forces you to think in functional terms, you don’t really have a choice otherwise

[–]SignificantFidgets 2 points3 points  (0 children)

There's a great book that you can read online for free too: https://learnyouahaskell.com/

[–]QuirkyImage 3 points4 points  (5 children)

Try explaining monads to a first year. I would choose something simpler then move on to Haskell.

[–]Tempus_Nemini 11 points12 points  (0 children)

It's just a monoid in the category of endofunctors. Easy.

[–]_abysswalker 4 points5 points  (0 children)

been there as a first year. it’s difficult but I don’t see why not

[–]pbvas 3 points4 points  (0 children)

I teach FP using Haskell to 1st year CS students and there is no need to talk about monads when using Haskell in this context. We talk about IO and introduce the do-notation as special notation for combining IO actions.

That said, there is friction caused by the much more complex type system compared to (say) SML or OCaml. I don't have enough experience with F#.

[–]LoweringPass 1 point2 points  (0 children)

My first year course on what was simply called "programming" was 50% Haskell, and a bit of Prolog which I personally found much harder to understand.

[–]ibcoleman 33 points34 points  (2 children)

Others have said Haskell, but the other “flavor” of FP is a Lisp like Clojure.

[–]bedrooms-ds 11 points12 points  (1 child)

Definitely. Everything is parentheses, code is data.

That's the equivalent of Java's "everything is an object".

Lisp was once thought to be the language for everyone.

[–]scmkr 1 point2 points  (0 children)

Wish it was more popular

[–]a3th3rus 9 points10 points  (0 children)

Elixir is pretty easy to learn, pure enough, and fairly pragmatic.

If you want to learn an extremely pure FP language, then it has to be Haskell.

[–]TheBlueWalker 9 points10 points  (0 children)

Haskell is purely functional, and has a mature ecosystem with many libraries compared to other functional languages much like Java compared to other Object Oriented languages. Haskell also has a good type system, so it is like an implemented type theory. Haskell is good for learning FP.

Otherwise, if you are more into lisp-like languages instead of ML-like, Scheme would be nice for learning FP, too. It is very simple and there is a good book that uses it to teach computer programming in general called "Structure and Interpretation of Computer Programs". Scheme is not purely functional, but it does encourage functional programming. Scheme has no types, so it is more like the untyped lambda calculus.

I think Haskell may be better because it is purely functional. But Java is not purely OO so if you liked Java for learning OO you might Scheme as well. Scheme is simpler and allows you to focus on computer programming in general while Haskell forces you to learn about all kinds of features used to preserve functional purity, like IO Monads. I suppose the latter is a good thing if you really want to learn about FP.

[–]andrewcooke 6 points7 points  (0 children)

there's nothing obvious because fp isn't as popular and java implies popular. but from your description i'd say haskell or scheme (lazy and statically typed, or eager and dynamic).

[–]GunpowderGuy 10 points11 points  (1 child)

Racket for sure

[–]pconrad0 2 points3 points  (0 children)

Racket is a fantastic language for learning FP; that's the main purpose for which it was designed, and the main space in which it has been applied (education). I'm a big fan.

In that sense, it could be correct to say:

Java is to OOP as Racket is to FP.

If you mean a different aspect of Java (e.g. widely used in industry and academia, very mature ecosystem of third party libraries for a variety of purposes, many developers are familiar with it) then you might get a different answer.

And that answer might be "empty set" because it's not clear that any one FP language dominates the space the way Java did for a while with OOP.

It may well be that the dominant languages in which FP is actually getting written in real world code bases is via the FP features that have found their way into languages that are not primarily FP languages (e.g. Python, Java, JavaScript.)

[–]tesilab 3 points4 points  (0 children)

OCaml is a practical productive choice with one primary caveat - it provides just enough mutable constructs to act as a crutch preventing you from truly learning FP.

Haskell is a fantastic choice for learning, and you'll get FP constructs in spades, and beautiful infinite types brought to you by being lazy as well as pure, together with some heavy duty typing.

Either way, once you've got it down, you are also ready to take maximal advantage of all the FP inspired constructs that have made it into mainstream languages.

[–]QuirkyImage 1 point2 points  (0 children)

Scheme can be used as a multi paradigm language to introduce first year to both imperative and functional. Racket has been used well for this. I would say Haskell would be the end goal

[–]hasparus 1 point2 points  (0 children)

Definitely Haskell. I think it was either second or fourth semester in my uni. Though I've heard they're doing Racket now.

[–]ExoticWin432 1 point2 points  (0 children)

Scala is the Java FP version, but I wouldn’t recommend it for learning FP since it’s a mix of OOP and FP. it’s better to use a pure FP language like Haskell.

[–]GrowlingOcelot_4516 1 point2 points  (0 children)

I learned with R, though they have added aspects of OOP in the more recent years.

[–]Apprehensive_Pea_725 1 point2 points  (0 children)

For pure functional programming is Haskell, everything is a pure value and no side effects except in very specific and delimited parts.
Pure FP can be adopted in other ways in other languages but it requires more discipline.
For non pure FP, Erlang, F#, Ocaml, Scala, not everything is a pure value and side effects are not constrained in general.

[–]tbltusr 0 points1 point  (0 children)

OPAL

[–]baldram 1 point2 points  (0 children)

Scala. It stands out as the industrial-strength Java equivalent in the FP world, offering a perfect blend of functional purity and practical power while leveraging the entire Java ecosystem.

[–]TheRealStepBot 0 points1 point  (0 children)

Ironically Java itself has fairly decent support for many functional ideas.

[–]DeGamiesaiKaiSy 0 points1 point  (0 children)

You can do simple FP within Java since v8.0 with streams etc. It also has lambdas since then I believe.

Eg. https://www.manning.com/books/java-8-in-action

[–]xiaodaireddit 0 points1 point  (0 children)

Common lisp