monad and variable result

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

monad and variable result

Damien Mattei
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?

note the goal is to do the same thing of my main function that works :

main :: IO ()
--main :: Int
main =

  do
    conn <- connect defaultConnectInfo
      { connectHost = "moita",
        connectUser = "mattei",
        connectPassword = "******",
        connectDatabase = "sidonie" }


-- we get all the Double Stars that have angular distance superior to a
threshold of 1 second = 0.000278 degree

    rows <- query_ conn "SELECT Nom,distance FROM AngularDistance WHERE
distance > 0.000278"

    forM_ rows $ \(name,distance) ->
      putStrLn $  Text.unpack name ++ " " ++ show (distance :: Double)


-- we will get the Durchmusterung Number BD from Sidonie and WDS and
compare them for a given name
-- Warning: there could be multiple result in WDS for a given entry name
(due to components)

-- first we get the N°BD from sidonie

    let name = "A    20"
--    let qry = "select `N° BD` from Coordonnées where Nom = " ++ name

    let qry_head = "select `N° BD` from sidonie.Coordonnées where Nom =
?" :: Query

--    bd_rows <- query_ conn "select `N° BD` from sidonie.Coordonnées
where Nom = 'A    20'"

    bd_rows <- query conn qry_head (Only (name::String))

    putStrLn $ show bd_rows
    putStrLn $ show name

    let resLst = Prelude.map fromOnly bd_rows

    let noBDtxt = fromOnly (Prelude.head bd_rows) :: Text
--    let noBD2 =  _ (Prelude.head bd_rows)

    putStrLn $ show resLst

    putStrLn $ show noBDtxt

    forM_ bd_rows $ \(Only a) ->
      putStrLn $  Text.unpack a

    let noBDstr = Text.unpack noBDtxt :: String
    let noBDfp = read $ noBDstr :: Float

    putStr "noBDfp ="
    (putStrLn (show noBDfp))

    close conn

    print "Exit."


for now i have errors in the function:

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

UpdateSidonie.hs:47:28: error:
    • Ambiguous type variable ‘r0’ arising from a use of ‘query’
      prevents the constraint ‘(QueryResults r0)’ from being solved.
      Relevant bindings include
        bd_rows :: IO [r0] (bound at UpdateSidonie.hs:46:9)
      Probable fix: use a type annotation to specify what ‘r0’ should be.
      These potential instances exist:
        instance Result a => QueryResults (Only a)
          -- Defined in ‘Database.MySQL.Simple.QueryResults’
        instance (Result a, Result b) => QueryResults (a, b)
          -- Defined in ‘Database.MySQL.Simple.QueryResults’
        instance (Result a, Result b, Result c) => QueryResults (a, b, c)
          -- Defined in ‘Database.MySQL.Simple.QueryResults’
        ...plus 21 others
        (use -fprint-potential-instances to see them all)
    • In a stmt of a 'do' block:
        local_bd_rows <- query conn qry_head (Only (name :: String))
      In the expression:
        do local_bd_rows <- query conn qry_head (Only (name :: String))
           return local_bd_rows
      In an equation for ‘bd_rows’:
          bd_rows
            = do local_bd_rows <- query conn qry_head (Only (name ::
String))
                 return local_bd_rows
   |
47 |           local_bd_rows <- query conn qry_head (Only (name::String))
   |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Failed, no modules loaded.



--
[hidden email], [hidden email], UNS / OCA / CNRS
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: monad and variable result

Francesco Ariis
Hy Damien,

On Mon, Dec 10, 2018 at 11:32:23AM +0100, Damien Mattei wrote:
> i want  the variable local_bd_rows accessible in the 'where' clause

does `query` return IO ()? If so, no you can't*. Once the result
is inside IO, it stays in IO. Move what you need inside out of where
(and inside a `do`) and get ready to change the signature of
`getBD` to

    getBD :: Connection -> String -> IO Float

-F

* (you can with unsafeSomething functions, but it is really really
advisable not to do that).
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: (SPAM 3)Re: monad and variable result

Damien Mattei
query returns a list i suppose because i can wirte this in my main code:
 bd_rows <- query conn qry_head (Only (name::String))

    putStrLn $ show bd_rows
    putStrLn $ show name

    let resLst = Prelude.map fromOnly bd_rows

i can o a map on bd_rows

i just want to convert this code in Main:

let qry_head = "select `N° BD` from sidonie.Coordonnées where Nom = ?"
:: Query

--    bd_rows <- query_ conn "select `N° BD` from sidonie.Coordonnées
where Nom = 'A    20'"

    bd_rows <- query conn qry_head (Only (name::String))

    putStrLn $ show bd_rows
    putStrLn $ show name

    let resLst = Prelude.map fromOnly bd_rows

    let noBDtxt = fromOnly (Prelude.head bd_rows) :: Text
--    let noBD2 =  _ (Prelude.head bd_rows)

    putStrLn $ show resLst

    putStrLn $ show noBDtxt

    forM_ bd_rows $ \(Only a) ->
      putStrLn $  Text.unpack a

    let noBDstr = Text.unpack noBDtxt :: String
    let noBDfp = read $ noBDstr :: Float

where i succeed in putting the Float extracted and converted from the
result of query in a variable of type Float,

i want to have this in  function getBD that take as arguments the
Connection and the String name ,i use name in the SQL query and get back
the noBD in text first ,string and finally float...

this should be done,i assume it is possible in haskell but i'm a
beginner....

i have problem finding good doc ,understanding Monads ans '<-' use and 'do'

but i assume the solution is perhaps trivial...

Le 10/12/2018 12:19, Francesco Ariis a écrit :

> Hy Damien,
>
> On Mon, Dec 10, 2018 at 11:32:23AM +0100, Damien Mattei wrote:
>> i want  the variable local_bd_rows accessible in the 'where' clause
>
> does `query` return IO ()? If so, no you can't*. Once the result
> is inside IO, it stays in IO. Move what you need inside out of where
> (and inside a `do`) and get ready to change the signature of
> `getBD` to
>
>     getBD :: Connection -> String -> IO Float
>
> -F
>
> * (you can with unsafeSomething functions, but it is really really
> advisable not to do that).
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>

--
[hidden email], [hidden email], UNS / OCA / CNRS
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: (SPAM 3)Re: monad and variable result

Francesco Ariis
On Mon, Dec 10, 2018 at 03:46:46PM +0100, Damien Mattei wrote:
> query returns a list i suppose because i can wirte this in my main code:
>  bd_rows <- query conn qry_head (Only (name::String))

`query` does not return a `[r]`, rather an `IO [r]`. The difference
is very important; if you don't get it you will always have troubles
with Haskell. This will for sure help you

    https://www.seas.upenn.edu/~cis194/fall16/lectures/06-io-and-monads.html
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: (SPAM 3)Re: (SPAM 3)Re: monad and variable result

Damien Mattei
thank you, i understand the difference, but can not see a solution,
seems i need main :: IO
so keep code in main, which is not really a problem, but i wanted to
structure it  more... i will keep searching a solution, understanding
more haskell

Le 10/12/2018 16:06, Francesco Ariis a écrit :

> On Mon, Dec 10, 2018 at 03:46:46PM +0100, Damien Mattei wrote:
>> query returns a list i suppose because i can wirte this in my main code:
>>  bd_rows <- query conn qry_head (Only (name::String))
>
> `query` does not return a `[r]`, rather an `IO [r]`. The difference
> is very important; if you don't get it you will always have troubles
> with Haskell. This will for sure help you
>
>     https://www.seas.upenn.edu/~cis194/fall16/lectures/06-io-and-monads.html
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>

--
[hidden email], [hidden email], UNS / OCA / CNRS
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners