database, list result, extraction and conversion

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

database, list result, extraction and conversion

Damien Mattei
again un haskell beginner question ;-) ...
i have this: [Only {fromOnly = "-04.3982"}]
as result of a database query, i understand it's a list and i can get
the first element:

*Main> Data.List.head [Only {fromOnly = "-04.3982"}]
Only {fromOnly = "-04.3982"}
*Main> let s = Data.List.head [Only {fromOnly = "-04.3982"}]
*Main> s
Only {fromOnly = "-04.3982"}
*Main> show s
"Only {fromOnly = \"-04.3982\"}"

but how can i get the String "-04.3982" ? and after converting it to a Float
--
[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: database, list result, extraction and conversion

Francesco Ariis
On Thu, Dec 06, 2018 at 12:16:08PM +0100, Damien Mattei wrote:
> *Main> show s
> "Only {fromOnly = \"-04.3982\"}"
>
> but how can i get the String "-04.3982" ? and after converting it to a
> Float?

I suspect that "fromOnly" accessor will do!

http://hackage.haskell.org/package/mysql-simple-0.4.5/docs/Database-MySQL-Simple.html#t:Only


_______________________________________________
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: database, list result, extraction and conversion

Damien Mattei
if it was so easy!

GHCi, version 8.4.3: http://www.haskell.org/ghc/  :? for help
Prelude> fromOnly  [Only {fromOnly = "-04.3982"}]

<interactive>:1:12: error: Not in scope: data constructor ‘Only’


and in a program:

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

    putStrLn $ show bd_rows
    putStrLn $ show name

    let resLst = []

    let noBD = fromOnly bd_rows



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


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

UpdateSidonie.hs:93:25: error:
    • Couldn't match expected type ‘Only a’
                  with actual type ‘[Only Text]’
    • In the first argument of ‘fromOnly’, namely ‘bd_rows’
      In the expression: fromOnly bd_rows
      In an equation for ‘noBD’: noBD = fromOnly bd_rows
    • Relevant bindings include
        noBD :: a (bound at UpdateSidonie.hs:93:9)
   |
93 |     let noBD = fromOnly bd_rows
   |                         ^^^^^^^
Failed, no modules loaded.




Le 06/12/2018 14:07, Francesco Ariis a écrit :

> On Thu, Dec 06, 2018 at 12:16:08PM +0100, Damien Mattei wrote:
>> *Main> show s
>> "Only {fromOnly = \"-04.3982\"}"
>>
>> but how can i get the String "-04.3982" ? and after converting it to a
>> Float?
>
> I suspect that "fromOnly" accessor will do!
>
> http://hackage.haskell.org/package/mysql-simple-0.4.5/docs/Database-MySQL-Simple.html#t:Only
>
>
> _______________________________________________
> 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: database, list result, extraction and conversion

Francesco Ariis
On Thu, Dec 06, 2018 at 03:54:45PM +0100, Damien Mattei wrote:
> if it was so easy!

Damien, you must heed the compiler!

> UpdateSidonie.hs:93:25: error:
>     • Couldn't match expected type ‘Only a’
>                   with actual type ‘[Only Text]’
>     • In the first argument of ‘fromOnly’, namely ‘bd_rows’
>       In the expression: fromOnly bd_rows
>       In an equation for ‘noBD’: noBD = fromOnly bd_rows
>     • Relevant bindings include
>         noBD :: a (bound at UpdateSidonie.hs:93:9)
>    |
> 93 |     let noBD = fromOnly bd_rows
>    |                         ^^^^^^^
> Failed, no modules loaded.

So you applied `fromOnly` to `bd_rows`. fromOnly has type

    fromOnly :: Only a -> a

Now GHC is complaining:

      • I was expecting ‘Only a’
              but you gave me ‘[Only Text]’

So either call "head" on [Only Text] (unsafe!) or map over it.
See if it works and if not, fire again here
-F
_______________________________________________
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: database, list result, extraction and conversion

Bill Kunyiha
Thank you for reaching out to me.
Is this a remote position?
I live in Los Angeles and cannot relocate.

Thank You
Bill Kunyiha

On Thu, Dec 6, 2018 at 8:10 AM Francesco Ariis <[hidden email]> wrote:
On Thu, Dec 06, 2018 at 03:54:45PM +0100, Damien Mattei wrote:
> if it was so easy!

Damien, you must heed the compiler!

> UpdateSidonie.hs:93:25: error:
>     • Couldn't match expected type ‘Only a’
>                   with actual type ‘[Only Text]’
>     • In the first argument of ‘fromOnly’, namely ‘bd_rows’
>       In the expression: fromOnly bd_rows
>       In an equation for ‘noBD’: noBD = fromOnly bd_rows
>     • Relevant bindings include
>         noBD :: a (bound at UpdateSidonie.hs:93:9)
>    |
> 93 |     let noBD = fromOnly bd_rows
>    |                         ^^^^^^^
> Failed, no modules loaded.

So you applied `fromOnly` to `bd_rows`. fromOnly has type

    fromOnly :: Only a -> a

Now GHC is complaining:

      • I was expecting ‘Only a’
              but you gave me ‘[Only Text]’

So either call "head" on [Only Text] (unsafe!) or map over it.
See if it works and if not, fire again here
-F
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
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: database, list result, extraction and conversion

Damien Mattei
In reply to this post by Francesco Ariis
 let noBD = fromOnly (Prelude.head bd_rows)

give me a solution, but i'm still searching a solution with map has in a
list there could be more than one result...

D

Le 06/12/2018 17:10, Francesco Ariis a écrit :
> On Thu, Dec 06, 2018 at 03:54:45PM +0100, Damien Mattei wrote:
>> if it was so easy!
>
> Damien, you must heed the compiler!

heed?

>
>> UpdateSidonie.hs:93:25: error:
>>     • Couldn't match expected type ‘Only a’
>>                   with actual type ‘[Only Text]’
>>     • In the first argument of ‘fromOnly’, namely ‘bd_rows’
>>       In the expression: fromOnly bd_rows
>>       In an equation for ‘noBD’: noBD = fromOnly bd_rows
>>     • Relevant bindings include
>>         noBD :: a (bound at UpdateSidonie.hs:93:9)
>>    |
>> 93 |     let noBD = fromOnly bd_rows
>>    |                         ^^^^^^^
>> Failed, no modules loaded.
>
> So you applied `fromOnly` to `bd_rows`. fromOnly has type
>
>     fromOnly :: Only a -> a
>
> Now GHC is complaining:
>
>       • I was expecting ‘Only a’
>               but you gave me ‘[Only Text]’
>
> So either call "head" on [Only Text] (unsafe!) or map over it.
> See if it works and if not, fire again here
> -F
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>

_______________________________________________
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: database, list result, extraction and conversion

Francesco Ariis
On Fri, Dec 07, 2018 at 09:44:03AM +0100, Damien Mattei wrote:
>  let noBD = fromOnly (Prelude.head bd_rows)
>
> give me a solution, but i'm still searching a solution with map has in a
> list there could be more than one result...

So put this in your code

    let noBD = map _ bd_rows

`_` will tell the compiler "tell me which type goes there". Then you
can choose the appropriate function!
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: database, list result, extraction and conversion

Damien Mattei
yes thanks you
i had swapped the arguments of map in my code, i lack sleeping those days
this solution worked :
 let resLst = Prelude.map fromOnly bd_rows

but not this:

let noBD2 = Prelude.map _ bd_rows

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

UpdateSidonie.hs:94:29: error:
    • Found hole: _ :: Only Text -> b
      Where: ‘b’ is a rigid type variable bound by
               the inferred type of noBD2 :: [b]
               at UpdateSidonie.hs:94:9-37
    • In the first argument of ‘Prelude.map’, namely ‘_’
      In the expression: Prelude.map _ bd_rows
      In an equation for ‘noBD2’: noBD2 = Prelude.map _ bd_rows
    • Relevant bindings include
        noBD2 :: [b] (bound at UpdateSidonie.hs:94:9)
        noBD :: Text (bound at UpdateSidonie.hs:93:9)
        resLst :: [Text] (bound at UpdateSidonie.hs:91:9)
        bd_rows :: [Only Text] (bound at UpdateSidonie.hs:86:5)
        qry_head :: Query (bound at UpdateSidonie.hs:79:9)
        qry :: [Char] (bound at UpdateSidonie.hs:77:9)
        (Some bindings suppressed; use -fmax-relevant-binds=N or
-fno-max-relevant-binds)
      Valid substitutions include
        undefined :: forall (a :: TYPE r).
                     GHC.Stack.Types.HasCallStack =>
                     a
          (imported from ‘Prelude’ at UpdateSidonie.hs:1:1
           (and originally defined in ‘GHC.Err’))
   |
94 |     let noBD2 = Prelude.map _ bd_rows
   |                             ^
Failed, no modules loaded.


it's annoying ,since i add 'import' ,i must Prelude.* basic functions:
import Database.MySQL.Simple
import Database.MySQL.Simple.QueryResults
import Database.MySQL.Simple.Result
import Control.Monad
import Data.Text

i must use Prelude.map, Prelude.head

is there a solution to this problem?



Le 07/12/2018 09:59, Francesco Ariis a écrit :

> On Fri, Dec 07, 2018 at 09:44:03AM +0100, Damien Mattei wrote:
>>  let noBD = fromOnly (Prelude.head bd_rows)
>>
>> give me a solution, but i'm still searching a solution with map has in a
>> list there could be more than one result...
>
> So put this in your code
>
>     let noBD = map _ bd_rows
>
> `_` will tell the compiler "tell me which type goes there". Then you
> can choose the appropriate function!
> _______________________________________________
> 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