[Haskell-cafe] Fwd: IO monad use

Tom Ellis tom-lists-haskell-cafe-2017 at jaguarpaw.co.uk
Sat Dec 29 10:30:04 UTC 2018


I guess you want

    return (nameStr, bd)

Every statement in a do-block must be of type `IO a` for some `a`. 
`(nameStr, bd)` is a pure value of type `(String, Maybe Float)`.  You turn
it into a value of type `IO (String, Maybe Float)` using `return`.

Tom

On Sat, Dec 29, 2018 at 10:08:32AM +0100, Damien Mattei wrote:
> ---------- 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

> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.



More information about the Haskell-Cafe mailing list