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.


Getting a WebSharper project up and running on Mac OS X

A few weeks ago, this post about Intellifactory's WebSharper showed up on Hacker News.  The top comment was enthusiastic enough for me to check it out.

Since F# and Mono came out of the Microsoft ecosystem and I use Mac for development (and Linux for deployment), I had to do some digging to get things working.  These are the steps I used to get a basic WebSharper app up and running on Mac OS X (10.9.x).   Note that these steps are very high level and don't go into much detail.

1. Install Xamarin Studio


I've used Xamarin Studio at work to create several cross-platform mobile apps.  Being comfortable with that environment, I elected to go with Xamarin Studio for F#/WebSharper development.   You can find the download here.  Note that you will have to supply some personal info to proceed.

Once the install begins, you will eventually get to this screen:




In order for the install to work, you'll have to select at least one of the products in the list.  For the purposes of doing WebSharper development, I don't think it matters which one(s) you choose.

Once you get to the "Install Succeeded" screen, click the "Launch Xamarin Studio" button:



If you had already quit the install process, you can find Xamarin Studio in your Applications folder.  When Xamarin Studio launches, it may have updates waiting.  If so, click the "Restart and Install Updates" button.

2. Install Intellifactory's WebSharper Add-in


To make working with WebSharper in Xamarin Studio easier, Intellifactory provides the WebSharper add-in.  Installing the add-in will add project templates that make creating WebSharper applications easier.  Click the "Xamarin Studio" menu, then choose "Add-in Manager...":



With the Add-in Manager window open, click the "Gallery" tab, then click the "Repository" drop-down and choose "Manage Repositories...":



From the "Add-in Repository Management" window,  click the "Add" button.



Then paste the following URL into the "Register an on-line repository" URL field:

https://raw.githubusercontent.com/intellifactory/monodevelop.websharper/master/repository/


...and click the "OK" button.  Eventually, the add-in will finish installing.  At this point,  Xamarin Studio is ready for creating WebSharper applications.

3. Create your first WebSharper Sitelet.


There are several different kinds of WebSharper projects.  We're going to create an app that has both a client and server piece, known as a "Sitelet".  

Close all windows until you get back to the main Xamarin Studio screen.  Then click the "New Solution..." button.  In the "New Solution" window, find the WebSharper entry in the list in the left-hand pane (it's probably the last entry), then choose "WebSharper Sitelets Website".



Fill in a Name (I chose HelloWorld), make sure the project will be created in a directory you agree to,  then click the "OK" button.

4. Build the Project


At this point, you now have a basic example starter project ready to explore.  Let's see it in action.  Before we can run it, we need to build it.  Choose the "Build" menu item, then "Build HelloWorld".  If everything went well, you should see a "Build successful." message.



5. Run


From what I can tell, there's no way to run the application from inside Xamarin Studio yet.  Open Terminal, then navigate to your project's directory.  You need to be in the directory below the top-level similarly-named directory, in my case HelloWorld/HelloWorld:




Once there, type the following on the command line:

xsp4

This will start the xsp4 web server.  You should see a "Listening on port: 8080..." message.  Open a web browser to http://localhost:8080 and your app should display:




Congratulations!  If you got this far, you now have a development environment all set up to create the next amazing WebSharper application.

Where to go from here


This post just explained how to get a dev environment up and running.   There is a BUNCH to learn.  Try exploring the different pieces of the project, particularly the Main.fs, Client.fs, and Remoting.fs files.  Keep in mind that if you make any changes, you will have to rebuild the project, and re-run the server.

I am new to F# and WebSharper, but here's what I've been looking at:


Resources Used:


Most of the information I used for this post was gleaned from the following:

  • The F# Software Foundation's Use F# on Mac OSX page.
  • This Intellifactory blog post.

If you liked this post, would like to see more posts on F#/WebSharper, or have corrections or criticisms, please leave a comment or let me know on twitter.  Good luck exploring!


Eon: A Clojure Basics Quiz

As part of an effort to avoid creating yet another blog engine, I started working on something completely different:  a quiz/game called Eon.


Eon is a simple quiz designed to test basic Clojure evaluation comprehension.  At the moment, it consists of 7 levels of 10 questions.  Each question presents you with a Clojure form.  You are given an answer box to submit your answer.  An answer is initially worth 100 points.  Entering an incorrect answer drops the possible point value by 10, so a perfect score at this point is 7000 (7 levels x 10 questions per level x 100 points).

I started writing Eon for a few reasons:

  • I wanted to get out of a rut where every personal project started to look like a blog engine.
  • I wanted to learn ClojureScript.  There are lots of interesting things happening in ClojureScript lately, and I wanted to get my feet wet.  Eon is written in ClojureScript with Reagent handling the UI.
  • Working on this helped solidify my understanding of simple Clojure evaluation.
Eon is very basic, but I hope that other Clojure beginners out there find it useful.  There are levels/questions that I'd like to add, but due to some limitations in the way I'm handling answers, I'm not sure I'll get to them.  Plus, I'm already itching to create something more substantial using ClojureScript--it's a lot of fun to work with.

If you have questions, comments, or find bugs/see something wrong, please let me know via email or on Twitter.