[Haskell] Newbie quick questions

Sebastian Sylvan sebastian.sylvan at gmail.com
Wed Oct 5 15:44:34 EDT 2005

On 10/4/05, Mike Crowe <mike at mikeandkellycrowe.com> wrote:
>  Thanks, all, especially Cale for the detail.
>  This may be unfair to ask, but is anybody willing to give an example?
> There are great examples for writing factorials.  However, that's not really
> useful.  I'm looking for a real-world example of using the language.
> Specifically, the first page of About Haskell states:
> WOW! I basically wrote this without testing just thinking about my program
> in terms of transformations between types. What I'm still missing is how to
> use this idea of functional programming to tie all this together.  Let's
> say, for example, I want to write a data input system for a database.
> Consider these two examples:
>  I think I understand how to take the following example (and others in that
> library) and expand to a complete UI for the data input:
> http://cvs.sourceforge.net/viewcvs.py/wxhaskell/wxhaskell/samples/wx/Grid.hs?rev=1.6&view=auto
>  I also looked over the examples in
> http://htoolkit.sourceforge.net/ for writing to a SQL
> database.  So I can see how to save the data.  The following example I get
> for inserting:
>  insertRecords :: Connection -> IO ()
>  insertRecords c = do
>      execute c "insert into Test(id,name) values (1,'Test1')"
>  How, though, would I start?  If I did this in an imperative language, I
> might do it like (in Python):
>  def main:
>      if gridCtrl.Show():                        # returns True if user exits
> pressing Save
>          data = gridCtrl.getData()
>          dataBase.insertRecords(data)
>  In Haskell, how would you start this at the top?  How would you define a
> relationship between two modules?
>  If this is more detailed than I should ask in this list, please LMK.
>  Thanks!
>  Mike

In general you write a small "shell" of IO code as your base
application. This IO code then calls the rest of the
(non-IO-)functions and presents the result in some way.

As you can see in the source code you linked you can attatch IO
actions to events. E.g.
set g [on gridEvent := onGrid]

So to, for example, trigger a database update when the user presses a
button, you would attatch the database-update action to the on click
event for that button.

You could also use partial application to pass along extra data that
this function may need

set but [on click := updateDB dbConnection]

where dbConnection is some value representing a database connection

and then in the function defintion:

updateDB dbConn  =  do ...

As you can see onGrid takes two parameters (everything it needs to do
what you want it to do) but when you attatch it to the gridEvent you
only pass it the first one (the event itself passes the second one).
You would most likely want to pass other data to updateDB, such as the
data that should be inserted into the table etc. You could e.g. pass
the gridcontrol to updateDB and let the updateDB function extract the
data from it and insert it into the database.

So the main IO code is very imperative in look and feel, except that
all data flow is explicit (and perhaps more importantly, that actions
are first class citizens). So you could lay out your haskell IO code
in much the same way as you would in an imperative language.

As an aside. if you're going to use databases, consider using
HaskellDB (an SQL "unwrapper"), which allows you type-safe database
queries (pretty cool!).


Sebastian Sylvan
UIN: 44640862

More information about the Haskell mailing list