Literate programming
2015-07-27 21:00:00What is it?
You can write posts in Firm in literate programming style. What that means is that instead of writing documents in markdown text and specifically format the parts that contain code, you turn it around and make code your main content and embed text as comments. The major upside of this is that the source of your post or page is an fsharp script file (.fsx) that can be run in e.g. Visual Studio.
Made possible by
The fsharp script file is fed to FSharp.Formatting, which does all the heavy lifting.
Example
Here are some code snippets copied from http://fssnip.net with the solution the first Project Euler problem (find the sum of all multiples of 3 or 5 below 1000).
First the range.
1:
|
let range = [0..999] |
Then the predicate that checks for multiples of 3 or 5.
1:
|
let condition x = x % 3 = 0 || x % 5 = 0 |
And finally the function that runs the above declared predicate on the range and sums it up using built in library functions.
1: 2: 3: |
range |> List.filter condition |> List.fold (+) 0 |
More examples
You can find an example that covers pretty much all features on the FSharp.Formatting documentation site.
You can check the source of this blog post to see the actual script.
Full name: Index.range
Full name: Index.condition
module List
from Microsoft.FSharp.Collections
--------------------
type List<'T> =
| ( [] )
| ( :: ) of Head: 'T * Tail: 'T list
interface IEnumerable
interface IEnumerable<'T>
member Head : 'T
member IsEmpty : bool
member Item : index:int -> 'T with get
member Length : int
member Tail : 'T list
static member Cons : head:'T * tail:'T list -> 'T list
static member Empty : 'T list
Full name: Microsoft.FSharp.Collections.List<_>
Full name: Microsoft.FSharp.Collections.List.filter
Full name: Microsoft.FSharp.Collections.List.fold