{
"metadata": {
"language": "haskell",
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"-- First of all, we can evaluate simple expressions.\n",
"3 + 5\n",
"\"Hello, \" ++ \"World!\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
"8"
]
},
{
"metadata": {},
"output_type": "display_data",
"text": [
"\"Hello, World!\""
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"-- Unlike in GHCi, we can have multi-line expressions.\n",
"concat [\n",
" \"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"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
"\"Hello,, World!\""
]
},
{
"metadata": {},
"output_type": "display_data",
"text": [
"100"
]
},
{
"metadata": {},
"output_type": "display_data",
"text": [
"12"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"-- We can also do IO.\n",
"print \"What's going on?\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
"\"What's going on?\""
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"-- We can disable extensions.\n",
":ext NoEmptyDataDecls\n",
"data Thing"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"`Thing' has no constructors
(-XEmptyDataDecls permits this)"
],
"metadata": {},
"output_type": "display_data",
"text": [
"`Thing' has no constructors\n",
" (-XEmptyDataDecls permits this)"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"-- And enable extensions.\n",
":ext EmptyDataDecls\n",
"data Thing"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"-- Various data declarations work fine.\n",
"data One\n",
" = A String\n",
" | B Int\n",
" deriving Show\n",
"\n",
"print [A \"Hello\", B 10]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
"[A \"Hello\",B 10]"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"-- We can look at types like in GHCi.\n",
":ty 3 + 3"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"forall a. Num a => a"
],
"metadata": {},
"output_type": "display_data",
"text": [
"forall a. Num a => a"
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"-- And we can inspect info of things!\n",
":info Integral"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
"class (Real a, Enum a) => Integral a where\n",
" quot :: a -> a -> a\n",
" rem :: a -> a -> a\n",
" div :: a -> a -> a\n",
" mod :: a -> a -> a\n",
" quotRem :: a -> a -> (a, a)\n",
" divMod :: a -> a -> (a, a)\n",
" toInteger :: a -> Integer\n",
" \t-- Defined in `GHC.Real'\n",
"instance Integral Integer -- Defined in `GHC.Real'\n",
"instance Integral Int -- Defined in `GHC.Real'"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"-- Results are printed as we go, even from a single expression.\n",
"import Control.Monad\n",
"import Control.Concurrent\n",
"\n",
"forM_ [1..5] $ \\x -> do\n",
" print x\n",
" threadDelay $ 100 * 1000"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
"1\n",
"2\n",
"3\n",
"4\n",
"5"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"-- We can display Maybes fancily for Show-able types.\n",
"Just ()\n",
"Nothing\n",
"\n",
"-- But it dies if it's not showable.\n",
"data NoShow = X Int\n",
"Just (X 3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"Just()"
],
"metadata": {},
"output_type": "display_data",
"text": [
"Just ()"
]
},
{
"html": [
"Nothing"
],
"metadata": {},
"output_type": "display_data",
"text": [
"Nothing"
]
},
{
"html": [
"No instance for (Show NoShow)
arising from a use of `print'
Possible fix:
add an instance declaration for (Show NoShow)"
],
"metadata": {},
"output_type": "display_data",
"text": [
"No instance for (Show NoShow)\n",
" arising from a use of `print'\n",
"Possible fix:\n",
" add an instance declaration for (Show NoShow)"
]
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"-- Aeson JSON data types are displayed nicely.\n",
":ext OverloadedStrings\n",
"\n",
"import Data.Aeson\n",
"\n",
"data Coord = Coord { x :: Double, y :: Double }\n",
"instance ToJSON Coord where\n",
" toJSON (Coord x y) = object [\"x\" .= x, \"y\" .= y]\n",
"\n",
"Null\n",
"Bool True\n",
"toJSON (Coord 3 2)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"
\n", " This is an example of BlazeMarkup syntax.\n", "
\n", " \n", " Hello\n", " \n", "\n", " This is an example of BlazeMarkup syntax.\n", "
\n", " \n", " Hello\n", " \n", " Redundant $ | |
Found: f $ 3\n",
" | Why Not: f 3\n",
" |
Use print | |
Found: putStrLn (show 3)\n",
" | Why Not: print 3\n",
" |
Redundant do | |
Found: do return 3\n",
" | Why Not: return 3\n",
" |