{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b9a85d06",
   "metadata": {},
   "source": [
    "# Jupyter kernels\n",
    "\n",
    "A Jupyter Notebook can utilise any program kernel that implements the [Jupyter messaging protocol](http://jupyter-client.readthedocs.io/en/latest/messaging.html) for executing code.\n",
    "There are kernels available for [Python](http://ipython.org/notebook.html), [Julia](https://github.com/JuliaLang/IJulia.jl), [Ruby](https://github.com/minad/iruby), [Haskell](https://github.com/gibiansky/IHaskell) and [many other languages](https://github.com/jupyter/jupyter/wiki/Jupyter-kernels).\n",
    "\n",
    "In this notebook we demonstrate executing code with the [Coconut Programming Language](http://coconut-lang.org), a variant of Python built for *simple, elegant, Pythonic functional programming*.\n",
    "\n",
    "In the first example we will define a recursive `factorial` function, a fundamentally functional approach that doesn’t involve any state changes or loops:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c179416c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "def factorial(n):\n",
    "    \"\"\"Compute n! where n is an integer >= 0.\"\"\"\n",
    "    case n:\n",
    "        match 0:\n",
    "            return 1\n",
    "        match x is int if x > 0:\n",
    "            return x * factorial(x-1)\n",
    "    else:\n",
    "        raise TypeError(\"the argument to factorial must be an integer >= 0\")\n",
    "\n",
    "3 |> factorial |> print"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b33ea4e8",
   "metadata": {},
   "source": [
    "Although this example is very basic, pattern-matching is both one of Coconut’s most powerful and most complicated features.\n",
    "\n",
    "In the second example, we implement the quick sort algorithm.\n",
    "This quick_sort algorithm works using a bunch of new constructs:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "aee9a132",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "def quick_sort(l):\n",
    "    \"\"\"Sort the input iterator using the quick sort algorithm.\"\"\"\n",
    "    match [head] :: tail in l:\n",
    "        tail = reiterable(tail)\n",
    "        yield from quick_sort(left) :: [head] :: quick_sort(right) where:\n",
    "            left = (x for x in tail if x < head)\n",
    "            right = (x for x in tail if x >= head)\n",
    "    # By yielding nothing if the match falls through, we implicitly return an empty iterator.\n",
    "\n",
    "[3,0,4,2,1] |> quick_sort |> list |> print"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ef4e1a0",
   "metadata": {},
   "source": [
    "Finally, we see that exceptions are raised as one would expect:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6524faae",
   "metadata": {
    "tags": [
     "raises-exception"
    ]
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'x' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-3-151148cf3fd5>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m  \u001b[0;31m# line 1: x\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'x' is not defined"
     ]
    }
   ],
   "source": [
    "x"
   ]
  }
 ],
 "metadata": {
  "file_format": "mystnb",
  "kernelspec": {
   "display_name": "coconut",
   "name": "coconut"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "python",
    "version": 3
   },
   "file_extension": ".coco",
   "mimetype": "text/x-python3",
   "name": "coconut",
   "pygments_lexer": "coconut",
   "version": "1.4.3"
  },
  "source_map": [
   5,
   16,
   28,
   35,
   46,
   50
  ]
 },
 "nbformat": 4,
 "nbformat_minor": 5
}