Mutability in functional programming

前端 未结 4 792
猫巷女王i
猫巷女王i 2021-02-05 13:02

First I am a Haskell newbie. I\'ve read this: Immutable functional objects in highly mutable domain And my question is nearly the same -- how to efficiently write algorithms whe

4条回答
  •  [愿得一人]
    2021-02-05 13:27

    There are of course many ways functional languages address this issue.

    1. Different data structures - many data structures can be implemented in a purely functional manner, with the same algorithmic complexity as imperative versions. Probably the most well-known work in this area is Chris Okasaki's Purely Functional Data Structures, but there are many other resources as well. For Dijkstra's algorithm, Martin Erwig's work on functional graphs is appropriate. See this question as well.

    2. Different algorithms - some algorithms have assumptions of mutability built-in, Quicksort is an example of this. In this case an alternative algorithm can be used that's more amenable to immutability.

    3. Mutable state - every functional language can model functional state with a State monad. Most provide other forms of mutability as well, such as Haskell's ST monad and IORef's.

提交回复
热议问题