{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CS 201: Introduction to Racket\n",
    "\n",
    "<script language=\"JavaScript\">\n",
    "    document.write(\"Last modified: \" + document.lastModified)\n",
    "</script>\n",
    "\n",
    "Define a list comprising 10 digits                                             \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "(define digits '(0 1 2 3 4 5 6 7 8 9)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<code>'(0 1 2 3 4 5 6 7 8 9)</code>"
      ],
      "text/plain": [
       "'(0 1 2 3 4 5 6 7 8 9)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "digits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define a function, <code>num-to-word</code>, which takes a single digit\n",
    "and returns the corresponding English word  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "(define (num-to-word num)                                                         \n",
    "  (if (> num 9) #f                                                                \n",
    "      (let ((numbers '(\"zero\" \"one\" \"two\" \"three\" \"four\" \"five\" \"six\" \"seven\" \"ei\\\n",
    "ght\" \"nine\")))                                                                    \n",
    "        (list-ref numbers num))))   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<code>\"eight\"</code>"
      ],
      "text/plain": [
       "\"eight\""
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(num-to-word 8)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define a function, <code>word-to-num</code>, which is the inverse of <code>num-to-word</code>.\n",
    "Note that we define a sub-function within the function itself.  The sub-function, <code>find-index</code>, is recursive.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "(define (word-to-num word)                                                        \n",
    "  (let ((numbers '(\"zero\" \"one\" \"two\" \"three\" \"four\" \"five\" \"six\" \"seven\" \"eight\" \"nine\")))                                                                        \n",
    "    (define (find-index lst n)                                                    \n",
    "      (cond ((null? lst) #f)                                                      \n",
    "            ((equal? (car lst) word) n)                                         \n",
    "            (else                                                                 \n",
    "             (find-index (cdr lst) (+ n 1)))))                                   \n",
    "    (find-index numbers 0)))        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<code>6</code>"
      ],
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(word-to-num \"six\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Test out the functions.  <code>map</code> is a function that applies a function to elements of a list, returning a new list comprising the results from each function application. The term <b>map</b> comes from math, not geography.                                            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "(define x (map num-to-word digits))                                               "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<code>'(\"zero\" \"one\" \"two\" \"three\" \"four\" \"five\" \"six\" \"seven\" \"eight\" \"nine\")</code>"
      ],
      "text/plain": [
       "'(\"zero\" \"one\" \"two\" \"three\" \"four\" \"five\" \"six\" \"seven\" \"eight\" \"nine\")"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<tt>x</tt> now contains a list of the words for the digits.  We will now put the words in alphabetical order using <code>sort</code> and the comparison predicate <code>string&lt;?</code> which returns true if the first string argument is alphabetically prior to the second string argument."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "(define y (sort x string<?))                                          "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<tt>y</tt> now contains a list of the digits in alphabetical order."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<code>'(\"eight\" \"five\" \"four\" \"nine\" \"one\" \"seven\" \"six\" \"three\" \"two\" \"zero\")</code>"
      ],
      "text/plain": [
       "'(\"eight\" \"five\" \"four\" \"nine\" \"one\" \"seven\" \"six\" \"three\" \"two\" \"zero\")"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We next convert the words in <tt>y</tt> back to numerals using our <code>word-to-num</code> function and our new friend <code>map</code>."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "(define z (map word-to-num y)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<code>'(8 5 4 9 1 7 6 3 2 0)</code>"
      ],
      "text/plain": [
       "'(8 5 4 9 1 7 6 3 2 0)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This list should look familiar."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Racket",
   "language": "racket",
   "name": "racket"
  },
  "language_info": {
   "codemirror_mode": "scheme",
   "file_extension": ".rkt",
   "mimetype": "text/x-racket",
   "name": "Racket",
   "pygments_lexer": "racket",
   "version": "7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
