yes i use do notation, but for example i have code that works in main and not in a function!
i print the code perheaps someone could help me:
first the function, so you have the import list too:
import Database.MySQL.Simple
import Database.MySQL.Simple.QueryResults
import Database.MySQL.Simple.Result
import Database.MySQL.Simple.QueryParams
import Database.MySQL.Simple.Param
import Control.Monad
import Data.Text as Text
--import Data.Int as Int
--import Data.List
import Debug.Trace
import Data.Maybe as Maybe
-- this function will return th N°BD from Sidonie for a given name
-- note: names have been standardized between Sidonie and WDS
getBD3 :: Connection -> String -> Float
getBD3 conn name = do
let qry_head_BD_Sidonie = "select `N° BD` from Coordonnées where Nom = ?" :: Query
(bd_rows :: [Only Text]) <- query conn qry_head_BD_Sidonie (Only (name::String))
let noBDtxt = fromOnly (Prelude.head bd_rows) :: Text
let noBDstr = Text.unpack noBDtxt :: String
let noBDfp = read $ noBDstr :: Float
return noBDfp
with this function i have this error:
Prelude> :load UpdateSidonie
[1 of 1] Compiling Main ( UpdateSidonie.hs, interpreted )
UpdateSidonie.hs:54:13: error:
• Couldn't match expected type ‘Float’ with actual type ‘IO Float’
• In a stmt of a 'do' block:
(bd_rows :: [Only Text]) <- query
conn qry_head_BD_Sidonie (Only (name :: String))
In the expression:
do let qry_head_BD_Sidonie = ...
(bd_rows :: [Only Text]) <- query
conn qry_head_BD_Sidonie (Only (name :: String))
let noBDtxt = ...
let noBDstr = ...
....
In an equation for ‘getBD3’:
getBD3 conn name
= do let qry_head_BD_Sidonie = ...
(bd_rows :: [Only Text]) <- query
conn qry_head_BD_Sidonie (Only (name :: String))
let noBDtxt = ...
....
|
54 | (bd_rows :: [Only Text]) <- query conn qry_head_BD_Sidonie (Only (name::String))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Failed, no modules loaded.
i do not understand the error complaining that i return an IO float,because i'm sure it's a float in noBDfp
if i put the same lines of code in the main it works !!! :
main :: IO ()
main =
do
conn <- connect defaultConnectInfo
{ connectHost = "moita",
connectUser = "mattei",
connectPassword = "sidonie2",
connectDatabase = "sidonie" }
let qry_head_BD_Sidonie = "select `N° BD` from Coordonnées where Nom = ?" :: Query
(bd_rows :: [Only Text]) <- query conn qry_head_BD_Sidonie (Only (name::String))
putStr "bd_rows ="
putStrLn $ show bd_rows
let noBDtxt = fromOnly (Prelude.head bd_rows) :: Text
let noBDstr = Text.unpack noBDtxt :: String
let noBDfp = read $ noBDstr :: Float
putStr "noBDfp ="
(putStrLn (show noBDfp))
close conn
it works i have output like this:
*Main> main
bd_rows =[Only {fromOnly = "-04.3982"}]
noBDtxt ="-04.3982"
noBDfp =-4.3982
noBDfp + 1 = -3.3982
i'm well getting a float in noBDfp , i even can add 1 to it :-) ( cool haskell...)
but i'm just wanting to that in the function getDB3 but it does not compile...
??????
Damien