Using Exercism.io to Grok F#

Since I'm at the beginning of learning F#, I'm looking for different ways to get up to speed as quickly as possible and test what I've learned.  To see how well I'm progressing, I've started doing the F# exercises on Exercism.io.

Exercism, created by Katrina Owen, provides learners a way to test their language comprehension by solving programming problems and getting feedback/mentoring by others.  It's like a remote code review.  After using it for a couple of languages and getting some insightful feedback, I've become a big fan.

Here's how it works:  sign up for an Exercism account, download the Exercism command line tool, fetch an exercise in the target language,  solve the exercise, then submit it to the Exercism server.  Once your solution has been published to the server, it is viewable by others who can mark it as "Looks Great", or give a "Nitpick".   You are notified of any feedback.   If the feedback suggests improvements, you can make a new revision and re-submit, or defend your choices and mark your solution as "Done."  Once you have submitted a solution for a given exercise, you can also browse other member's solutions to that same exercise and leave feedback of your own.  It's interesting to see how differently  people solve the same problem, and to contrast other solutions with your own.

An Exercism exercise consists of at least two files:  (1) a Markdown-formatted README and (2) a test file.  The README explains the exercise and the test file contains several tests written in the target language. You create a source file in the target language that makes the tests in the test file pass.   Once the tests pass, you submit the solution, then fetch the next exercise and repeat.

If you're at that point where you know enough F# to be dangerous but aren't sure where to go next, I highly recommend attempting the exercises on Exercism.io.  The more people use it, the more value it will bring those seeking to learn.  Feel free to log in and give me some Nitpicks--I can take it!

The remainder of this post walks through how I set my environment up (Mac OSX + Xamarin Studio) to do the Exercism problems.  I added some tips and the end of the post that cover some problems I experienced.


Download Xamarin Studio


I wrote about this in a previous post.


Sign up/install Exercism


Sign up for an Exercism account (requires a GitHub login) and follow the instructions to get started and download the command line tool.  Make sure you have already run exercism fetch fsharp (to download the first exercise for just F#), or exercism fetch (to download the first exercise for all languages).  The remaining steps assume you have the exercism command-line tool set up and have the exercism directory containing at least the first exercise ready to go.


Create a new F# Library Project in Xamarin Studio


Run Xamarin Studio, then click the "New Solution..." button, choose "F# Library", and give your project a name:





Install the NUnit and NUnit Runners packages


Select the "Project" menu, then the "Add Packages..." menu item.  When the "Add Package" window appears, type "NUnit" in the search field to limit the package results (there are a lot), then check the "NUnit" and "NUnit.Runners" checkboxes and click the "Add Package" button:





Add the first exercise's test file


When you ran the exercism fetch command, it should have created an exercism directory with an fsharp subdirectory.  This is directory Exercism will fetch exercises to.  Each exercise gets its own subdirectory.  Inside the fsharp subdirectory, you should see a sum-of-multiples subdirectory.  This is the first exercise.

Inside the sum-of-multiples subdirectory, you should see 2 files:
  1. README.md
  2. SumOfMultiplesTest.fs
The README file is a Markdown-formatted text file that explains the exercise and gives some basic tips on how to solve it.  The SumOfMultiplesTest.fs file is an F# source file that contains several NUnit tests that your solution file must pass before submitting.   The test file should be added to the  project.

Right-click on the project's name (not the solution's name), then choose "Add", then "Add Files...".  In the "Add Files" window, navigate to the sum-of-multiples directory and select the SumOfMultiplesTest.fs file:





Solve the exercise


If you open the SumOfMultiplesTest.fs file in Xamarin Studio, you should see some errors:




Your goal for the exercise is to: (1) make the errors go away, and (2) make all the tests in SumOfMultiplesTest.fs pass.  To do this, you'll need to create an F# source file that contains the missing functions and that fulfills the obligations of the tests.   You can run the tests by selecting the "Run" menu, then the "Run Unit Tests" menu item.

Once all of the tests pass, copy your solution file back into the exercism/fsharp/sum-of-multiples directory, cd into that directory and type exercism submit [solution filename].  This will submit your solution to the Exercism.io server and allow it to be viewable by others.  If you run the exercism fetch command again, it will pull down the next exercise.  


Tips


The F# exercises require the answers to be written in the form of a module and a class containing the solution functions.  Before attempting the problems, you should understand how F# classes work.  This post on the awesome F# for Fun and Profit should help.

The ordering of files in an F# project matters.  Your solution file should come before the test file.  You can re-order files by clicking on one, then dragging it above/below other files.

The test files have the [<Ignore>] attribute set on all but the first test.   Once you solve a  test, remove the Ignore attribute from the next test and run the tests again or it will show up ignored in the Test Results window.


Good Luck! 



If you see anything wrong with the post or have comments, please let me know here or on twitter.


No comments: