
On 6/16/08, Magicloud Magiclouds
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