[Haskell-beginners] Lazyness and forM_

Marc Busqué marc at lamarciana.com
Fri Apr 13 09:42:20 UTC 2018


Hi there!

I'm using [selda](https://github.com/valderman/selda) package to work
with databases.

I'm trying to write a function to generate several tables at once. In
the following examples, `categories` and `expenses` are two selda
`Table`. The other functions in use and their respective imports should
be self-evident:

If I do:

```
migrate :: IO ()
migrate = do
     dir <- dBDir
     createDirectoryIfMissing True dir
     forM_ (categories, expenses)
        $ withDB . createTable
```

tables are not actually created.

However, if I do:

```
migrate :: IO ()
migrate = do
     dir <- dBDir
     createDirectoryIfMissing True dir
     withDB . createTable $ categories
     withDB . createTable $ expenses
```

Both tables are actually created.

So it seems like the thugs created with `formM_` are actually never
executed. Is it so? I'm new with Haskell and it seems strange for me. If
it is so, doesn't make it useless `forM_` for IO?

Furthermore, I can't reproduce it in ghci. Doing this:

```
["a", "b"] `forM_` print
```

Actually prints both `"a"` and `"b"`.

Thanks for any enlightment.

Marc Busqué
http://waiting-for-dev.github.io/about/


More information about the Beginners mailing list