6 ms·
> functors, monoids, monads, lazy evaluation These three things are mostly Haskell-specific though, at least in relation to functional programming.
by adamwk 5y ago
> functors, monoids, monads, lazy evaluation
These three things are mostly Haskell-specific though, at least in relation to functional programming.
- flowerlad 5y agoRust does lazy evaluation, and has functors, monoids and monads: https://docs.rs/functional/0.0.5/functional/trait.Functor.html https://docs.rs/functional/0.0.5/functional/trait.Functor.ht... https://docs.rs/functional/0.0.5/functional/trait.Monoid.html https://docs.rs/functional/0.0.5/functional/trait.Monoid.htm... https://docs.rs/functional/0.0.5/functional/trait.Monad.html https://docs.rs/functional/0.0.5/functional/trait.Monad.html
- yawaramin 5y agoRust 'does' lazy evaluation only in the sense that you can implement it if you go out of your way. It's not the default like in Haskell. It's not a functional programming language–having functors, monoids, and monads (in some restricted sense) doesn't make a language FP.
- pasabagi 5y agoI don't think it's particularly out of the way. Iterators are a normal part of rust, and are lazy.
- epolanski 5y agoLazy evaluation is not a requirement of functional programming. There's nothing really in typed lambda calculus requiring any laziness, that's just one possible way of expression evaluation. The only real requirement of functional programming is the support of first class functions, the possibility to pass and return functions like any other values. If you want to discuss your own interpretation of what makes a language more or less functional, which could be interesting, please provide a definition first.
- yawaramin 5y agoNote that I didn't say that lazy evaluation is needed for functional programming. I said that Rust doesn't have lazy evaluation, and that it's not a functional programming language. IMHO, a functional programming language requires: - First-class functions and function literal (lambda) syntax support - Expression-oriented syntax - Emphasis on immutable data structures - Tail call optimization Other features are 'take-it-or-leave-it'.
- pjmlp 5y agoSo given that ML languages don't require tail call optimizations, and allow for mutable data structures....
- yawaramin 5y agoAFAIK all ML languages support tail call optimization, and put the emphasis on immutable data structures.
- pjmlp 5y agoNope, Scheme is one of the few languages that require tail call optimisation as per language standard. There is no emphasis when mutability is one mut and ref cell away.
- yawaramin 5y agoOCaml doesn't have a language standard afaik, and its implementation(s) support(s) TCO, so imho that's more important. 'Emphasis' just means the 'paved path', i.e. default data structures like records and variants are immutable, default list type is immutable. Of course if you want mutable stuff it's there, but it's not the default.
- pjmlp 5y agoEven so, OCaml isn't all ML languages.
- lmm 5y agoRust doesn't have useful functors or monads since it doesn't have higher-kinded types.
- grumpyprole 5y agoThere's nothing Haskell specific about those concepts. In fact, the first three where developed by mathematicians long before Haskell even existed.
- AnimalMuppet 5y agoWhen mathematicians developed monads, they were doing abstract algebra. They were not using them as a kludge to (for example) impose sequence on functional programming. Having to use monads for sequencing is something that happens in Haskell and not in Rust.
- epolanski 5y agoJust to say, Haskell doesn't need monads to sequence computations, in fact Haskell as a programming language (1989) is older than Moggi's seminal paper on monads applied to programming in 1993.
- creata 5y agoTrue, but imo Haskell I/O was less than pretty before monadic IO came along: https://stackoverflow.com/a/17004448 https://stackoverflow.com/a/17004448
- creata 5y agoSure, I/O in Rust is a little easier because it's not wrapped up in an abstract type, but in exchange, Rust needs Try, async/.await and const-fn to solve just a few of the issues that monads solve in more generality. I don't think either choice is a kludge, it's just a bunch of trade-offs.
- lmm 5y agoMonads or something like them are vital to be able to recover imperative programming in a functional context. If functional programming means programming with functions (in the mathematical sense) then it means expression-oriented languages/styles, and how you express conventional (procedural) programming idioms in those is a key part of having a functional programming language that you can actually use.
- AlexCoventry 5y agoWhat's wrong with OCaml's approach of just breaking out in imperative-style code whenever it suits you?
- creata 5y agoImo, the advantage of "Haskell's approach" is that it's always obvious when you can and can't use imperative code, and the type system enforces it. I think this is important in many contexts, like STM[1] or Facebook's React[0]. But there are plenty of disadvantages: * it's a bit more verbose. * you can accidentally implement a weaker interface than the one you'd want, e.g. by only implementing Functor and not Traversable for your data structure. * the flip side of the benefits is that adding I/O to a previously pure function changes its interface. I don't know how it'd compare to OCaml's algebraic effects proposal, though. [0]: https://reactjs.org/docs/hooks-effect.html https://reactjs.org/docs/hooks-effect.html [1]: http://joeduffyblog.com/2010/01/03/a-brief-retrospective-on-transactional-memory/ http://joeduffyblog.com/2010/01/03/a-brief-retrospective-on-...
- lmm 5y agoYou lose the freedom to fearlessly refactor that's the main advantage of functional programming. If it's not clear which parts of the code can be reordered then you either just can't keep the code clean anywhere near as much, or you need much higher test coverage etc. to keep the same level of confidence when you do.