"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",
"We can now write slightly more complicated scripts."
]
},
{
...
...
@@ -324,7 +331,7 @@
"\n",
"forM_ [1..5] $ \\x -> do\n",
" print x\n",
" threadDelay $ 100 * 1000"
" threadDelay $ 200 * 1000"
],
"language": "python",
"metadata": {},
...
...
@@ -341,7 +348,110 @@
]
}
],
"prompt_number": 10
"prompt_number": 13
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is where the similarities with GHCi end, and the particularly shiny features of IHaskell begin.\n",
"\n",
"Although looking at text outputs is often enough, there are many times where we really want a richer output. Suppose we have a custom data type for color:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"data Color = Red | Green | Blue"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If we were playing around with designing GUI applications for color-blind users, we might want to actually *see* these colors, instead of just seeing the text \"Red\", \"Green\", and \"Blue\" when we are debugging.\n",
"\n",
"IHaskell lets you define a custom display mechanism for any data type via its `IHaskellDisplay` typeclass. Since you can use IHaskell in console mode as well as notebook mode, you can provide a list of display outputs for any data type, and the frontend will simply choose the best one. Here's how you would implement a very simple display mechanism for this `Color` data type:"
"The `DisplayData` type has several constructors which let you display your data as plain text, HTML, images (SVG, PNG, JPG), or even as LaTeX code.\n",
"\n",
"In order to ship an extension for IHaskell, simply create a package named `ihaskell-thing` with a module named `IHaskell.Display.Thing`. As long as `ihaskell-thing` is installed, IHaskell will detect and use it automatically.\n",
"\n",
"A number of packages already exist, which we can briefly look at. First, in `ihaskell-basic`, we have very simple displays for data types from `Prelude`."
]
},
{
"cell_type": "code",
...
...
@@ -397,7 +507,14 @@
]
}
],
"prompt_number": 11
"prompt_number": 18
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `ihaskell-aeson` package adds a display for [Aeson](http://hackage.haskell.org/package/aeson) JSON `Value` types. These are automatically syntax highlighted and formatted nicely."
]
},
{
"cell_type": "code",
...
...
@@ -456,7 +573,14 @@
]
}
],
"prompt_number": 12
"prompt_number": 19
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `ihaskell-blaze` package lets you play around with HTML straight from within IHaskell using the [Blaze](http://jaspervdj.be/blaze/tutorial.html) library."
]
},
{
"cell_type": "code",
...
...
@@ -521,7 +645,14 @@
]
}
],
"prompt_number": 13
"prompt_number": 21
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `ihaskell-diagrams` package allows you to experiment with the [diagrams](http://projects.haskell.org/diagrams/) package. It requires the Cairo backend."
]
},
{
"cell_type": "code",
...
...
@@ -590,7 +721,14 @@
]
}
],
"prompt_number": 14
"prompt_number": 22
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Just like with Diagrams, `ihaskell-chart` allows you to use the [Chart](https://github.com/timbod7/haskell-chart/wiki) library for plotting from within IHaskell."
]
},
{
"cell_type": "code",
...
...
@@ -833,7 +971,83 @@
]
}
],
"prompt_number": 15
"prompt_number": 23
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By default, both `ihaskell-diagrams` and `ihaskell-chart` use SVG displays. However, the notebook does not support interactive resizing for SVG image. However, if you use `:set no-svg`, all SVG outputs will instead be shown as PNG images, and they can be resized easily."
"Finally, the `ihaskell-magic` chart uses the [magic](http://hackage.haskell.org/package/magic) library (bindings to `libmagic`) to create a display mechanism for `ByteString`s. If you try to load an image, for instance, you will see that image immediately in the notebook."