{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 2 - First Steps\n", "\n", "### Glasgow Haskell Compiler\n", "\n", "- GHC is the leading implementation of Haskell, and comprises a compiler and interpreter;\n", "- The Interactive nature of the interpreter makes it well suited for teaching and prototyping;\n", "- GHC is freely available from [this website](http://www.haskell.org/platform)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Starting GHCI\n", "\n", "The interpreter can be started from the terminal command prompt $ by simply typing `ghci`\n", "\n", "```\n", "$ ghci\n", "GHCi, version X: http://www.haskell.org/ghc/\n", ":? for help\n", "Prelude>\n", "```\n", "\n", "\n", "The GHCi prompt > means that the interpreter is\n", "now ready to evaluate an expression." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For example, it can be used as a desktop calculator to evaluate simple numeric expresions:\n", "\n", "``` haskell\n", "> 2+3*4\n", "14\n", "> (2+3)*4\n", "20\n", "> sqrt (3^2 + 4^2)\n", "5.0\n", "```" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "14" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "2+3*4" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "20" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "(2+3)*4" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "sqrt (3^2 + 4^2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The Standard Prelude\n", "\n", "Haskell comes with a large number of standard library functions. In addition to the familiar numeric functions such as + and *, the library also provides many useful functions on lists.\n", "\n", "- Select the first element of a list:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "head [1,2,3,4,5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Remoe the first element from a list" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "[2,3,4,5]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "tail [1,2,3,4,5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Select the nth element of a list:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "[1,2,3,4,5] !! 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Select the first n elements of a list:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "[1,2,3]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "take 3 [1,2,3,4,5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Remove the first n elements from a list:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "[4,5]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "drop 3 [1,2,3,4,5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Calculate the length of a list:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "length [1,2,3,4,5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Calculate the sum of a list of numbers:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "15" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "sum [1,2,3,4,5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Calculate the product of a list of numbers:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "120" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "product [1,2,3,4,5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Append two lists:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "[1,2,3,4,5]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "[1,2,3] ++ [4,5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Reverse a list:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "[5,4,3,2,1]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "reverse [1,2,3,4,5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Function Application\n", "\n", "In mathematics, function application is denoted using parentheses, and multiplication is often denoted using juxtaposition or space.\n", "\n", "$$\n", "f(a,b) + c d\n", "$$\n", "\n", "> Apply the function f to a and b, and add the result to the product of c and d.\n", "\n", "In Haskell, function application is denoted using space, and multiplication is denoted using *.\n", "\n", "``` Haskell\n", "f a b + c*d\n", "```\n", "\n", "> As previously, but in Haskell syntax.\n", "\n", "Moreover, function application is assumed to have higher priority than all other operators.\n", "\n", "``` Haskell\n", "f a + b\n", "```\n", "\n", "> Means `(f a) + b`, rather than `f (a + b)`.\n", "\n", "Examples\n", "\n", "| Mathematics | Haskell |\n", "| --- | --- |\n", "| $f(x)$ | `f x` |\n", "| $f(x, y)$ | `f x y` |\n", "| $f(g(x))$ | `f (g x)` |\n", "| $f(x, g(y))$ | `f x (g y)` |\n", "| $f(x) g(y)$ | `f x * g y` |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Haskell Scripts\n", "\n", "- As well as the functions in the standard library, you can also define your own functions;\n", "- New functions are defined within a script, a text file comprising a sequence of definitions;\n", "- By convention, Haskell scripts usually have a .hs suffix on their filename. This is not mandatory, but is useful for identification purposes.\n", "\n", "### My First Script\n", "\n", "When developing a Haskell script, it is useful to keep two windows open, one running an editor for the script, and the other running GHCi.\n", "\n", "Start an editor, type in the following two function definitions, and save the script as test.hs:\n", "\n", "``` Haskell\n", "double x = x + x\n", "quadruple x = double (double x)\n", "```\n", "\n", "Leaving the editor open, in another window start up GHCi with the new script:\n", "\n", "``` bash\n", "ghci test.hs\n", "```\n", "Now both the standard library and the file test.hs are loaded, and functions from both can be used:\n", "\n", "``` Haskell\n", "> quadruple 10\n", "40\n", "> take (double 2) [1,2,3,4,5,6]\n", "[1,2,3,4]\n", "```\n", "\n", "Leaving GHCi open, return to the editor, add the following two definitions, and resave:\n", "\n", "``` Haskell\n", "factorial n = product [1..n]\n", "average ns = sum ns `div` length ns\n", "```\n", "\n", "> Note:\n", "> - div is enclose in back quotes, not forward;\n", "> - x 'f' y is just __syntatctic sugar__ for f x y\n", "\n", "GHCi does not automatically detect that the script has been changed, so a reload command must be executed before the new definitions can be used:\n", "\n", "``` haskell\n", "> :reload\n", "Reading file \"test.hs\"\n", "> factorial 10\n", "3628800\n", "> average [1,2,3,4,5]\n", "3\n", "```\n", "\n", "### Useful GHCi Commands\n", "\n", "| Command | Meaning |\n", "| --- | --- |\n", "| :load _name_ | load script _name_ |\n", "| :reload | reload current script |\n", "| :set editor _name_ | set editor to _name_|\n", "| : edit _name_ | edit script _name_ |\n", "| :edit | edit current script |\n", "| : type _expr_ | show type of _expr_ |\n", "| :? | show all commands |\n", "| :quit | quit GHCi |\n", "\n", "### Naming Requirements\n", "\n", "- Function and argument names must begin with a lower-case letter. For example:\n", "\n", "```\n", "myFun fun1 arg_2 x’\n", "\n", "```\n", "\n", "- By convention, list arguments usually have an s suffix on their name. For example:\n", "\n", "```\n", "xs ns nss\n", "```\n", "\n", "### The Layout Rule\n", "\n", "In a sequence of definition, each definition must begin in precisely the same column:\n", "\n", "> Correct\n", "\n", "```\n", "a = 10\n", "b = 20\n", "c = 30\n", "```\n", "\n", "> Incorrect\n", "\n", "```\n", "a = 10\n", " b = 20\n", "c = 30\n", "```\n", "\n", "> Incorrect\n", "\n", "```\n", " a = 10\n", "b = 20\n", " c = 30\n", "```\n", "\n", "The layout rule avoids the need for explicit syntax to indicate the grouping of definitions.\n", "\n", "> The following snippets are the same\n", "\n", "``` Haskell\n", "-- Implicit grouping\n", "a = b + c\n", " where\n", " b = 1\n", " c = 1\n", "d = a * 2\n", "```\n", "\n", "``` Haskell\n", "-- explicit grouping\n", "a = b + c\n", " where\n", " {b = 1;\n", " c = 1}\n", "d = a * 2\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercises\n", "\n", "1. Try out the Haskell scripts in this notebook using GHCi/notebook\n", "2. Fix the syntax errors in the program bellow, and test your solution using the notebook." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true } }, "outputs": [], "source": [ "N = a 'div' length xs\n", " where\n", " a = 10\n", " xs = [1,2,3,4,5]" ] } ], "metadata": { "kernelspec": { "display_name": "Haskell", "language": "haskell", "name": "haskell" }, "language_info": { "codemirror_mode": "ihaskell", "file_extension": ".hs", "name": "haskell", "pygments_lexer": "Haskell", "version": "8.6.5" } }, "nbformat": 4, "nbformat_minor": 4 }