What if I implement something which is patented, purely to teach myself about the subject?

This appears to be OK, if done with care. For example, writing software for something patented, and distributing it in source form only for educational purposes seems to be legally OK. I'm not sure I would like to try implementing a complete polished solution in this way, though. It seems like skating on thin ice.