[Haskell-beginners] Lazyness and forM_

Kim-Ee Yeoh ky3 at atamo.com
Fri Apr 13 10:53:28 UTC 2018


Hi Marc, and welcome to the haskell beginners list.

```
> migrate :: IO ()
> migrate = do
>     dir <- dBDir
>     createDirectoryIfMissing True dir
>     forM_ (categories, expenses)
>        $ withDB . createTable
> ```
>
> tables are not actually created.
>
>
Well,

forM_ (categories, expenses) $ withDB . createTable

is equivalent to

withDB . createTable $ expenses.

So exactly one table is created.



> ```
> ["a", "b"] `forM_` print
> ```
>
> Actually prints both `"a"` and `"b"`.
>
>
Here the code uses square brackets--and so we have honest-to-goodness
lists--whereas the previous used parentheses.

See what happens with: ("a","b") `forM_` print.

Marc: Feel free to write to the haskell-cafe mailing list for questions
such as this. Fortuitously in this case, it turns out that your query
needed knowledge only about the forM* combinators and--ever since their ilk
was generalized--the known instances declared for the Traversable
constraint. As you explore the domain-specific package "selda" further, you
will find more people acquainted with the package over at the cafe than
here in beginners. Suffice to say, everyone here in this list is also in
cafe, which is also open to beginners questions.

p.s. Veterans would recognize this as ye olde controversy on the Foldable
instance for pairs introduced in GHC 7.10.x. The controversy still simmers
apparently because there isn't an instance for triples and higher tuples in
the latest and greatest GHC 8.4.1. We are at the mercy of this potentially
toe-stubbing absence of uniformity.

-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180413/8addc080/attachment-0001.html>


More information about the Beginners mailing list