[Haskell-cafe] (SPAM 3)Re: fmap use

Damien Mattei mattei at oca.eu
Fri Dec 14 14:36:03 UTC 2018


ok Elis i tried again,

first ,as i am familiar with λ expressions i can use both do syntactic
sugar or the other form with λ.

so itried you code,surpringly it compiles but when i launch it the
function can not print somethin on screen, i assume it is because
function are pure in haskell, nor with putStrln or Debug.trace:

getBD_WDS :: Connection -> String -> IO Float
getBD_WDS conn name = do
  let qry_head = "select DNUM from WDS where DISC = ?" :: Query

  bd_rows <- query conn qry_head (Only name)

  let lg = Prelude.length bd_rows

--  putStrLn (if lg > 1 then "some" else "one or zero")
  let tst = trace (if lg > 1 then "some" else "one or zero") (if lg > 1
then "some" else "one or zero")

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

  --        noBDstr :: String
  let noBDstr = Text.unpack noBDtxt

--        noBDfp :: Float
  let noBDfp = read noBDstr

  return noBDfp

in fact due to lazy evaluation i'm not sure it compiles all the code
because a more simple example give compilation errors:

getLstBD_WDS :: Connection -> String -> IO Int
getLstBD_WDS conn name = do
  let qry_head = "select DNUM from WDS where DISC = ?" :: Query

  bd_rows <- query conn qry_head (Only name)

  let lg = Prelude.length bd_rows

  return lg

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

UpdateSidonie.hs:123:14: error:
    • Ambiguous type variable ‘r0’ arising from a use of ‘query’
      prevents the constraint ‘(QueryResults r0)’ from being solved.
      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:
        bd_rows <- query conn qry_head (Only name)
      In the expression:
        do let qry_head = ...
           bd_rows <- query conn qry_head (Only name)
           let lg = Prelude.length bd_rows
           return lg
      In an equation for ‘getLstBD_WDS’:
          getLstBD_WDS conn name
            = do let qry_head = ...
                 bd_rows <- query conn qry_head (Only name)
                 let lg = ...
                 ....
    |
123 |   bd_rows <- query conn qry_head (Only name)
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Failed, no modules loaded.


Le 14/12/2018 12:45, Tom Ellis a écrit :
> On Fri, Dec 14, 2018 at 11:22:38AM +0000, Tom Ellis wrote:
>> On Fri, Dec 14, 2018 at 11:49:45AM +0100, Damien Mattei wrote:
>>> another question with Haskell, again with my database but that is again
>>> related to Monads:
>>
>> Damien, it's very hard to read your posts because they consist of snippets
>> of code interspersed with commentary.  You'll probably get much better
>> responses if you post single, very small, well-formatted examples of sample
>> code.
>>
>> Anyway, the ultimate answer to your query is "use do-notation".  Your
>> getBS_WDS would be clearer written something like:
> 
> And ultimately I think you wanted to do something with lg and tst:
> 
> ```
> getBD_WDS :: Connection -> String -> IO Float
> getBD_WDS conn name = do
>     let qry_head = "select DNUM from WDS where DISC = ?" :: Query
> 
>     bd_rows <- query conn qry_head (Only name)
> 
>     let lg = Prelude.length bd_rows
> 
>     putStrLn (if lg > 1 then "some" else "one or zero")
> 
>     let noBDtxt :: Text
>         noBDtxt = fromOnly (Prelude.head bd_rows)
> 	
>         noBDstr :: String
>         noBDstr = Text.unpack noBDtxt
> 
>         noBDfp :: Float
>         noBDfp = read noBDstr
> 
>     return noBDfp
> ```
> 
> _______________________________________________
> 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.
> 

-- 
Damien.Mattei at unice.fr, Damien.Mattei at oca.eu, UNS / OCA / CNRS


More information about the Haskell-Cafe mailing list