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

2008-06-16 Thread Thomas Davie


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.


And the one left over will point out that he asked how to do this the  
FP way, not the imperative way?


If it was me btw, I'd take a stab at the problem being that each time  
we do something a time gets updated and we want to know how much time  
has passed since we last did something.


I'd approach this by generating a lazy list of times at which we  
started doing something, and then generating a lazy list of time  
differences.


Bob
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


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

2008-06-16 Thread Achim Schneider
Thomas Davie [EMAIL PROTECTED] wrote:

 
  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.
 
 And the one left over will point out that he asked how to do this
 the FP way, not the imperative way?
 
There's no difference, as you can't do time-accounting non-strict and
still expect it to give meaningful results: I'm merely trying to be
helpful. None of the other solutions allow for the IO Monad. 

-- 
(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. 

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


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

2008-06-16 Thread Thomas Davie


On 16 Jun 2008, at 18:28, Achim Schneider wrote:


Thomas Davie [EMAIL PROTECTED] wrote:



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.


And the one left over will point out that he asked how to do this
the FP way, not the imperative way?


There's no difference, as you can't do time-accounting non-strict and
still expect it to give meaningful results: I'm merely trying to be
helpful. None of the other solutions allow for the IO Monad.


Firstly, I'd phrase that differently -- the IO Monad doesn't allow for  
the other solutions -- the other solutions are the truly functional  
ones.  Secondly, I'm curious as to why you think that the two are  
incompatible, are you saying that for any meaningful kind of  
computation we need to resort to IORefs?  I'd strongly contest that  
idea.


Bob
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


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

2008-06-16 Thread Achim Schneider
Thomas Davie [EMAIL PROTECTED] wrote:

 
 On 16 Jun 2008, at 18:28, Achim Schneider wrote:
 
  Thomas Davie [EMAIL PROTECTED] wrote:
 
 
  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.
 
  And the one left over will point out that he asked how to do this
  the FP way, not the imperative way?
 
  There's no difference, as you can't do time-accounting non-strict
  and still expect it to give meaningful results: I'm merely trying
  to be helpful. None of the other solutions allow for the IO Monad.
 
 Firstly, I'd phrase that differently -- the IO Monad doesn't allow
 for the other solutions -- the other solutions are the truly
 functional ones.  Secondly, I'm curious as to why you think that the
 two are incompatible, are you saying that for any meaningful kind of  
 computation we need to resort to IORefs?  I'd strongly contest that  
 idea.
 
We have to resort to IO actions to get the time, and to IORefs because
we need to chain up different calls to getCurrentTime using the IO
Monad. The rest of the program can work with whatever you like best.


-- 
(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. 

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


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

2008-06-16 Thread Henning Thielemann


On Mon, 16 Jun 2008, Achim Schneider wrote:


We have to resort to IO actions to get the time, and to IORefs because
we need to chain up different calls to getCurrentTime using the IO
Monad. The rest of the program can work with whatever you like best.


Isn't  (StateT s IO a)  the cleaner alternative to IORef?
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


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

2008-06-16 Thread Achim Schneider
Henning Thielemann [EMAIL PROTECTED] wrote:

 
 On Mon, 16 Jun 2008, Achim Schneider wrote:
 
  We have to resort to IO actions to get the time, and to IORefs
  because we need to chain up different calls to getCurrentTime using
  the IO Monad. The rest of the program can work with whatever you
  like best.
 
 Isn't  (StateT s IO a)  the cleaner alternative to IORef?

Yes. But then using one IORef in one place won't make your program
harder to understand, using Monad transformers quite certainly will.
Admittedly, I'm becoming dodgy.

-- 
(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. 

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


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

2008-06-16 Thread Thomas Davie


On 16 Jun 2008, at 19:24, Achim Schneider wrote:


Thomas Davie [EMAIL PROTECTED] wrote:



On 16 Jun 2008, at 18:28, Achim Schneider wrote:


Thomas Davie [EMAIL PROTECTED] wrote:



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.


And the one left over will point out that he asked how to do this
the FP way, not the imperative way?


There's no difference, as you can't do time-accounting non-strict
and still expect it to give meaningful results: I'm merely trying
to be helpful. None of the other solutions allow for the IO Monad.


Firstly, I'd phrase that differently -- the IO Monad doesn't allow
for the other solutions -- the other solutions are the truly
functional ones.  Secondly, I'm curious as to why you think that the
two are incompatible, are you saying that for any meaningful kind of
computation we need to resort to IORefs?  I'd strongly contest that
idea.


We have to resort to IO actions to get the time, and to IORefs because
we need to chain up different calls to getCurrentTime using the IO
Monad. The rest of the program can work with whatever you like best.


And in what way is this incompatible with either FRP as pierre  
suggested, or with generating an infinite list of times at which we  
call the function, and scanning it to find the differences?


Bob

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


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

2008-06-16 Thread Achim Schneider
Thomas Davie [EMAIL PROTECTED] wrote:

 
 On 16 Jun 2008, at 19:24, Achim Schneider wrote:
 
  Thomas Davie [EMAIL PROTECTED] wrote:
 
 
  On 16 Jun 2008, at 18:28, Achim Schneider wrote:
 
  Thomas Davie [EMAIL PROTECTED] wrote:
 
 
  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.
 
  And the one left over will point out that he asked how to do this
  the FP way, not the imperative way?
 
  There's no difference, as you can't do time-accounting non-strict
  and still expect it to give meaningful results: I'm merely trying
  to be helpful. None of the other solutions allow for the IO Monad.
 
  Firstly, I'd phrase that differently -- the IO Monad doesn't allow
  for the other solutions -- the other solutions are the truly
  functional ones.  Secondly, I'm curious as to why you think that
  the two are incompatible, are you saying that for any meaningful
  kind of computation we need to resort to IORefs?  I'd strongly
  contest that idea.
 
  We have to resort to IO actions to get the time, and to IORefs
  because we need to chain up different calls to getCurrentTime using
  the IO Monad. The rest of the program can work with whatever you
  like best.
 
 And in what way is this incompatible with either FRP as pierre  
 suggested, or with generating an infinite list of times at which we  
 call the function, and scanning it to find the differences?
 
Argh. I should not have copypasted that GLUT code, as it introduces
IORefs not because they're 100% necessary but because that code is
called back from C. My main point is that his task, if it is as simple
as he described it, will stay imperative. My point also depends on that
code being part of a main driver loop, as you see it in games: It's
just the thing I instantly recognised in his code.

Consider this:

update :: MyState - Int - MyState
draw :: MyState - IO ()

mainLoop :: MyState - Int - IO ()
mainLoop st old = do
now - getTimeOfDay
let td = now - old
st' = update st td
draw st'
mainLoop st' now

This, to my understanding of things, is imperative, although there's no
IORef to be found. OTOH, recursing like this is the FP way to do it.
mainLoop's arguments are variables to my eyes.

-- 
(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. 

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


答复: [Haskell-cafe] Re: How to do this in FP way?

2008-06-16 Thread Magicloud
Yes, that's it, State.
Thanks.

-邮件原件-
发件人: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] 代表 Achim Schneider
发送时间: 2008年6月16日 12:01
收件人: haskell-cafe@haskell.org
主题: [Haskell-cafe] Re: How to do this in FP way?

Magicloud Magiclouds [EMAIL PROTECTED] wrote:

 static int old;
 int diff (int now) { /* this would be called once a second */
   int ret = now - old;
   old = 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 = do
t0 - get $ t state
t1 - get elapsedTime
t state $= t1
let td = fromIntegral t1 - fromIntegral t0
fps state $= 1/td * 1000

angle' state $~! (+2)

(bpx, bpy) - get $ ballPos state
(bvx, bvy) - get $ ballVel state

ballPos state $= (bpx + bvx*td, bpy + bvy*td)   
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. 

-- 
(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. 

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


答复: [Haskell-cafe] Re: How to do this in FP way?

2008-06-16 Thread Magicloud
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?

-邮件原件-
发件人: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] 代表 Achim Schneider
发送时间: 2008年6月16日 12:01
收件人: haskell-cafe@haskell.org
主题: [Haskell-cafe] Re: How to do this in FP way?

Magicloud Magiclouds [EMAIL PROTECTED] wrote:

 static int old;
 int diff (int now) { /* this would be called once a second */
   int ret = now - old;
   old = 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 = do
t0 - get $ t state
t1 - get elapsedTime
t state $= t1
let td = fromIntegral t1 - fromIntegral t0
fps state $= 1/td * 1000

angle' state $~! (+2)

(bpx, bpy) - get $ ballPos state
(bvx, bvy) - get $ ballVel state

ballPos state $= (bpx + bvx*td, bpy + bvy*td)   
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. 

-- 
(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. 

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


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

2008-06-16 Thread Achim Schneider
Magicloud [EMAIL PROTECTED] wrote:

 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?
 
nope, at least not in general.

update :: MyState - Int - MyState
draw :: MyState - IO ()

mainLoop :: MyState - Int - IO ()
mainLoop st old = do
now - getTimeOfDay
let td = now - old
st' = update st td
draw st'
mainLoop st' now

runs in constant space. Look up tail recursion in wikipedia.

 -邮件原件-
 发件人: [EMAIL PROTECTED]
 [mailto:[EMAIL PROTECTED] 代表 Achim Schneider
 发送时间: 2008年6月16日 12:01
 收件人: haskell-cafe@haskell.org
 主题: [Haskell-cafe] Re: How to do this in FP way?
 
 Magicloud Magiclouds [EMAIL PROTECTED] wrote:
 
  static int old;
  int diff (int now) { /* this would be called once a second */
int ret = now - old;
old = 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 = do
 t0 - get $ t state
 t1 - get elapsedTime
 t state $= t1
 let td = fromIntegral t1 - fromIntegral t0
 fps state $= 1/td * 1000
 
 angle' state $~! (+2)
 
 (bpx, bpy) - get $ ballPos state
 (bvx, bvy) - get $ ballVel state
 
 ballPos state $= (bpx + bvx*td, bpy + bvy*td)   
 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. 
 


-- 
(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. 

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


答复: [Haskell-cafe] Re: How to do this in FP way?

2008-06-16 Thread Magicloud
But if there are some beautiful arithmetics that do not use something like
state, I won't say no to them.

-邮件原件-
发件人: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] 代表 Achim Schneider
发送时间: 2008年6月16日 12:01
收件人: haskell-cafe@haskell.org
主题: [Haskell-cafe] Re: How to do this in FP way?

Magicloud Magiclouds [EMAIL PROTECTED] wrote:

 static int old;
 int diff (int now) { /* this would be called once a second */
   int ret = now - old;
   old = 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 = do
t0 - get $ t state
t1 - get elapsedTime
t state $= t1
let td = fromIntegral t1 - fromIntegral t0
fps state $= 1/td * 1000

angle' state $~! (+2)

(bpx, bpy) - get $ ballPos state
(bvx, bvy) - get $ ballVel state

ballPos state $= (bpx + bvx*td, bpy + bvy*td)   
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. 

-- 
(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. 

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


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

2008-06-15 Thread Achim Schneider
Magicloud Magiclouds [EMAIL PROTECTED] wrote:

 static int old;
 int diff (int now) { /* this would be called once a second */
   int ret = now - old;
   old = 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 = do
t0 - get $ t state
t1 - get elapsedTime
t state $= t1
let td = fromIntegral t1 - fromIntegral t0
fps state $= 1/td * 1000

angle' state $~! (+2)

(bpx, bpy) - get $ ballPos state
(bvx, bvy) - get $ ballVel state

ballPos state $= (bpx + bvx*td, bpy + bvy*td)   
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. 

-- 
(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. 

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe