**doall**,

**dorun**, and

**doseq**. What are their differences, and when to use each one?

The functional language I know best is Haskell, so I will find what Haskell functions correspond to the Clojure ones, using Hoogle.

##
**doall.**

The documentation says:

When lazy sequences are produced via functions that have side effects, any effects other than those needed to produce the first element in the seq do not occur until the seq is consumed. doall can be used to force any effects. Walks through the successive nexts of the seq, retains the head and returns it, thus causing the entire seq to reside in memory at one time.Ok, we have a list where each element is produced through a side effect. Using

**doall**we perform all these effects "up front" and get the whole list as a result.

In Haskell, values which are obtained through side effects are "tagged" with the IO monad, to distinguish them from pure values. A value of type IO a could be understood as "a description of a program that, when run, produces a value of type a".

We want a function with a type like this:

[IO a] -> IO [a][] is the type constructor for lists. -> means a function from some type to another. For simplicity, in this post we only deal with lists whose elements are all of the same type.

We put the signature in Hoogle, we find the following

sequence :: Monad m => [m a] -> m [a]

Evaluate each action in the sequence from left to right, and collect the results.The type is more general but, when m is IO, this is the function we are looking for.

##
**dorun.**

The documentation says:

When lazy sequences are produced via functions that have side effects, any effects other than those needed to produce the first element in the seq do not occur until the seq is consumed. dorun can be used to force any effects. Walks through the successive nexts of the seq, does not retain the head and returns nil.In Haskell, functions which are executed only for side effects return values of type (). There is only one such value, also named (). It's roughly similar to void in C and Java.

Let's then search for the signature

[IO a] -> IO ()We find

sequence_ :: Monad m => [m a] -> m ()

Evaluate each action in the sequence from left to right, and ignore the results.The trailing underscore is a notational convention. It means that this is a function that behaves like the one without the underscore, but ignores the return value.

##
**doseq.**

The documentation says:

Repeatedly executes body (presumably for side-effects) with bindings and filtering as provided by "for". Does not retain the head of the sequence. Returns nil.Ok, it seems that now the values in the input list are "pure", but we want to apply an effectful function to each one of them. And we do not care about the return value.

So we must search Hoogle for something like

[a] -> (a -> IO b) -> IO ()We find

forM_ :: Monad m => [a] -> (a -> m b) -> m ()So that's it.

**doall**is

**sequence**,

**dorun**is

**sequence_**,

**doseq**is

**forM_**. I won't confuse them anymore!

A good introduction to Haskell IO can be found here.