[Haskell-cafe] Fwd: IO monad use

Damien Mattei damien.mattei at gmail.com
Sat Dec 29 09:08:32 UTC 2018


---------- Forwarded message ---------
From: Damien Mattei <damien.mattei at gmail.com>
Date: Sat, Dec 29, 2018 at 9:46 AM
Subject: IO monad use
To: Damien MATTEI <damien.mattei at gmail.com>


again an annoying error with my code, i want to apply sort of MAP on  list
resut of my database IO accessed extracting info with another query,
queries works both but i can not MAP or if i can i do not know how to SHOW
the result, here is the code:

 lstNamesBD <- mapM (\(Only name) ->
                               do
                                let nameStr = Text.unpack name
                                bd <- getBD conn nameStr
                                (nameStr,bd))
                       names

it fails to compile with this error:

Prelude> :load UpdateSidonie
[1 of 1] Compiling Main             ( UpdateSidonie.hs, interpreted )

UpdateSidonie.hs:203:33: error:
    • Couldn't match type ‘(,) String’ with ‘IO’
      Expected type: IO (Maybe Float)
        Actual type: (String, Maybe Float)
    • In a stmt of a 'do' block: (nameStr, bd)
      In the expression:
        do let nameStr = unpack name
           bd <- getBD conn nameStr
           (nameStr, bd)
      In the first argument of ‘mapM’, namely
        ‘(\ (Only name)
            -> do let nameStr = ...
                  bd <- getBD conn nameStr
                  (nameStr, bd))’
    |
203 |                                 (nameStr,bd))
    |                                 ^^^^^^^^^^^^
Failed, no modules loaded.


if i code like this  show does not know how to display result:

 let lstNamesBD = Prelude.map (\(Only name) ->
                                         do
                                          let nameStr = Text.unpack name
                                          bd <- getBD conn nameStr
                                          res <- (nameStr,bd)
                                          res)
                                 names

 putStr "lstNamesBD ="
 putStrLn $ show lstNamesBD

*Main> :load UpdateSidonie
[1 of 1] Compiling Main             ( UpdateSidonie.hs, interpreted )

UpdateSidonie.hs:193:16: error:
    • No instance for (Show (IO (Maybe Float)))
        arising from a use of ‘show’
    • In the second argument of ‘($)’, namely ‘show lstNamesBD’
      In a stmt of a 'do' block: putStrLn $ show lstNamesBD
      In the expression:
        do conn <- connect
                     defaultConnectInfo
                       {connectHost = "moita", connectUser = "mattei",
                        connectPassword = "sidonie2", connectDatabase =
"sidonie"}
           (rows :: [(Text, Double)]) <- query_
                                           conn
                                           "SELECT Nom,distance FROM
AngularDistance WHERE distance > 0.000278"
           (names :: [Only Text]) <- query_
                                       conn
                                       "SELECT Nom FROM AngularDistance
WHERE distance > 0.000278"
           let resLstNames = Prelude.map fromOnly names
           ....
    |
193 |     putStrLn $ show lstNamesBD
    |                ^^^^^^^^^^^^^^^
Failed, no modules loaded.

help me please
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20181229/988b7ea3/attachment-0001.html>


More information about the Haskell-Cafe mailing list