[Haskell-cafe] Fwd: [Haskell-beginners] monad and variable result
Damien Mattei
mattei at oca.eu
Tue Dec 11 15:54:24 UTC 2018
Thank you Seph, with your help and reading some pages i can get to this:
getBD :: Connection -> String -> IO Float
getBD conn name = noBDfp
where qry_head = "select `N° BD` from sidonie.Coordonnées where Nom =
?" :: Query
bd_rows :: IO [Only Text]
bd_rows = query conn qry_head (Only (name::String))
-- noBDtxt :: [Text]
-- noBDtxt = fromOnly (Prelude.head bd_rows)
-- noBDtxt :: IO [Text]
noBDtxt :: IO Text
noBDtxt = fmap (fromOnly . Prelude.head) bd_rows
-- noBDstr :: String
-- noBDstr = Text.unpack noBDtxt
noBDstr :: IO String
noBDstr = fmap Text.unpack noBDtxt
-- noBDfp = read $ noBDstr :: Float
noBDfp = fmap read noBDstr :: IO Float
which compile
but the question rest entire :how can i get the loat number from all this?
i have a Main that looks like this:
main :: IO ()
--main :: Int
main =
do
conn <- connect defaultConnectInfo
{ connectHost = "moita",
connectUser = "mattei",
connectPassword = "sidonie2",
connectDatabase = "sidonie" }
-- first we get the N°BD from sidonie
let name = "A 20"
let noBD_IO = getBD conn name
-- putStrLn $ show $ read $ Text.unpack noBD_IO
close conn
print "Exit."
how can i get the float number from noBD_IO ?
Regards,
Damien
Le 10/12/2018 19:56, Seph Shewell Brockway a écrit :
> On Mon, Dec 10, 2018 at 06:06:30PM +0100, Damien Mattei wrote:
>> 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
>
> Okay, I think I understand how your code is structured now. The point to
> recognize is that bd_rows has type IO [Only Text], which is not the same
> type as [Only Text]. Prelude.head has the type [a] -> a, and so it can’t
> be used on bd_rows as-is. Fortunately, being a monad, IO has an instance
> of Functor for free, and we can go from
>
> head :: [Only Text] -> Only Text
>
> to
>
> fmap head :: IO [Only Text] -> IO (Only Text)
>
> which takes an IO operation returning [Only Text] and applies head to
> its result, giving an IO operation returning an Only Text.
>
> Can you see how the IO monad follows you through everything that uses
> its result? If you rewrite noBDtxt as
>
> noBDtxt :: IO [Text]
> noBDtxt = fmap (fromOnly . Prelude.head) bd_rows
>
> then noBDstr has to be rewritten in a similar way, and so on through to
> the final result, giving the main function a type of
> Connection -> String -> IO Float.
>
> A lot of the functions in your where clause can be amalgamated, for
> example by combining noBDtxt and noBDstr as
>
> noBDstr = fmap (Text.unpack . fromOnly . Prelude.head)
>
> Similarly, getBD takes the result of noBDfp and returns it unaltered, so
> why not just write
>
> getBD = fmap read noBDstr
>
> ?
>
> Let me know if you would like me to explain anything in this message in more
> detail.
>
> Regards,
>
> Seph
>
--
Damien.Mattei at unice.fr, Damien.Mattei at oca.eu, UNS / OCA / CNRS
More information about the Haskell-Cafe
mailing list