-- First of all, we can evaluate simple expressions.
3 + 5
"Hello, " ++ "World!"
it1
-- Unlike in GHCi, we can have multi-line expressions.
concat [
"Hello",
", ",
"World!"
] :: String
thing :: String -> Int -> Int
thing "no" _ = 100
thing str int = int + length str
thing "no" 10
thing "ah" 10
print "What's going on?"
-- We can disable extensions.
:ext NoEmptyDataDecls
data Thing
-- And enable extensions.
:ext EmptyDataDecls
data Thing
-- Various data declarations work fine.
data One
= A String
| B Int
deriving Show
print [A "Hello", B 10]
-- We can look at types like in GHCi.
:ty 3 + 3
-- What is the Integral typeclass?
:info Integral
-- Only takes effect on later cells, so stick it in its own cell.
:opt no-pager
:info Integral
-- Results are printed as we go, even from a single expression.
import Control.Monad
import Control.Concurrent
forM_ [1..5] $ \x -> do
print x
threadDelay $ 200 * 1000
data Color = Red | Green | Blue
import IHaskell.Display
instance IHaskellDisplay Color where
display color = return $ Display [html code]
where
code = concat ["
Look!
"]
css Red = "red"
css Blue = "blue"
css Green = "green"
Red
Green
Blue
-- Aeson JSON data types are displayed nicely.
:ext OverloadedStrings
import Data.Aeson
data Coord = Coord { x :: Double, y :: Double }
instance ToJSON Coord where
toJSON (Coord x y) = object ["x" .= x, "y" .= y]
Null
Bool True
toJSON (Coord 3 2)
-- Turn off SVG output
:option no-svg
-- Small bits of HTML generated via Blaze are displayed.
import Prelude hiding (div, id)
import Text.Blaze.Html4.Strict hiding (map, style)
import Text.Blaze.Html4.Strict.Attributes
div ! style "color: red" $ do
p "This is an example of BlazeMarkup syntax."
b "Hello"
forM [1..5] $ \size -> do
let s = toValue $ size * 70
img ! src "https://www.google.com/images/srpr/logo11w.png" ! width s
-- We can draw diagrams, right in the notebook.
:extension NoMonomorphismRestriction FlexibleContexts TypeFamilies
import Diagrams.Prelude
-- By Brent Yorgey
-- Draw a Sierpinski triangle!
sierpinski 1 = eqTriangle 1
sierpinski n = s
===
(s ||| s) # centerX
where s = sierpinski (n-1)
-- The `diagram` function is used to display them in the notebook.
diagram $ sierpinski 4
# centerXY
# fc black
`atop` square 10
# fc white
-- We can draw small charts in the notebook.
-- This example is taken from the haskell-chart documentation.
import Graphics.Rendering.Chart
import Data.Default.Class
import Control.Lens
let values = [
("Mexico City" , 19.2, 0),
("Mumbai" , 12.9, 10),
("Sydney" , 4.3, 0),
("London" , 8.3, 0),
("New York" , 8.2, 25)]
pitem (s, v, o) = pitem_value .~ v
$ pitem_label .~ s
$ pitem_offset .~ o
$ def
-- Convert to a renderable in order to display it.
toRenderable
$ pie_title .~ "Relative Population"
$ pie_plot . pie_data .~ map pitem values
$ def
-- There is also hlint integration enabled by default.
-- If you write sketchy code, it will tell you:
f :: Int -> Int
f x = x + 1
-- Most warnings are orange...
f $ 3
do
return 3
-- If hlint annoys you, though, you can turn it off.
-- Note that this only takes effect in the next cell execution.
:opt no-lint
-- You could similarly use `:opt lint` to turn it back on.
f $ 3
:doc filterM
:hoogle :: a -> ST s (STRef s a)
:help
-- If your code isn't running fast enough, you can just put it into a module.
module A.B where
fib 0 = 1
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
-- The module is automatically imported unqualified.
print $ A.B.fib 20
print $ fib 20
f 3
import qualified A.B as Fib
Fib.fib 20
fib 20