[Haskell-cafe] How to do this in FP way?

Thomas M. DuBuisson thomas.dubuisson at gmail.com
Sun Jun 15 23:00:03 EDT 2008


Magicloud Magiclouds wrote:
>     Say I have something like this in C:
> static int old;
> int diff (int now) { /* this would be called once a second */
>   int ret = now - old;
>   old = now;
>   return ret;
> }
>     Because there is no "variable" in Haskell. So how to do this in a
> FP way?

So you have a global time count that is updated every second - I presume
for the rest of the process to use, avoiding syscalls due to expense.
Am I getting this right?

You can use mutable variables in Haskell, though some people will frown,
but it might fit your need.  See MVar, STM, or IO Refs.  At any rate, if
what you are doing needs to check the clock time then this code isn't
going to be pure.

Perhaps you just want something in particular to be triggered every
second then use control-timeout, event-list, or control-event.

Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20080615/cccd2049/attachment.bin


More information about the Haskell-Cafe mailing list