Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: SQLite3 Row <-> Data models -- Is this a typical Haskell
pattern ? (Antoine Latter)
2. Re: SQLite3 Row <-> Data models -- Is this a typical Haskell
pattern ? (Stef T)
3. Re: SQLite3 Row <-> Data models -- Is this a typical Haskell
pattern ? (Antoine Latter)
4. Re: Lazy vs Strict ponderings... ([email protected])
5. Re: Lazy vs Strict ponderings... (Heinrich Apfelmus)
----------------------------------------------------------------------
Message: 1
Date: Sat, 19 Mar 2011 19:43:41 -0500
From: Antoine Latter <[email protected]>
Subject: Re: [Haskell-beginners] SQLite3 Row <-> Data models -- Is
this a typical Haskell pattern ?
To: Stef T <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
On Sat, Mar 19, 2011 at 7:21 PM, Stef T <[email protected]> wrote:
> Hey Everyone,
> ? ?Greetings, I am a total newb to haskell, so, I have a strange question
> (perhaps). Consider I have a data model which looks like this;
>
> data Brand = Brand { id :: Int,
> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? name :: String,
> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? created_at :: String,
> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? updated_at :: String
> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
>
> ? ?Now, I have a sqlite3 function such as
>
> checkout :: Int -> IO (Either String [[Row Value]])
> checkout a = do
> ? ? ? handle <- openConnection "/Users/stef/haskell/db/development.sqlite3"
> ? ? ? execStatement handle $ "SELECT * from brands where id = " ++ show a
>
> ? ?From this, I get a Row/Tuple. So far, so good, but, the question _I_ have
> is, how do you go about mapping a returned row to the data model ? Is this
> even a design pattern that is used in FP languages ? I admit, I have spent
> numerous years in the MVC world, so, perhaps this is simply "not done". It
> would seem to be a much nicer thing to then do ;
>
What problem are you running into trying to do this?
You would need to write functions to convert each column into the
format you want it for the Brand type, and then pass output of the
functions Brand data constructor. Depending on the format of the data
and the library you're using these functions might turn out to be
pretty simple.
It's considered good in Haskell to get data out of "weak" types into
specific types when you want them, as in from a String or from a 'Row
Value' - it makes manipulations of the values easier to read and you
get the compiler's help figuring things out.
Which SQL library are you using?
> ? ?name myBrandModel
>
> ? ?Thanks for reading this far, feel free to complain about my design :D
> ? ?Regards
> ? ?S.
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 2
Date: Sat, 19 Mar 2011 18:14:51 -0700
From: Stef T <[email protected]>
Subject: Re: [Haskell-beginners] SQLite3 Row <-> Data models -- Is
this a typical Haskell pattern ?
To: Antoine Latter <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Mar 19, 2011, at 5:43 PM, Antoine Latter wrote:
> On Sat, Mar 19, 2011 at 7:21 PM, Stef T <[email protected]> wrote:
>> Hey Everyone,
>> Greetings, I am a total newb to haskell, so, I have a strange question
>> (perhaps). Consider I have a data model which looks like this;
>>
>> data Brand = Brand { id :: Int,
>> name :: String,
>> created_at :: String,
>> updated_at :: String
>> }
>>
>> Now, I have a sqlite3 function such as
>>
>> checkout :: Int -> IO (Either String [[Row Value]])
>> checkout a = do
>> handle <- openConnection "/Users/stef/haskell/db/development.sqlite3"
>> execStatement handle $ "SELECT * from brands where id = " ++ show a
>>
>> From this, I get a Row/Tuple. So far, so good, but, the question _I_ have
>> is, how do you go about mapping a returned row to the data model ? Is this
>> even a design pattern that is used in FP languages ? I admit, I have spent
>> numerous years in the MVC world, so, perhaps this is simply "not done". It
>> would seem to be a much nicer thing to then do ;
>>
>
> What problem are you running into trying to do this?
>
> You would need to write functions to convert each column into the
> format you want it for the Brand type, and then pass output of the
> functions Brand data constructor. Depending on the format of the data
> and the library you're using these functions might turn out to be
> pretty simple.
>
I guess I am having a fundamental disconnect in 'how' to do it nicely (or
DRY-ly).
the format of the row is ;
Right [[[("id",Int 4239),("name",Text "Zoppini"),("created_at",Text "2011-02-02
20:51:44.706633+0000"),("updated_at",Text "2011-02-02 20:51:44.706633+0000")]]]
I had attempted to do something along the lines of ;
getBrand :: Int -> Brand
getBrand a = do
b <- checkout a
Brand { id = b id, name = b name }
but, that explodes (not least of which is the id being a reserved keyword from
prelude)
> It's considered good in Haskell to get data out of "weak" types into
> specific types when you want them, as in from a String or from a 'Row
> Value' - it makes manipulations of the values easier to read and you
> get the compiler's help figuring things out.
>
makes sense. It also helps -me- (and possibly future programmers/maintainers)
to understand what the heck is going on.
> Which SQL library are you using?
I am using the haskell sqlite (version 0.5.2)
Regards
S.
>
>> name myBrandModel
>>
>> Thanks for reading this far, feel free to complain about my design :D
>> Regards
>> S.
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
------------------------------
Message: 3
Date: Sat, 19 Mar 2011 20:43:54 -0500
From: Antoine Latter <[email protected]>
Subject: Re: [Haskell-beginners] SQLite3 Row <-> Data models -- Is
this a typical Haskell pattern ?
To: Stef T <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
On Sat, Mar 19, 2011 at 8:14 PM, Stef T <[email protected]> wrote:
> On Mar 19, 2011, at 5:43 PM, Antoine Latter wrote:
>
>> On Sat, Mar 19, 2011 at 7:21 PM, Stef T <[email protected]> wrote:
>>> Hey Everyone,
>>> ? ?Greetings, I am a total newb to haskell, so, I have a strange question
>>> (perhaps). Consider I have a data model which looks like this;
>>>
>>> data Brand = Brand { id :: Int,
>>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? name :: String,
>>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? created_at :: String,
>>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? updated_at :: String
>>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
>>>
>>> ? ?Now, I have a sqlite3 function such as
>>>
>>> checkout :: Int -> IO (Either String [[Row Value]])
>>> checkout a = do
>>> ? ? ? handle <- openConnection "/Users/stef/haskell/db/development.sqlite3"
>>> ? ? ? execStatement handle $ "SELECT * from brands where id = " ++ show a
>>>
>>> ? ?From this, I get a Row/Tuple. So far, so good, but, the question _I_
>>> have is, how do you go about mapping a returned row to the data model ? Is
>>> this even a design pattern that is used in FP languages ? I admit, I have
>>> spent numerous years in the MVC world, so, perhaps this is simply "not
>>> done". It would seem to be a much nicer thing to then do ;
>>>
>>
>> What problem are you running into trying to do this?
>>
>> You would need to write functions to convert each column into the
>> format you want it for the Brand type, and then pass output of the
>> functions Brand data constructor. Depending on the format of the data
>> and the library you're using these functions might turn out to be
>> pretty simple.
>>
>
> I guess I am having a fundamental disconnect in 'how' to do it nicely (or
> DRY-ly).
>
> the format of the row is ;
>
> Right [[[("id",Int 4239),("name",Text "Zoppini"),("created_at",Text
> "2011-02-02 20:51:44.706633+0000"),("updated_at",Text "2011-02-02
> 20:51:44.706633+0000")]]]
>
> I had attempted to do something along the lines of ;
>
> getBrand :: Int -> Brand
> getBrand a = do
> ? ? ? ?b <- checkout a
> ? ? ? ?Brand { id = b id, name = b name }
>
> but, that explodes (not least of which is the id being a reserved keyword
> from prelude)
>
First - 'id' is reserved, it's an ordinary function. But it can be
confusing to give things the same name as things in the Prelude.
Here you're running into a few problems - is there a tutorial you're
working with? There are some good ones over here:
http://www.haskell.org/haskellwiki/Tutorials
First is the type of your function:
> getBrand :: Int -> Brand
Since you're calling functions which do IO (to get data from the
database) in your function, your function needs to note that in its
type signature:
> getBrand :: Int -> IO Brand
On to the body of the function:
> getBrand a = do
> ? ? ? ?b <- checkout a
> ? ? ? ?Brand { id = b id, name = b name }
In the second line of the body, you have "b id" and "b name", this is
treating "b" has if it were a function, and calling it first with the
value "id" and then with the value "name" which is almost certainly
not what you want.
I don't know much about the library you're using, but you can start
with the types of the functions to figure out what to do with them.
Here, the value 'b' os of type "Either String [[Row Value]]", so you
can use a case statement to deconstruct the out-side 'Either' type:
> case b of
> Left errorStr -> [do something with the error message]
> Right rows -> [do something with the list of rows]
In this bit of code, the value 'rows' is of type '[[Row Value]]', so
then you have to make choices about what to do if the list is empty,
or if the list has more than row. Once you have a row you then need to
lookup its type and figure out how to take it apart and make sense of
the values.
So you've got more code to write :-)
I don't have much experience mixing SQL and Haskell, so there might be
tools to automate some of this.
Antoine
>
>> It's considered good in Haskell to get data out of "weak" types into
>> specific types when you want them, as in from a String or from a 'Row
>> Value' - it makes manipulations of the values easier to read and you
>> get the compiler's help figuring things out.
>>
>
> makes sense. It also helps -me- (and possibly future programmers/maintainers)
> to understand what the heck is going on.
>
>> Which SQL library are you using?
>
> I am using the haskell sqlite (version 0.5.2)
>
> Regards
> S.
>
>>
>>> ? ?name myBrandModel
>>>
>>> ? ?Thanks for reading this far, feel free to complain about my design :D
>>> ? ?Regards
>>> ? ?S.
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>
>
------------------------------
Message: 4
Date: Sun, 20 Mar 2011 08:09:04 +0000
From: [email protected]
Subject: Re: [Haskell-beginners] Lazy vs Strict ponderings...
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; DelSp="Yes";
format="flowed"
>> 2) Don't worry about memory leaks until they actually appear. If they
>> do, use a profiling tool to find out what's going on. Most likely, one
>> of the two things above happened in an interesting way.
At the risk of sounding stupid in Haskell mode, how would I be aware
of this other than obvious messages or seg-faults about memory getting
low ?
>
> It is likely that, if you develop a medium to large sized application for
> a customer, memory leaks will appear at the customers site. It will often
> be very difficult to reproduce the situation and find the leak.
That scares me enough to not want to use Haskell for serious
application development. With 26 years in the trade I have had too
many times when a fault cannot be reproduced and despite an 'obvious
fault in the source' being fixed and the problem never coming back,
without being able to reproduce and thus confirm that you have
eliminated the problem, you can't ever relax on a Saturday night! LOL
I hate the word 'random' when clients describe problems too!
> If you
> want to produce quality software, it is best to be sure what you are doing
> during the whole development process. There should be a set of guidelines,
> on how to prevent space leaks.
>
I guess "being sure..." comes with experience of the language, like
any other. I can 'think' in quite a few languages with complete
confidence, knowing that things will just work. I guess Haskell is a
bigger elephant sandwich! There is something beautiful about its
syntax and the fact that everything is (or can be) so precise and
concise, sometimes a little too concise for me to read. It *makes* me
want to be in that club just so I can have that level of understaning
about my job.
I learned BASIC and UCSD-PASCAL at age 11 and I have never stopped
being fascinated about *what* computing is all about. Sometimes I
think that a CPU *is* a perfectly enlightened consciousness always in
the *now*.
Time for my medication and music therapy.....
> See also "On the reliability of programs",
> http://www.cs.utexas.edu/users/EWD/transcriptions/EWD03xx/EWD303.html
Will do, thanks.
Sean
------------------------------
Message: 5
Date: Sun, 20 Mar 2011 10:37:23 +0100
From: Heinrich Apfelmus <[email protected]>
Subject: Re: [Haskell-beginners] Lazy vs Strict ponderings...
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
[email protected] wrote:
>>> 2) Don't worry about memory leaks until they actually appear. If they
>>> do, use a profiling tool to find out what's going on. Most likely, one
>>> of the two things above happened in an interesting way.
>
> At the risk of sounding stupid in Haskell mode, how would I be aware of
> this other than obvious messages or seg-faults about memory getting low ?
The usual sign is that GHC is requesting more RAM from the OS than it
should. For instance, if you expect your program to run in constant
space, but GHCs memory usage keeps growing by 100 MB every twenty
seconds, then you have a space leak. You can also run a profile
preemptively and look at the graph.
http://book.realworldhaskell.org/read/profiling-and-optimization.html
>> It is likely that, if you develop a medium to large sized application for
>> a customer, memory leaks will appear at the customers site. It will often
>> be very difficult to reproduce the situation and find the leak.
>
> That scares me enough to not want to use Haskell for serious application
> development. With 26 years in the trade I have had too many times when a
> fault cannot be reproduced and despite an 'obvious fault in the source'
> being fixed and the problem never coming back, without being able to
> reproduce and thus confirm that you have eliminated the problem, you
> can't ever relax on a Saturday night! LOL
>
> I hate the word 'random' when clients describe problems too!
That were the fears that I wanted to address with "don't worry". :D
You see, the thing is this: any kind of error can happen at the
customer's site, be it memory leaks, dangling pointers, division by zero
or all the nasty bugs you put in your code. The programming language you
choose influences both the kind of errors and the frequency of errors.
Choosing Haskell over an imperative language like C, Pascal or Java
drastically reduces logic bugs, and hence the total frequency of errors,
even though you might be slightly worse off on the memory leak side,
because laziness introduces a complication there. But that is a small
price to pay for the general reduction in errors.
>> See also "On the reliability of programs",
>> http://www.cs.utexas.edu/users/EWD/transcriptions/EWD03xx/EWD303.html
Dijkstra would be very happy about how close mathematics and Haskell are.
Regards,
Heinrich Apfelmus
--
http://apfelmus.nfshost.com
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 33, Issue 28
*****************************************