[Haskell] Newbie quick questions

Sebastian Sylvan sebastian.sylvan at gmail.com
Wed Oct 5 15:46:40 EDT 2005


On 10/5/05, Sebastian Sylvan <sebastian.sylvan at gmail.com> wrote:
> 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).

I meant updateDB and click-event and there respectively. Sorry.

/S

--
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862


More information about the Haskell mailing list