=?gb2312?B?tPC4tDogW0hhc2tlbGwtY2FmZV0gUmU6IEhvdyB0byBkbyB0aGlzIA==?= =?gb2312?B?aW4gRlAgd2F5Pw==?=

Magicloud magicloud.magiclouds at gmail.com
Mon Jun 16 23:43:04 EDT 2008


I think if I do not use a state, and the function would be called for =
many
times, it would waste memory, if using something like loop, right?

-----=D3=CA=BC=FE=D4=AD=BC=FE-----
=B7=A2=BC=FE=C8=CB: haskell-cafe-bounces at haskell.org
[mailto:haskell-cafe-bounces at haskell.org] =B4=FA=B1=ED Achim Schneider
=B7=A2=CB=CD=CA=B1=BC=E4: 2008=C4=EA6=D4=C216=C8=D5 12:01
=CA=D5=BC=FE=C8=CB: haskell-cafe at haskell.org
=D6=F7=CC=E2: [Haskell-cafe] Re: How to do this in FP way?

"Magicloud Magiclouds" <magicloud.magiclouds at gmail.com> wrote:

> static int old;
> int diff (int now) { /* this would be called once a second */
>   int ret =3D now - old;
>   old =3D now;
>   return ret;
> }
>
You do it with variables, of course. This is out of some GLUT code,
using IORef's:

idle :: State -> IdleCallback
idle state =3D do
    t0 <- get $ t state
    t1 <- get elapsedTime
    t state $=3D t1
    let td =3D fromIntegral t1 - fromIntegral t0
    fps state $=3D 1/td * 1000

    angle' state $~! (+2)
   =20
    (bpx, bpy) <- get $ ballPos state
    (bvx, bvy) <- get $ ballVel state
   =20
    ballPos state $=3D (bpx + bvx*td, bpy + bvy*td)  =20
    postRedisplay Nothing

One half of all Haskell coders will tell you that mutable state isn't a
good starting point to learn Haskell, the other half will tell you the
same because they want to be cool kids, too.=20

--=20
(c) this sig last receiving data processing entity. Inspect headers for
past copyright information. All rights reserved. Unauthorised copying,
hiring, renting, public performance and/or broadcasting of this
signature prohibited.=20

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe at haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list