"Hello, and welcome to the **IHaskell Notebook**. IHaskell Notebook is similar to an interactive shell along the lines of GHCi. However, it is much more powerful, and provides features such as syntax highlighting, autocompletion, multi-line input cells, integrated documentation, rich output visualization, and more. In this notebook, I'd like to demonstrate many of the awesome features IHaskell provides.\n",
"\n",
"Hello, and welcome to the IHaskell Notebook.\n",
"IHaskell is implemented as a language kernel for the [IPython](http://ipython.org) project, which means that although the entire thing is written only in Haskell, we get a beautiful notebook interface practically for free.\n",
"We can start with very simple Haskell expressions:"
]
},
{
...
...
@@ -46,8 +49,43 @@
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As you can see, each input cell get an execution number. The first input cell is labeled `In [1]`. Just like in GHCi, the output of the last executed statement or expression is available via the `it` variable - however, in addition, the output of the $n$th cell is available via the `itN` variable. For example, if we wanted to see what the first cell printed, we can go ahead and output that:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"it1"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
"\"Hello, World!\""
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In addition to simple code cells such as the ones you see, you can also have other types of cells. All of this inline text, for instance, is written using Markdown cells, which support the majority of Github markdown syntax. This lets you embed images and formatting and arbitrary HTML interspersed with your Haskell code. In addition, you can export these notebooks into HTML or even as presentations using `reveal.js`. \n",
"\n",
"Alright, back to code. Let's do something slightly fancier:"
]
},
{
"cell_type": "code",
"collapsed": false,
...
...
@@ -57,17 +95,7 @@
" \"Hello,\",\n",
" \", \",\n",
" \"World!\"\n",
" ] :: String\n",
" \n",
"-- We can also have normal Haskell declarations, without `let`.\n",
"-- As long as you group type signatures and declarations together,\n",
"-- you can use pattern matching and add type signatures.\n",
"thing :: String -> Int -> Int\n",
"thing \"no\" _ = 100\n",
"thing str int = int + length str\n",
"\n",
"thing \"no\" 10\n",
"thing \"ah\" 10"
" ] :: String"
],
"language": "python",
"metadata": {},
...
...
@@ -78,7 +106,31 @@
"text": [
"\"Hello,, World!\""
]
},
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In addition to multi-line expressions, IHaskell supports most things that you could put in a standard Haskell file. For example, we can have function bindings without the `let` that GHCi requires. (As long as you group type signatures and their corresponding declarations together, you can use pattern matching and put signatures on your top-level declarations!)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"thing :: String -> Int -> Int\n",
"thing \"no\" _ = 100\n",
"thing str int = int + length str\n",
"\n",
"thing \"no\" 10\n",
"thing \"ah\" 10"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
...
...
@@ -94,13 +146,19 @@
]
}
],
"prompt_number": 3
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So far we've just looked at pure functions, but nothing is stopping us from doing IO."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"-- We can also do IO.\n",
"print \"What's going on?\""
],
"language": "python",
...
...
@@ -114,7 +172,14 @@
]
}
],
"prompt_number": 4
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"IHaskell supports most GHC extensions via the `:extension` directive (or any shorthand thereof)."
]
},
{
"cell_type": "code",
...
...
@@ -139,7 +204,7 @@
]
}
],
"prompt_number": 5
"prompt_number": 7
},
{
"cell_type": "code",
...
...
@@ -152,7 +217,14 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Data declarations do pretty much what you expect, and work fine on multiple lines. If a declaration turns out to be not quite what you wanted, you can just go back, edit it, and re-evaluate the code cell."
]
},
{
"cell_type": "code",
...
...
@@ -177,7 +249,14 @@
]
}
],
"prompt_number": 7
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Although this doesn't hold everywhere, we've tried to keep IHaskell relatively similar to GHCi in terms of naming. So, just like in GHCi, you can inspect types with `:type` (or shorthands):"
]
},
{
"cell_type": "code",
...
...
@@ -200,13 +279,20 @@
]
}
],
"prompt_number": 8
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The same goes for the `:info` command. However, unlike GHCi, which simply prints info, the IHaskell notebook brings up a separate pane."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"-- And we can inspect info of things!\n",
"-- What is the Integral typeclass?\n",
":info Integral"
],
"language": "python",
...
...
@@ -217,7 +303,16 @@
"output_type": "display_data"
}
],
"prompt_number": 9
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you're looking at this notebook after it's been exported to HTML, you won't be able to see this interactive pane. However, it looks approximately like this:\n",