1

At the moment I'm aware of the following methods to integrate side-effects into purely functional programming languages:

  • effect systems
  • continuations
  • unique types
  • monads

Monads are often cited to be the most effective and most general way to do this.

Which other methods exist? How do they compare?

  • Possible duplicate of https://stackoverflow.com/questions/8666618/possible-means-of-side-effectst-in-purely-functional-languages ? – atravers Jan 20 '21 at 21:25

3 Answers3

1

Arrows, which are more general than monads.

Pavel Minaev
  • 99,783
  • 25
  • 219
  • 289
0

The very simplest method is to simply pass around the environment between the functions. This is often used to teach scheme.

Imagist
  • 18,086
  • 12
  • 58
  • 77
0

To me a more general way is via a monad/comonad pair. This generalizes the common "monad" approach which should correctly be called the "strong monad" approach, since it only works with strong monads.

Moving to a monad/comonad pair allows effects to be modeled that result in some variables no longer being available. An example where this is useful is the effect of migrating a thread to another host in a distributed setting.

An additional method of historical interest is to make the whole program a function mapping a stream/list of input events to a stream/list of output events. See: "How to Declare an Imperative" by Phil Wadler: http://www.cs.bell-labs.com/~wadler/topics/monads.html#monadsdeclare

RD1
  • 3,305
  • 19
  • 28