Last year, not knowing what to expect on the ride, I was over prepared. This turned out to be great, though: I was able to have fun on the ride, lead a lot of drafting lines, and do harder events later in the year. This year, I have not had as much time to prepare, but I still feel ready.
Yesterday, J and I did 50 mile ride up to the Crystal Springs Reservoirs. Today, in an effort to practice back-to-back days on the saddle, I'm going try this ride out to the Pacific. Riding these distances is very liberating; with planning, I can basically travel anywhere in the Bay area without relying on anything other than my bike and my own legs.
Saturday, May 19, 2007
Wednesday, May 9, 2007
What We Do
"I'm programming in the Bay Area," I usually reply. This superficial answer is unfortunately about as deep as non-tech people want to hear. Computer science has the dubious distinction of being the most popular profession that most people don't understand. It's certainly not helped by a media image of a loan hacker sitting in front of a glowing screen in a dark room. I'm going to try to boil it down so that my mom and sister can understand the basic areas of the computer profession. I try to avoid metaphors because I want people to understand what I *do* do, not what I "like" do.
Coders
OK, maybe not the best group to start with, since this group includes the load hacker in the dark room. But more commonly, these are the folks that write detailed instructions which tell the computer how to handle specific situations. For example, at Amazon, when you search for products, the words you type in the box have certain rules applied to them. When you type "shirts" products labeled with "shirt" need to come up, so you need a rule to singularize the words. But think about the English language: this isn't easy. Boxes, hoodies, shoes. So a coder needs to come up with instructions to handle all of these cases, the rule to "remove the last 's' from words that end in s" will only be a part of it.
Engineers
Computer engineers take real world problems, break them down into smaller sub-problems, and describe a general way to solve them. Coders take that general solution and write specific instructions to the computer to solve the sub-problem. Continuing the search example, engineers took a problem -- how can people find products they're looking for? -- and broke it into sub-problems: if people tell us what they're looking for, how can we give them suggestions? if we have a list of suggestions, how can we show them? Then they arrive at a general solution: people type in a short description of the desired product to tell us, we make a list of products that have all of the words they're looking for in their description, then we show some of the products on the web page.
Operations
This is my group. 'Ops' is in charge of keeping the software running. Sometimes programs crash; we develop software called 'cradles' which can restart a program in the event of a failure. Sometimes programs get overloaded when everyone logs in at once to begin their Christmas shopping; we set up new computers and faster networks to handle increased number of users. Sometimes software mysteriously starts running slower than usual; we discover assumptions that coders or engineers made that aren't valid and try to work around them. One example is if a machine runs out of disk space. We either produce software that detects this and removes old files, or do it manually.
Quality Assurance
QA teams develop and run tests against the software. They make sure that a search for 'shoes' matches products with 'shoe,' not 'sho.' Many techniques are involved in this: regression (comparing a new version of the software to the old), unit test (compare each part of the software, for example just feeding words into the part of the software the singularizes), corner cases (what happens in weird situations like using foreign languages).
Scientist
Computer scientists usually write two types of research: papers which solve hypothetical problems, or papers which analyze a class of solutions. The hope is that hypothetical problems will eventually become real problems for the former, or that by analyzing a class of solutions, an improved solution to an existing problem will make itself apparent. For the class I am taking, an example of the former is papers from the 1980s which try to solve how computers should keep in sync with each other: when I transfer money to your bank account, that money shouldn't disappear, it should either be in my account or yours. However, the techniques that were proposed at that time aren't working so well, so more recent papers are analyzing the techniques that engineers ended up implementing.
Now You Understand Completely
Well, not really. It turns out that most jobs are a combination on these fields. Engineers often code the specific instructions after they describe the general solution. QA folks have to understand what real world problems a program is trying to solve in order to think of test cases. Scientists work with companies to develop solutions for specific problems.
Additionally, this is just a taxonomy I wrote down after a little thought. Certainly there can be other ways to organize the field, and you won't get a definitive answer if you ask people "what category are you in?" Hopefully, though, this can give you a background for some of the issues that are discussed around the computer industry.
Outsourcing
Coding jobs are the main target of outsourcing. Because the process of coding is often closed -- general solution in, computer instructions out -- coding teams can be separated from the end customer.
Schools
Universities want to produce engineers, but often they produce a mix of the worst qualities of scientists and coders. They produce a scientist's attention to implementation details and a coder's attention to the overall problem being solved. Classroom projects are run in an academic setting and don't need to worry about security, portability, or decade long maintenance issues. Without a bigger picture, coders can product instructions that are technically correct, but which fail to anticipate customer needs and implicit requirements.
"Why aren't you programming?"
Graduate classes in computer science focus on the science aspect, not coding. That's why my assignments don't involve programming and why professors often need help setting up their Windows computer. When programming is involved, it is used to assess how well you understand a problem. If you can write instructions to the computer for every situation in a problem space, then you've demonstrated good comprehension of the problem and solution.
The Story Continues?
Agree, disagree? Write comments :)
Coders
OK, maybe not the best group to start with, since this group includes the load hacker in the dark room. But more commonly, these are the folks that write detailed instructions which tell the computer how to handle specific situations. For example, at Amazon, when you search for products, the words you type in the box have certain rules applied to them. When you type "shirts" products labeled with "shirt" need to come up, so you need a rule to singularize the words. But think about the English language: this isn't easy. Boxes, hoodies, shoes. So a coder needs to come up with instructions to handle all of these cases, the rule to "remove the last 's' from words that end in s" will only be a part of it.
Engineers
Computer engineers take real world problems, break them down into smaller sub-problems, and describe a general way to solve them. Coders take that general solution and write specific instructions to the computer to solve the sub-problem. Continuing the search example, engineers took a problem -- how can people find products they're looking for? -- and broke it into sub-problems: if people tell us what they're looking for, how can we give them suggestions? if we have a list of suggestions, how can we show them? Then they arrive at a general solution: people type in a short description of the desired product to tell us, we make a list of products that have all of the words they're looking for in their description, then we show some of the products on the web page.
Operations
This is my group. 'Ops' is in charge of keeping the software running. Sometimes programs crash; we develop software called 'cradles' which can restart a program in the event of a failure. Sometimes programs get overloaded when everyone logs in at once to begin their Christmas shopping; we set up new computers and faster networks to handle increased number of users. Sometimes software mysteriously starts running slower than usual; we discover assumptions that coders or engineers made that aren't valid and try to work around them. One example is if a machine runs out of disk space. We either produce software that detects this and removes old files, or do it manually.
Quality Assurance
QA teams develop and run tests against the software. They make sure that a search for 'shoes' matches products with 'shoe,' not 'sho.' Many techniques are involved in this: regression (comparing a new version of the software to the old), unit test (compare each part of the software, for example just feeding words into the part of the software the singularizes), corner cases (what happens in weird situations like using foreign languages).
Scientist
Computer scientists usually write two types of research: papers which solve hypothetical problems, or papers which analyze a class of solutions. The hope is that hypothetical problems will eventually become real problems for the former, or that by analyzing a class of solutions, an improved solution to an existing problem will make itself apparent. For the class I am taking, an example of the former is papers from the 1980s which try to solve how computers should keep in sync with each other: when I transfer money to your bank account, that money shouldn't disappear, it should either be in my account or yours. However, the techniques that were proposed at that time aren't working so well, so more recent papers are analyzing the techniques that engineers ended up implementing.
Now You Understand Completely
Well, not really. It turns out that most jobs are a combination on these fields. Engineers often code the specific instructions after they describe the general solution. QA folks have to understand what real world problems a program is trying to solve in order to think of test cases. Scientists work with companies to develop solutions for specific problems.
Additionally, this is just a taxonomy I wrote down after a little thought. Certainly there can be other ways to organize the field, and you won't get a definitive answer if you ask people "what category are you in?" Hopefully, though, this can give you a background for some of the issues that are discussed around the computer industry.
Outsourcing
Coding jobs are the main target of outsourcing. Because the process of coding is often closed -- general solution in, computer instructions out -- coding teams can be separated from the end customer.
Schools
Universities want to produce engineers, but often they produce a mix of the worst qualities of scientists and coders. They produce a scientist's attention to implementation details and a coder's attention to the overall problem being solved. Classroom projects are run in an academic setting and don't need to worry about security, portability, or decade long maintenance issues. Without a bigger picture, coders can product instructions that are technically correct, but which fail to anticipate customer needs and implicit requirements.
"Why aren't you programming?"
Graduate classes in computer science focus on the science aspect, not coding. That's why my assignments don't involve programming and why professors often need help setting up their Windows computer. When programming is involved, it is used to assess how well you understand a problem. If you can write instructions to the computer for every situation in a problem space, then you've demonstrated good comprehension of the problem and solution.
The Story Continues?
Agree, disagree? Write comments :)
Sunday, May 6, 2007
Take that, AIDS!
First, the awesome news: because of the generosity of many people, we broke the $7000 mark for AIDS ride fundraising! I'm so glad I decided to do the ride again this year, because my experience already has been very different. Last year, I started fundraising and training much earlier. Donations came in from mostly close friends and relatives: a steady stream that was encouraging. This year I began late, but if anything that seems to have made folks more motivated! I have been getting donation from a wider range of people, and my repeat sponsors are giving inspiring amounts.
For those who haven't had a chance to donate yet, even though we've surpassed my fundraising goal, I want to point out that we still haven't met the goal of defeating an epidemic that has killed 25 million people. Your support is still needed and appreciated!
Second is a change in plans. Earlier this year I had really wanted to do the Tour de Cure, 1 day cycling rides that raise funds and awareness for diabetes. Unfortunately, because of scheduling conflicts, I won't be able to do these rides this year. I encourage the other cyclists who read this blog to look into them, though, as I think charity rides are a great way to take the next step in the sport.
Thanks again for everyone's support!
For those who haven't had a chance to donate yet, even though we've surpassed my fundraising goal, I want to point out that we still haven't met the goal of defeating an epidemic that has killed 25 million people. Your support is still needed and appreciated!
Second is a change in plans. Earlier this year I had really wanted to do the Tour de Cure, 1 day cycling rides that raise funds and awareness for diabetes. Unfortunately, because of scheduling conflicts, I won't be able to do these rides this year. I encourage the other cyclists who read this blog to look into them, though, as I think charity rides are a great way to take the next step in the sport.
Thanks again for everyone's support!
Sunday, April 29, 2007
Beautiful Backyard
Thank you for the donations so far, guys. I got a late start this year, so if you're thinking of donating, don't delay! Today I completed a big training ride around the Open Spaces area. The weather was awesome, the hills were green, and I got to meet a bunch of people.
The route.
I think Moody is easier than the lower part of Page Mill because it slowly elevates before the big hills. Page Mill was very challenging, as usual: I think I set a new record on my heart rate monitor: 194bpm. Here's my favorite turn on Page Mill, taken last year:

There's a Y in the road where Alpine turns West that I like to stop and eat my snacks. Today there were a couple of bikers hanging out there as well, so I stopped to chat. They had driven from the Central Valley just to do this ride with a few friends. "You have a beautiful backyard here," they said. I'd have to agree, I can't think of a better place to train and improve. The hills are hard, but the scenery, bay views, and redwoods make every turn exciting.
There used to be this random water fountain on the south side of Page Mill. It was great because there are basically no other places to get water up on Skyline. It was removed some time during the winter, though, and all that's left is PVC pipe sticking up out of the ground. Luckily, though, I ran into a ranger who, when asked where the nearest fountain was, offered two of his own water battles. After ensuring that he didn't need them, I talked to him while I refilled my bottles. Apparently a rancher had set up the fountain for folks passing by, but the cold snaps we had last winter broke the water lines.
Coming back up La Honda, I stopped at my favorite burger place, Alice's, for refueling. The tables are big picnic tables, so I happened to sit with some mountain bikers. After a delicious burger and ale, I swooped back down the mountain, passing a couple slow cars that were polite enough to move over.
I do need to concentrate on training in the next couple weeks: ALC is only 5 weeks away!
The route.
I think Moody is easier than the lower part of Page Mill because it slowly elevates before the big hills. Page Mill was very challenging, as usual: I think I set a new record on my heart rate monitor: 194bpm. Here's my favorite turn on Page Mill, taken last year:
There's a Y in the road where Alpine turns West that I like to stop and eat my snacks. Today there were a couple of bikers hanging out there as well, so I stopped to chat. They had driven from the Central Valley just to do this ride with a few friends. "You have a beautiful backyard here," they said. I'd have to agree, I can't think of a better place to train and improve. The hills are hard, but the scenery, bay views, and redwoods make every turn exciting.
There used to be this random water fountain on the south side of Page Mill. It was great because there are basically no other places to get water up on Skyline. It was removed some time during the winter, though, and all that's left is PVC pipe sticking up out of the ground. Luckily, though, I ran into a ranger who, when asked where the nearest fountain was, offered two of his own water battles. After ensuring that he didn't need them, I talked to him while I refilled my bottles. Apparently a rancher had set up the fountain for folks passing by, but the cold snaps we had last winter broke the water lines.
Coming back up La Honda, I stopped at my favorite burger place, Alice's, for refueling. The tables are big picnic tables, so I happened to sit with some mountain bikers. After a delicious burger and ale, I swooped back down the mountain, passing a couple slow cars that were polite enough to move over.
I do need to concentrate on training in the next couple weeks: ALC is only 5 weeks away!
Thursday, April 12, 2007
By Accident
This post was from February of last year. Miraculously, I manged to correctly navigate all of last year's AIDS ride. I still rely on S to get me back on track when I get turned around, but luckily those request have become much more infrequent. Without further ado:
I was chased by a dog today for the first time! Luckily it was a smallish dog... with stubbly legs. OK, it wasn't really a threat, but it was exciting. I was doing my longest ride yet, a loop around the South Bay. I made a little detour around mile 19, mistakenly thinking it would reconnect with my route. As I was turning around, I heard the barking of a dog preparing to protect it's territory. Rather than get off the bike and trying to settle the dog down, possibly involving small teeth embedded in my skin, I took off. Its instinct aroused, the dog gave chase. Winding through a small park, I eventually lost him as the park path connected to the street. I'm not saying that I look forward to my next canine encounter, but surprises like this turn out to be essential parts of my training, pushing me a little harder than I would on my own.
One of my most common surprise is the wrong turn. The Sand Hill/Alpine/Page Mill region around Portolla Valley still manages to confound me. My medium commute was originally going to be shorter, cutting into Palo Alto on Alpine Rd. instead of going around to Sand Hill. However, the "wrong" turn I made on my inaugural trip ended up being a much better morning workout; my original route would have shortchanged me.
Of course, the next time I rode, I missed the Arestradero turn, and ended up stuggling up Page Mill. After about 800 feet of climbing, and no real idea of when it would end, I decided to turn back. The climb was a great challenge, though; pushing yourself up hills like these are the best way to really increase the body's ability to process oxygen.
Last weekend's ride was determined en route as well. Rolling out of bed at 9, I realized that I had missed nearly all of the weekly AIDS training rides. Hastily I decided I could make the 9:30 roll out time for the South Bay group, but driving up to the Union City meet point, I saw the big group already cycling down the rode. Foolishly undeterred, I parked and hopped on my bike. Not knowing the route, though, I was never able to catch up to the main group. Riding in a southern direction, my best guess as to the route, I found myself serendipitously in S's neighborhood. After a quick consult of Google maps, and a good-luck kiss, I was back on the rode, solo. I don't think I would have attempted such a long route without having taken that first stumble forward. So, while next time I hope to be better prepared for a ride, I'm thankful for all of my surprises so far.
I was chased by a dog today for the first time! Luckily it was a smallish dog... with stubbly legs. OK, it wasn't really a threat, but it was exciting. I was doing my longest ride yet, a loop around the South Bay. I made a little detour around mile 19, mistakenly thinking it would reconnect with my route. As I was turning around, I heard the barking of a dog preparing to protect it's territory. Rather than get off the bike and trying to settle the dog down, possibly involving small teeth embedded in my skin, I took off. Its instinct aroused, the dog gave chase. Winding through a small park, I eventually lost him as the park path connected to the street. I'm not saying that I look forward to my next canine encounter, but surprises like this turn out to be essential parts of my training, pushing me a little harder than I would on my own.
One of my most common surprise is the wrong turn. The Sand Hill/Alpine/Page Mill region around Portolla Valley still manages to confound me. My medium commute was originally going to be shorter, cutting into Palo Alto on Alpine Rd. instead of going around to Sand Hill. However, the "wrong" turn I made on my inaugural trip ended up being a much better morning workout; my original route would have shortchanged me.
Of course, the next time I rode, I missed the Arestradero turn, and ended up stuggling up Page Mill. After about 800 feet of climbing, and no real idea of when it would end, I decided to turn back. The climb was a great challenge, though; pushing yourself up hills like these are the best way to really increase the body's ability to process oxygen.
Last weekend's ride was determined en route as well. Rolling out of bed at 9, I realized that I had missed nearly all of the weekly AIDS training rides. Hastily I decided I could make the 9:30 roll out time for the South Bay group, but driving up to the Union City meet point, I saw the big group already cycling down the rode. Foolishly undeterred, I parked and hopped on my bike. Not knowing the route, though, I was never able to catch up to the main group. Riding in a southern direction, my best guess as to the route, I found myself serendipitously in S's neighborhood. After a quick consult of Google maps, and a good-luck kiss, I was back on the rode, solo. I don't think I would have attempted such a long route without having taken that first stumble forward. So, while next time I hope to be better prepared for a ride, I'm thankful for all of my surprises so far.
Wednesday, April 4, 2007
On the Road Again
It's official: I'm riding in this year's AIDS/Lifecycle! Last year I road almost 550 miles, slept in 6 new cities in a tent with a stranger, met hundreds of awesome people, popped 4 spokes, and raised $5,000 for the SF AIDS foundation. It was an amazing experience, and I knew I had to do it again this year. You can sign up to sponsor me now!

This year, I'm also planning on doing two Tour de Cure rides, so I'll post details about that when I sign up. My fund raising goals are $3500 for ALC and $750 for each TdC. (For the curious, this is approximately the ratio of days I'll be spending on each ride.)
I'll be reposting my entries from last year's ride, and keeping everyone up to date on my training. Thanks for your support.
This year, I'm also planning on doing two Tour de Cure rides, so I'll post details about that when I sign up. My fund raising goals are $3500 for ALC and $750 for each TdC. (For the curious, this is approximately the ratio of days I'll be spending on each ride.)
I'll be reposting my entries from last year's ride, and keeping everyone up to date on my training. Thanks for your support.
Saturday, March 24, 2007
OpenGL Tetris in Haskell
Over the past couple weekends I've tried putting my new language interest, Haskell, through the paces by witting a version of Tetris with HOpenGL bindings. Herein is an overview of my progress, and a call for help from the more experienced in the Haskell community to show me and other newbies how this can be improved and why.

The Good
As noted, um, everywhere, Haskell is a really deep language. As a beginner, the features that have struck me are the ease of defining functions, pattern matching in definitions, currying, and the seemingly endless stream of higher order functions.
Making function definitions as easy as variable assignments in other languages encourages me to break apart logic and leads to cleaner code. Defining functions in the `where` clause I've found to be especially handy and elegant. Unlike imperative programming, where code with very similar functionality can hide in a jungle of statements, repeating code in Haskell is easy to pick out and convenient to factor out.
Pattern matching in function definitions has been incredibly handy. In Perl, I'll often use a hash to map things like tetris shapes to their set of points. Haskell's version is more useful because I can define a function which takes a shape and returns a set of points associated with it. The difference is I can pass this function around or use it in higher order functions without using extra syntax to do the lookup.
Currying without any extra syntax is a big contributer to Haskell's clarity. Now that I've experienced it's usefulness, I don't think I could go back to languages like Scheme without constantly complaining about its absence.
Besides the standard `fold` and `map`, I find myself thinking "Wow, that will be usefull" whenever I look through Haskell's standard libraries. Functions like `zipWith` or `unionsWith` make even the most useful Java classes look barbaric.
The Bad
When I first started writing Haskell, the thing I found most annoying was the big change in associativity. The downside of functions being treated exactly like data is that it becomes impossible for the compiler to correctly predict how you want things grouped. It didn't take long to become accustomed to Haskell's rules, but for whatever reason learning to use $ and . correctly took a bit time.
Monads obviously need to be included in this list, but I found understanding the simple monads such as IO, Maybe, and Either simple enough to quickly make progress. I have not implemented my own, and I suspect my Tetris code could be significantly improved with better understanding, so I'll be digging into this area again soon.
Typing has not been a problem for me. Haskell's typing inference system works great, and the times I've run into typing errors have been either an error in my code (most frequently), or, in my belief, a bad framework for what I was trying to accomplish.
The Ugly
I've basically run into the same problem with libraries and setup documented by Eric. Documentation of libraries could also use a lot of improvement. Here's an example of documentation for a function in some introspective code that I was trying to understand:
What About Tetris?
First off, I don't think that Tetris and OpenGL map onto Haskell very easily. OpenGL in particular is very stateful, and even setting up a simple animation requires using Monads to work around Haskell's lack of side effects. I think this excersize is still useful, though, because in pushing a language to it's boundaries and forcing you to use it's less hyped features, you can often getter a better understanding of it's strengths and limitations. Before starting this project, I read over Michi's posts. In fact, his cube generation code is largely unchanged in mine.
What I'd like to see happen is for the amazingly helpful Haskell community (seriously, check out #haskell some time, it is the nicest, most helpful channel I've visited) to take my crappy code, explain why it sucks, and show me and other timid beginners how to do better. Just add a comment to where people should look for improved versions.
My code is available at PBWiki.
Tetris.hs
This is the code that kicks off the OpenGL loop. Callback functions are used to display objects, coordinate movements, and set state. You can see by the number of newIORef calls how much state is being passed around to each callback. The most confusing line here is probably the assignment to curMatrix. curMatrix keeps track of the matrix transformations that occur when the mouse is moved so you can use a "glass ball" type interface to rotate the tetris board around.
Display.hs
This file contains the display call backs that render the pieces and board. To implement the mouse interface, the display matrix is reset with loadIdentity. The new translations from the most recent mouse movement are applied, then the previous matrix is applied.
dropPiece drops the current piece, or sets the fallen piece and starts a new one.
idle is a callback that is continually called and it simply asks OpenGL to redisplay the screen in it's next loop. This is the alternative to calling postRedisplay after all movement changes.
TetrisPiece.hs
This file contains the code that keeps the coordinates for shapes and instructions on how to render them. translatedPoints is a function which can take the default coordinates of a shape and apply the transformations that the displayed shape receives so that collision detection can be done.
TetrisBoard.hs
Setup for the shape of the board and the boundary initialization are done here. tableMap is basically an array of all the (x,y) coordinates with False inside the boundaries (because there are no fallen pieces yet), and True along the boarders.
clearLines takes a board and clears any full lines; basically, the purpose of Tetris. Using foldl, it iterates over a list of lines given by their 'y' coordinate. For a given line, if that line is at or above 'y', it takes the contents of the line above it. The base cases are the boarders of the board, which should keep their boundaries.
Sorry for the mixed naming, by the way. 'board', 'b', 'tableMap', 'boarder' are all generally the same thing: the mapping of the contents of the Tetris board, ie the fallen pieces.
Bindings.hs
keyboardAct defines how a key press can move a shape on the board, while movePiece determines if this movement is valid.
Translations.hs
These functions are used to calculate translated points for collision detection and the "tetris unit" is defined here. Using `tunit` not only helps to keep things in correct proportion, but it also helps Haskell's typing inference when used as a unit.

The Good
As noted, um, everywhere, Haskell is a really deep language. As a beginner, the features that have struck me are the ease of defining functions, pattern matching in definitions, currying, and the seemingly endless stream of higher order functions.
Making function definitions as easy as variable assignments in other languages encourages me to break apart logic and leads to cleaner code. Defining functions in the `where` clause I've found to be especially handy and elegant. Unlike imperative programming, where code with very similar functionality can hide in a jungle of statements, repeating code in Haskell is easy to pick out and convenient to factor out.
Pattern matching in function definitions has been incredibly handy. In Perl, I'll often use a hash to map things like tetris shapes to their set of points. Haskell's version is more useful because I can define a function which takes a shape and returns a set of points associated with it. The difference is I can pass this function around or use it in higher order functions without using extra syntax to do the lookup.
Currying without any extra syntax is a big contributer to Haskell's clarity. Now that I've experienced it's usefulness, I don't think I could go back to languages like Scheme without constantly complaining about its absence.
Besides the standard `fold` and `map`, I find myself thinking "Wow, that will be usefull" whenever I look through Haskell's standard libraries. Functions like `zipWith` or `unionsWith` make even the most useful Java classes look barbaric.
The Bad
When I first started writing Haskell, the thing I found most annoying was the big change in associativity. The downside of functions being treated exactly like data is that it becomes impossible for the compiler to correctly predict how you want things grouped. It didn't take long to become accustomed to Haskell's rules, but for whatever reason learning to use $ and . correctly took a bit time.
Monads obviously need to be included in this list, but I found understanding the simple monads such as IO, Maybe, and Either simple enough to quickly make progress. I have not implemented my own, and I suspect my Tetris code could be significantly improved with better understanding, so I'll be digging into this area again soon.
Typing has not been a problem for me. Haskell's typing inference system works great, and the times I've run into typing errors have been either an error in my code (most frequently), or, in my belief, a bad framework for what I was trying to accomplish.
The Ugly
I've basically run into the same problem with libraries and setup documented by Eric. Documentation of libraries could also use a lot of improvement. Here's an example of documentation for a function in some introspective code that I was trying to understand:
mkConstr :: DataType -> String -> [String] -> Fixity -> ConstrNot very helpful.
Constructs a constructor
What About Tetris?
First off, I don't think that Tetris and OpenGL map onto Haskell very easily. OpenGL in particular is very stateful, and even setting up a simple animation requires using Monads to work around Haskell's lack of side effects. I think this excersize is still useful, though, because in pushing a language to it's boundaries and forcing you to use it's less hyped features, you can often getter a better understanding of it's strengths and limitations. Before starting this project, I read over Michi's posts. In fact, his cube generation code is largely unchanged in mine.
What I'd like to see happen is for the amazingly helpful Haskell community (seriously, check out #haskell some time, it is the nicest, most helpful channel I've visited) to take my crappy code, explain why it sucks, and show me and other timid beginners how to do better. Just add a comment to where people should look for improved versions.
My code is available at PBWiki.
Tetris.hs
This is the code that kicks off the OpenGL loop. Callback functions are used to display objects, coordinate movements, and set state. You can see by the number of newIORef calls how much state is being passed around to each callback. The most confusing line here is probably the assignment to curMatrix. curMatrix keeps track of the matrix transformations that occur when the mouse is moved so you can use a "glass ball" type interface to rotate the tetris board around.
Display.hs
This file contains the display call backs that render the pieces and board. To implement the mouse interface, the display matrix is reset with loadIdentity. The new translations from the most recent mouse movement are applied, then the previous matrix is applied.
dropPiece drops the current piece, or sets the fallen piece and starts a new one.
idle is a callback that is continually called and it simply asks OpenGL to redisplay the screen in it's next loop. This is the alternative to calling postRedisplay after all movement changes.
TetrisPiece.hs
This file contains the code that keeps the coordinates for shapes and instructions on how to render them. translatedPoints is a function which can take the default coordinates of a shape and apply the transformations that the displayed shape receives so that collision detection can be done.
TetrisBoard.hs
Setup for the shape of the board and the boundary initialization are done here. tableMap is basically an array of all the (x,y) coordinates with False inside the boundaries (because there are no fallen pieces yet), and True along the boarders.
clearLines takes a board and clears any full lines; basically, the purpose of Tetris. Using foldl, it iterates over a list of lines given by their 'y' coordinate. For a given line, if that line is at or above 'y', it takes the contents of the line above it. The base cases are the boarders of the board, which should keep their boundaries.
Sorry for the mixed naming, by the way. 'board', 'b', 'tableMap', 'boarder' are all generally the same thing: the mapping of the contents of the Tetris board, ie the fallen pieces.
Bindings.hs
keyboardAct defines how a key press can move a shape on the board, while movePiece determines if this movement is valid.
Translations.hs
These functions are used to calculate translated points for collision detection and the "tetris unit" is defined here. Using `tunit` not only helps to keep things in correct proportion, but it also helps Haskell's typing inference when used as a unit.
Subscribe to:
Posts (Atom)