[Haskell-cafe] Fwd: [Haskell-beginners] monad and variable result
Damien Mattei
mattei at oca.eu
Mon Dec 10 17:06:30 UTC 2018
for now i'm here:
getBD :: Connection -> String -> Float
getBD conn name = noBDfp
where qry_head = "select `N° BD` from sidonie.Coordonnées where Nom =
?" :: Query
-- bd_rows =
-- do
-- local_bd_rows <- query conn qry_head (Only (name::String))
-- return local_bd_rows
bd_rows :: IO [Only Text]
bd_rows = query conn qry_head (Only (name::String))
noBDtxt :: [Text]
noBDtxt = fromOnly (Prelude.head bd_rows)
noBDstr :: String
noBDstr = Text.unpack noBDtxt :: String
noBDfp = read $ noBDstr :: Float
but it fails due to the IO :
Prelude> :load UpdateSidonie
[1 of 1] Compiling Main ( UpdateSidonie.hs, interpreted )
UpdateSidonie.hs:53:42: error:
• Couldn't match expected type ‘[Only [Text]]’
with actual type ‘IO [Only Text]’
• In the first argument of ‘Prelude.head’, namely ‘bd_rows’
In the first argument of ‘fromOnly’, namely
‘(Prelude.head bd_rows)’
In the expression: fromOnly (Prelude.head bd_rows)
|
53 | noBDtxt = fromOnly (Prelude.head bd_rows)
| ^^^^^^^
UpdateSidonie.hs:55:31: error:
• Couldn't match expected type ‘Text’ with actual type ‘[Text]’
• In the first argument of ‘unpack’, namely ‘noBDtxt’
In the expression: unpack noBDtxt :: String
In an equation for ‘noBDstr’: noBDstr = unpack noBDtxt :: String
|
55 | noBDstr = Text.unpack noBDtxt :: String
| ^^^^^^^
Failed, no modules loaded.
Le 10/12/2018 17:19, Seph Shewell Brockway a écrit :
> Hi Damien,
>
> On Mon, Dec 10, 2018 at 03:18:48PM +0100, Damien Mattei wrote:
>> have some code that works but want to put it in a simple function
>> without sucess:
>>
>> getBD :: Connection -> String -> Float
>> getBD conn name = noBDfp
>> where qry_head = "select `N° BD` from sidonie.Coordonnées where Nom =
>> ?" :: Query
>> bd_rows = do
>> local_bd_rows <- query conn qry_head (Only (name::String))
>> return local_bd_rows
>>
>>
>> i want the variable local_bd_rows accessible in the 'where' clause
>>
>> how can i do that?
>
> You don’t seem to be using the function bd_rows anywhere in the main
> body of the definition. You would need to do something like
>
> getBD :: Connection -> String -> IO Float
> getBD = do rows <- bd_rows
> {- code that does something with the returned data -}
>
> Note the change to the type signature—querying the database is an IO
> action, and therefore takes place in the IO monad.
>
> Incidently, your use of do notation in the definition of bd_rows is
> unnecessary:
>
> do x <- doSomething
> return x
>
> is actually syntactic sugar for
>
> doSomething >>= \x -> return x
>
> which the monad laws state is equivalent to just doSomething. This is a
> common misapprehension among Haskell novices: the do notation is just a
> syntactic convenience, and it is perfectly possible to write monadic
> functions, including in the IO monad, without it.
>
> Hope at least some of this helps.
>
> Seph
>
--
Damien.Mattei at unice.fr, Damien.Mattei at oca.eu, UNS / OCA / CNRS
More information about the Haskell-Cafe
mailing list