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

Sebastian Sylvan sebastian.sylvan at gmail.com
Mon Jun 16 15:14:48 EDT 2008


On 6/16/08, Magicloud Magiclouds <magicloud.magiclouds at gmail.com> wrote:
>
> Hello,
>     I am getting familiar with FP now, and I have a "program design" kind
> of question.
>     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?

The short answer is that your question amounts to "How do I do imperative
programming in an FP way", to which the answer is "you really should try to
avoid it".

Longer answer:

I think you'll be bette served if you describe your problem on a much higher
level than this. Chances are that if you write your program in an FP way,
you wouldn't need a function like your diff.

That said, Haskell do have variables (in this case an IORef would do what
you want), but again, you probably don't want that, so if you post what
problem you're trying to solve using "diff", then it will be easier to help
you design it in an FP way. Doing things in an FP way tend to impact your
program a lot more than just some minor changes to the functions at the
"bottom", it will change the whole design.

--
Sebastian Sylvan
+44(0)7857-300802
UIN: 44640862
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20080616/79698302/attachment.htm


More information about the Haskell-Cafe mailing list