I hear they are good, make it easier to maintain code-bases. Most often I reach for python to get the job done. Does anyone have experiences with functional languages for larger projects?

In particular I am interested to learn more on how to handle databases, and writing to them and what patterns they come up with. Is a database handle you can write to not … basically mutable state, the arch-nemesis of functional languages?

Are functional languages only useful with an imperative shell?

  • subversive_dev@lemmy.ml
    link
    fedilink
    English
    arrow-up
    2
    ·
    edit-2
    12 days ago

    Got to put my plug in for Elixir. Built on the legendary Erlang runtime, looks like Ruby and secretly a Lisp under the covers. The Ecto database library blows every other ORM or tool I have ever used out of the water.

    Regarding mutability, I would argue the runtime has everything you need like message passing, software transactional memory, pleasant I/O and the best exception handling you could imagine

  • DNEAVES@lemmy.world
    link
    fedilink
    arrow-up
    1
    ·
    12 days ago

    To answer the headline: Functional languages are as useful in pretty much any context non-functional ones are, they just need to be handled a little different.

    Functional languages’ goal tend to lean towards static or predictable typing and reduction of clandestine mutable state changes. State has to change in a program, it’s just about how its handled, and this is more about reducing referential update behavior.

    Like in Python, you can have a list, pass that to a function that returns None, but deep inside that function appends to the list, and you may be surprised that your list is different as a result. This would (typically) not happen in an FP language, you would have to explicitly return the changed list up the stack of functions, and as a dev you could see from a glance that the list could change as a result of the function, leading to less surprises.

    “IO” is of course an outlier, as it’s leaving the safety bubble the language provided. I could read the same database row twice in 60 seconds and get a different result, because someone else could make a transaction in that time. But once I have read the row and parsed it into a data-model, I can expect that my model in-code won’t change unless I tell it to.

  • FizzyOrange@programming.dev
    link
    fedilink
    arrow-up
    1
    ·
    10 days ago

    They don’t really let you do anything you couldn’t do in Python, they just let you write more elegant code.

    Personally I find ML-style languages to be difficult to read. They deliberately leave out a lot of the punctuation that makes code readable leading to code that just looks like a stream of words.

    Rust is I think the best option here - it steals most of the good ideas from functional programming but has saner syntax.

    Also you seem to be conflating pure languages with functional languages. I also made this mistake because Haskell is probably the best known functional language and it’s also pure… But they’re different things. OCaml is functional and not pure. You can use mutable variables to your heart’s content.

    TL:DR learn Rust not Haskell or OCaml.

  • darklamer@lemmy.dbzer0.com
    link
    fedilink
    arrow-up
    1
    ·
    12 days ago

    “Lisp is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days, even if you never actually use Lisp itself a lot.” — Eric S. Raymond, How to Become a Hacker

    There’s definitely some truth behind the hyperbole in that quote and there are quite a lot of techniques from functional programming that can be used also in a language like Python with good results, when used judiciously.

    • Kacarott@aussie.zone
      link
      fedilink
      arrow-up
      2
      ·
      12 days ago

      I highly agree with the sentiment. Learning languages of different paradigms is sort of like travelling to visit other cultures to make you a more rounded, better person. Learn a functional language (lisp/Haskell). Learn a concatenative language (forth/Factor). Learn a logical language (Prolog/?). Heck even learn an assembly! (I suggest RISC-V).

  • FishFace@piefed.social
    link
    fedilink
    English
    arrow-up
    1
    ·
    12 days ago

    Functional programming languages can be extremely beautiful when doing a task suited to them, but extremely ugly and cumbersome when doing anything not suited.

    With an imperative language you can always fall back on C-style for loops and manipulating state. It may not be beautiful but it’s fine. But if you need to use an STArray in Haskell, every single access is through a verbose function call without even array access syntax.

    So I would suggest either finding a toy project to do in a pure functional language, or else use a mixed paradigm language and make maximum use of its functional features.

    On databases, yeah database access is by definition stateful. It can be encapsulated in a monad, but that just means it’s not a good fit for pure functional programming.