Recommended F# Beginner Resources

It's still very early in my F# journey, but after spending some time with several resources available to newcomers, I have some recommendations.  I believe the following list to be a decent path for moving from Novice to Advanced Beginner in F#.  If you think I've missed some gems or disagree, please leave a comment.


Try F#

This site takes the "Try [insert language]" idea that has been done in other languages and really runs with it, providing several topic-oriented tutorials beyond the basics.  It's a great way to get your brain prepped for F#.   Note:  I had some issues getting the script window to echo what I was typing in Safari on Mac OSX.  I had to switch to Chrome in order to get it to work.


The F# Survival Guide

Even though it is dated, I consider this the best tutorial on F# for a beginner.  The concepts are introduced in a logical progression that build on top of one another, the pacing feels right, the language used is simple and direct, and code is introduced in a way that can be run directly in fsi (a recurring theme in this post is learning by doing).   After getting discouraged reading a couple of books where the authors failed to trim the context down in their examples or would bounce around concepts in a strange order, this resource gave me confidence.  Once I finish this, I plan on going back to the books I gave up on because they are more current than the survival guide.

For some reason, the F# Survival Guide's original website is no longer available, but the contents can still be found on the Internet Archive.   It's obvious the authors put a lot of effort into this excellent resource.   It's a shame it doesn't have an official home or the ability to be updated.


F# Koans

Many languages now have Koans, but the basic premise is the same:  given a series of tests that cover the language's fundamentals, fill in blanks in the code that makes the tests pass.   Starting from simple basics and moving to more complex concepts, the Koans are a great way to have simple problems put in front of you that require thinking in the language to solve.


Exercism.io

I recommend not attempting these until you have some F# practice under your belt.  Similar to the Koans, Exercism exercises present you with failing test cases that you must write code to fix.  The difference from the Koans is that the exercises are not F#-specific and you can get feedback from mentors when you submit a solution.  I wrote more about Exercism with F# in a post that includes how to get set up using Xamarin Studio on Mac OSX.

This one suffers a bit due to the size of the F# community participating.   None of the F# exercises I've submitted have gotten feedback.  Other languages have more mentors willing/able to provide feedback, and it does make a difference.   Maybe a combination of attempting these and checking in with the F# irc channel would suffice?


Domain Modelling with the F# Type System

This fantastic talk by Scott Wlaschin opened my eyes to algebraic types.  Scott has a knack for taking complex concepts and making them accessible.  Watching this talk made me feel like I've got a lot to learn (more fun ahead).  It brings up some questions:
  • What would business logic/service layers look like using these types? 
  • How would these relationships be stored in an RDBMS?
  • What are the best practices for structuring large F# programs?

Which leads me to the following...

Continuing Education


Once you've read/watched the above, you should be comfortable with a lot of the F# that you will encounter.  It's at this point where you enter a different phase:  knowing enough to be dangerous but feeling ill-equipped to tackle more serious problems.

For myself, I'm planning on building a couple of pet projects and writing about them, but I also need more directed learning.   Something along the lines of A Recipe for a Functional App on F# For Fun and Profit (also by Scott Wlaschin, mentioned above).  If you are reading this and have  any recommendations, I'd appreciate them.   Any books, talks?   Are there any non-trivial F# code bases out there that exhibit best practices and are ideal for learners?   Please share!

If you are working on something targeted at teaching F# newbies and need a guinea pig or some feedback, let me know--I love learning this stuff and am interested in contributing.

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!