{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "------------------------------------------------------------------------\n", "\n", "Haskell\n", "=======\n", "\n", "Syntactic elements\n", "------------------\n", "\n", "- A Haskell program (basically) consists of *expressions* and\n", " *declarations*\n", "\n", "Expressions\n", "-----------\n", "\n", "- Literals\n", "- Arithmetic expressions\n", "- Function applications\n", "- Conditionals\n", "- Identifiers (names)\n", "\n", "Declarations\n", "------------\n", "\n", "- Bind expressions to names\n", "- Use the operator `=`\n", "- Top-level bindings\n", "- Local bindings\n", "\n", "------------------------------------------------------------------------\n", "\n", "Expressions\n", "===========\n", "\n", "Literals\n", "--------" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "42\n", "\"Arthur Dent\"\n", "[1, 2, 3]\n", "(42, \"fortytwo\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Evaluates to the value itself\n", "\n", "Arithmetic\n", "----------" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(1 + 2) * 3\n", "3.14 * 2 * 17\n", "3^2 + 4^2 == 5^2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Evaluates to the result of the operation\n", "\n", "Function invocation\n", "-------------------" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "max 23 42\n", "length [1, 2, 3]\n", "triple 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Evaluates to the function value returned\n", "\n", "------------------------------------------------------------------------\n", "\n", "Declarations\n", "============\n", "\n", "Bind expressions\n", "----------------" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "answer = 42\n", "crew = [\"Kirk\", \"Spock\", \"McCoy\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Value `42` is bound to name `answer`\n", "- List of crew members is bound to name `crew`\n", "- Identifiers `answer` and `crew` can be used in other expression\n", "- They evaluate to the bound values\n", "\n", "Bind functions\n", "--------------" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "triple x = x + x + x\n", "hello name = \"Hello \" ++ name" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Functions of one parameter are bound to names `triple` and `hello`\n", "- Functions `triple` and `hello` can be invoked in other expressions\n", "- They evaluate to the computed function value\n", "\n", "------------------------------------------------------------------------\n", "\n", "Special expression syntax\n", "=========================\n", "\n", "Conditional `if` expression\n", "---------------------------" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 1\n", "if x < 2 then \"Smaller than two\" else \"Bigger than 2\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`if` expression in a function declaration\n", "-----------------------------------------" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "max a b = if a > b then a else b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "------------------------------------------------------------------------\n", "\n", "Function declaration\n", "====================\n", "\n", "3 Examples\n", "----------\n", "\n", "``` {.haskell}\n", "seven = 7\n", "triple x = x + x + x\n", "max a b = if a > b then a else b\n", "```\n", "\n", "Components\n", "----------\n", "\n", "- Function names (`seven`, `triple`, `max`)\n", "- Zero or more function parameters (`a`, `b`, `x`)\n", "- Function expression with identifiers bound to the function\n", " parameters\n", "\n", "Function and parameter names\n", "----------------------------\n", "\n", "- Must start with a *lower-case* character\n", "- Consist of *alpha-numeric* characters only\n", "\n", "------------------------------------------------------------------------\n", "\n", "Function example\n", "================\n", "\n", "Consider these expressions\n", "--------------------------" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "label": "Haskell" }, "outputs": [], "source": [ "3.141 * 5 * 5\n", "3.141 * 42 * 42\n", "3.141 * 17 * 17" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try to abstract that into a function\n", "------------------------------------" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "label": "Haskell" }, "outputs": [], "source": [ "circleArea r = 3.141 * r ^ 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "------------------------------------------------------------------------\n", "\n", "Recursive operations on lists\n", "=============================\n", "\n", "Task: Calculate the arithmetic mean of a list of numbers\n", "--------------------------------------------------------\n", "\n", "$$\n", "\\bar{x} = \\frac{1}{n}\\left (\\sum_{i=1}^n{x_i}\\right ) = \\frac{x_1+x_2+\\cdots +x_n}{n}\n", "$$\n", "\n", "The mean function\n", "-----------------" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "label": "Haskell" }, "outputs": [], "source": [ "mean list = totalSum list / count list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Now implement `count` and `totalSum`]{.fragment}\n", "\n", "------------------------------------------------------------------------\n", "\n", "Recursive operations on lists\n", "=============================\n", "\n", "Task: Calculate the number of elements in a list\n", "------------------------------------------------\n", "\n", "The function `count`\n", "--------------------" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "label": "Haskell" }, "outputs": [], "source": [ "count list =\n", " if null list\n", " then 0\n", " else 1 + count (tail list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "------------------------------------------------------------------------\n", "\n", "Recursive operations on lists\n", "=============================\n", "\n", "Task: Calculate the sum of elements in a list of numbers\n", "--------------------------------------------------------\n", "\n", "The function `totalSum`\n", "-----------------------" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "label": "Haskell" }, "outputs": [], "source": [ "totalSum list =\n", " if null list \n", " then 0\n", " else head list + totalSum (tail list)" ] } ], "nbformat": 4, "nbformat_minor": 5, "metadata": { "language_info": { "name": "haskell", "file_extension": ".hs", "pygments_lexer": "Haskell", "version": "8.6.5", "codemirror_mode": "ihaskell" }, "kernelspec": { "name": "haskell", "display_name": "Haskell", "language": "haskell" } } }