Confused by GLFW and more

To: Beginners List I am trying to get a simple program working that will display something on the screen, get user input via joystick until a quit event, and then start a new trial, and repeat this until all the trials have been performed. The problem is that if I try to sequence the trials it hangs on the last trial (screen freezes and I have to kill it), however, if I just execute one after the other it exits fine. I don't understand what is different about doing the commented line versus explicitly invoking two trials (as demonstrated in the snippet below). Here is a snippet ( I am happy to provide more, but I don't know what is needed to diagnose my problem and I don't want to clutter the question): tn = 3 main :: IO () main = do let ps = initPlayers createWindow putStr "Window Initialized\n" let joy = GLFW.Joystick 1 putStr "Got Joystick\n" --sequence_ $ map (trialLoop gain tolerance joy) (trialList ps) trialLoop gain tolerance joy ((trialList ps)!!0) trialLoop gain tolerance joy ((trialList ps)!!1) putStr "Back from Sequence\n" where trialList oldps = [[(h' i) , (p' i)] | i <- [1 ..], i < tn] where x = 0.2 y = 0.2 h' idx = (oldps!!0) p' idx = Prey (Circle (((-1)^idx)*0.2,0.2) ((radius . shape) (oldps!!1))) (clr $ oldps!!1) As an aside, I was wondering if anyone has experience using joysticks with any of the Graphics.UI I have both a logitech and a saitek and I find that although both are found by the Graphics.UI.SDL I cannot get the buttons for the Saitek, but can for logitech. For neither does pollEvents seem to work right, although I can use update and getAxis for both joysticks. It was this unpredictable behaviour that drove me from SDL to GLFW. Any general advice on the UIs for joysticks would be appreciated as well. I am not actually writing a game, but a simple psychophysics experiment (the use of Haskell is just an exercise - normally I would do this [and have been done with it long ago] in python. So, my questions are really focused on trying to use haskell and its libraries for this purpose). Using Arch Linux if that makes any difference. Thanks in advance for any comments. --Britt

On Thu, Mar 3, 2011 at 3:34 PM, Britt Anderson
--sequence_ $ map (trialLoop gain tolerance joy) (trialList ps) trialLoop gain tolerance joy ((trialList ps)!!0) trialLoop gain tolerance joy ((trialList ps)!!1)
First of all, instead of "sequence_ $ map ..." use "mapM_ ...", which is the same thing but nicer. Now, have you tried "mapM_ (trialLoop gain tolerance joy) (take 2 $ trialList ps)"? That should be the same as those two lines you wrote. I am suspecting here that your program is hanging only with mapM_ because of some of the trials that are not the first two. HTH, -- Felipe.

This does fix the problem. Thank you. But raises a new question. Since
that list was being constructed like this
where trialList oldps =
[[(h' i) , (p' i)] | i <- [1 ..], i < tn]
it should have only had two elements when tn = 3. So, I assume that I
am getting hit by a laziness issue, but how is it that I should have
known that using mapM_ wouldn't have force thelist to exist in a
finite form?
By the way, I just now use "take (tn -1)" and all is good, but I would
like to understand this issue better so I can avoid similar pitfalls
in the future.
Thx, Britt
On Thu, Mar 3, 2011 at 10:59 AM, Felipe Almeida Lessa
On Thu, Mar 3, 2011 at 3:34 PM, Britt Anderson
wrote: --sequence_ $ map (trialLoop gain tolerance joy) (trialList ps) trialLoop gain tolerance joy ((trialList ps)!!0) trialLoop gain tolerance joy ((trialList ps)!!1)
First of all, instead of "sequence_ $ map ..." use "mapM_ ...", which is the same thing but nicer.
Now, have you tried "mapM_ (trialLoop gain tolerance joy) (take 2 $ trialList ps)"? That should be the same as those two lines you wrote. I am suspecting here that your program is hanging only with mapM_ because of some of the trials that are not the first two.
HTH,
-- Felipe.

On Thursday 03 March 2011 17:40:52, Britt Anderson wrote:
This does fix the problem. Thank you. But raises a new question. Since that list was being constructed like this
where trialList oldps = [[(h' i) , (p' i)] | i <- [1 ..], i < tn]
where trialList oldps = [[h' i, p' i] | i <- takeWhile (< tn) [1 .. ]] The probem is that the compiler can't know that i never gets smaller than tn again once the first i >= tn is reached¹, so it is busy generating ever larger integers and testing them against tn, never finishing (well, it will finish when i is large enough to blow your memory, perhaps when i >= 2^(2^31) on a 32-bit system, I don't know how overflow of Integer is handled) ¹ An instance of Enum and Ord may wrap around, although that would be against the spirit of those two classes.
it should have only had two elements when tn = 3. So, I assume that I am getting hit by a laziness issue, but how is it that I should have known that using mapM_ wouldn't have force thelist to exist in a finite form?
Laziness only in so far as it's necessary for [1 .. ]. The issue has nothing to do with monads or mapM_, it's the fact that the compiler doesn't use 'rules that should hold for instances of ...' and hence has to try on after a human knows it's over.
By the way, I just now use "take (tn -1)" and all is good, but I would like to understand this issue better so I can avoid similar pitfalls in the future.
A similar pit is often fallen into with filter.
Thx, Britt

On Thu, Mar 3, 2011 at 5:13 PM, Daniel Fischer
On Thursday 03 March 2011 17:40:52, Britt Anderson wrote:
This does fix the problem. Thank you. But raises a new question. Since that list was being constructed like this
where trialList oldps = [[(h' i) , (p' i)] | i <- [1 ..], i < tn]
where trialList oldps = [[h' i, p' i] | i <- takeWhile (< tn) [1 .. ]]
The probem is that the compiler can't know that i never gets smaller than tn again once the first i >= tn is reached¹, so it is busy generating ever larger integers and testing them against tn, never finishing (well, it will finish when i is large enough to blow your memory, perhaps when i >= 2^(2^31) on a 32-bit system, I don't know how overflow of Integer is handled)
And the fix would be to code as where trialList oldps = [[h' i, p' i] | i <- [1..tn]] PS: Why aren't you using a tuple? =) -- Felipe.
participants (3)
-
Britt Anderson
-
Daniel Fischer
-
Felipe Almeida Lessa