# fsprojects/FSharp.Formatting

d42458c Apr 10, 2017
2 contributors

### Users who have contributed to this file

177 lines (151 sloc) 8.01 KB
 Literate sample

Literate sample

This file demonstrates how to write literate F# script files (*.fsx) that can be transformed into nice HTML using the literate.fsx script from the F# Formatting package.

As you can see, a comment starting with double asterisk is treated as part of the document and is transformed using Markdown, which means that you can use:

• Unordered or ordered lists
• Text formatting including bold and emphasis

And numerous other Markdown features.

Writing F# code

Code that is not inside comment will be formatted as a sample snippet (which also means that you can run it in Visual Studio or MonoDevelop).

1:
2:
3:
4:
5:
6:
/// The Hello World of functional languages!
let rec factorial x =
if x = 0 then 1
else x * (factorial (x - 1))

let f10 = factorial 10

Hiding code

If you want to include some code in the source code, but omit it from the output, you can use the hide command.

The value will be deffined in the F# code and so you can use it from other (visible) code and get correct tool tips:

1:

Moving code around

Sometimes, it is useful to first explain some code that has to be located at the end of the snippet (perhaps because it uses some definitions discussed in the middle). This can be done using include and define commands.

The following snippet gets correct tool tips, even though it uses laterFunction:

1:
2:
3:
let sample =
laterFunction()
|> printfn "Got: %s"

Then we can explain how laterFunction is defined:

1:
2:
let laterFunction() =
"Not very difficult, is it?"

This example covers pretty much all features that are currently implemented in literate.fsx, but feel free to fork the project on GitHub and add more features or report bugs!

Other features

The tool-tips also work for double-backtick identifiers. This might be useful to generate nice documents from tests:

1:
2:
let 1 + 1 should be equal to 2() =
1 + 1 = 2

val factorial : x:int -> int

Full name: Demo.factorial

The Hello World of functional languages!
val x : int
val f10 : int

Full name: Demo.f10
val hidden : int

Full name: Demo.hidden