some help on do and preservingMatrix

Sorry to ask such a basic question here, but I am trying to write my first Haskell OpenGL animation and just encontered with these questions: Could someone point me to some help on how does "do" work and how does preservingMatrix? I am trying to write a program starting from the OpenGL tutorial on the wiki, but I never used do's before. My very basic question is that how do I generally repeat a command many times? I understand "do" can do it, but it's not clear to me how to use it and how it works. So if I would like to write the following in one line, how could I write it?: spiral 0.7 spiral 0.8 spiral 0.9 spiral 1.0 I don't understand in which cases I can use "map something [0.7,0.8..1]" and in which cases I need do and how could I map using do.
From the tutorial, I could write this line, which works, but I don't really know what does mapM_ and preservingMatrix do.
mapM_ (\x -> preservingMatrix $ do scale x x (1::GLfloat) rotate (x*(200)) $ Vector3 0 0 (1::GLfloat) spiral ((sin(a/50))^2/2+0.5) ) $ ([0.5,0.6..1::GLfloat]) Zsolt

2009/11/26 Zsolt Ero
My very basic question is that how do I generally repeat a command many times? I understand "do" can do it, but it's not clear to me how to use it and how it works.
So if I would like to write the following in one line, how could I write it?: spiral 0.7 spiral 0.8 spiral 0.9 spiral 1.0
I don't understand in which cases I can use "map something [0.7,0.8..1]" and in which cases I need do and how could I map using do.
Say we have something to draw a spiral: spiral :: (Floating f) => f -> IO () So `spiral` takes a floating point number and gives us a program in the IO monad that returns `()`. We wish to draw several spirals. Here's one way: draw_them_all = do spiral 0.7 spiral 0.8 spiral 0.9 spiral 1.0 Here's another way: another_way = do sequence_ several_spirals where several_spirals :: [IO ()] several_spirals = map spiral [0.7,0.8,0.9,1.0] Or even: another_way' = sequence_ several_spirals where several_spirals :: [IO ()] several_spirals = map spiral [0.7,0.8,0.9,1.0] The `do` is helpful to compose several monadic values (in this case, programs in the IO monad). If you have a list of monadic values you can use `sequence_` to join them together into one monadic value. So you use `map` to build your programs and then `sequence_` to join them together in order.
From the tutorial, I could write this line, which works, but I don't really know what does mapM_ and preservingMatrix do.
The function `mapM_` is simply the composition of `sequence_` and `map`. mapM_ f list = sequence_ (map f list) As for `preserveMatrix`, it is some OpenGL thing; probably what it does is ensure that any changes you make to one of the matrices that make up the rendering state are undone once you exit the enclosed computation. -- Jason Dusek
participants (2)
-
Jason Dusek
-
Zsolt Ero