I heard about F# Koans on a recent episode of .NET Rocks and went to check it out. It is a series of little bite sized F# tutorials. Koans are a great way to teach a language. The little independent chunks of functionality get wrapped up in a little test to complete to show you understand. I picked up a few specific F# things from it.
- Multiple types in discriminated unions example
- type Favorite =
| Bourbon of string
| Number of int
- type Favorite =
- Seq is IEnumerable, which explains why it can have deferred execution but doesn’t always
- Arrays are the .Net arrays and that means they’re mutable
I also got some broader insights as well.
- Seeing code using classes and modules helped me better understand the scoping issues I was having with them writing them on my own.
- Looking through all of the code that was designed to run the system since it showed off some other F# that wasn’t covered before like annotations and reflection.
I still haven’t written code with the backwards pipe operator <| but reading code using it makes me want to refactor what I did write that way and try to get there. I think it’s much clearer in terms of readability. I’m not sure how to overcome my natural inclination toward the forwards pipe operator or parenthesis and instead write the code I want to read the first time.
I had not thought a fill-in-the-blank style of application would work with a compiled language since you couldn’t run each chunk without having done all of the exercises. But this application worked around the type system to create code that would compile but was clear that you needed to fill in the blanks. I liked the koans for learning purposes since I could pop over to do a couple in a quick burst. I also liked that it had exercises that asked you what a provided code sample did, as well as ones that asked you to write code yourself to achieve a specific result, not just one or the other. It took me about two hours to do all of the exercises so go ahead and check it out.