[Haskell-cafe] Callback functions?
Stefan O'Rear
stefanor at cox.net
Mon Jul 2 18:34:03 EDT 2007
On Tue, Jul 03, 2007 at 12:26:47AM +0200, Hugh Perkins wrote:
> This sounds something like using a continuation function.
>
> In imperative languages, we might have something like:
>
> class MyDisplayForm
> {
> void ButtonGo_Press()
> {
> Processor.Go( new CallbackDelegate( this.SetStatus ) );
> }
>
> public void SetStatus( string message )
> {
> StatusLbl.Text = message;
> Application.DoEvents();
> }
> }
>
>
> So: we call Processor.Go, passing in a pointer to the function SetStatus (a
> "delegate" in C# terminology, an "interface" in Java, a pointer in C, ...).
>
> The Processor.Go function is going to update the status bar in MyDisplayForm
> as it goes along with useful messages "Processing blah.txt...", etc.
>
> It does that by calling the passed in "callback" function, with the
> appropriate status message as a parameter.
>
> This is also implicit to the Observer pattern, which may or may not be
> useful in FP (?)
>
>
> Anyway, so the question is: how do we write callback functions in
> FP/Haskell? Can someone provide a simple, but functional example?
mapM_ print [1,2,3]
But usually we don't do that, because returning the list directly is
simpler and because of laziness, almost as fast.
Stefan
More information about the Haskell-Cafe
mailing list