Skip to content

Instantly share code, notes, and snippets.

@agoose77
Created August 20, 2019 16:12
Show Gist options
  • Save agoose77/d51e99528e63d0442b119ab12bc2a246 to your computer and use it in GitHub Desktop.
Save agoose77/d51e99528e63d0442b119ab12bc2a246 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# General introduction"
]a
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Definitions"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>6</code>"
],
"text/plain": [
"6"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; Add 2 to 4\n",
"(+ 2 4)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>4</code>"
],
"text/plain": [
"4"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(define x 4)\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>16</code>"
],
"text/plain": [
"16"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(define (squared x) (* x x))\n",
"(squared 4)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>14</code>"
],
"text/plain": [
"14"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; Let is effectively an expression statement e.g x:=5 binds x to 5 & returns x\n",
";; (let ([id expr] [id expr] ...) body-expr)\n",
"(let ([x 5] [y 9]) (+ x y))"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>14</code>"
],
"text/plain": [
"14"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; let* can bind to identifiers earlier in the (list?) of expressions\n",
";; Here, x=5, y=x+4, return x+y\n",
"(let* ([x 5] [y (+ x 4)]) (+ x y))"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>16</code>"
],
"text/plain": [
"16"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; Lambdas are anonymous functions, and the functional define syntax is shorthand for a named lambda\n",
"(define squared (lambda (x) (* x x)))\n",
"(squared 4)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'(1 2 3 4)</code>"
],
"text/plain": [
"'(1 2 3 4)"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; List of numbers\n",
"(define data (list 1 2 3 4))\n",
"data"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'(1 4 9 16)</code>"
],
"text/plain": [
"'(1 4 9 16)"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; Compute the squares\n",
"(map squared data)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>3</code>"
],
"text/plain": [
"3"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(define (add a b) (+ a b))\n",
"(apply add (list 1 2))"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>3</code>"
],
"text/plain": [
"3"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; Use ` to defer evaluation of already-evaluated list\n",
"(apply add `(1 2))"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"(require racket/class)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
";; Define a class. Class names end in % by convention. Root class is builtin object%\n",
"(define relic%\n",
" (class object%\n",
" (super-new); superclass initialization, required. Position in definition depends upon whether other fields invoke superclass methods\n",
" (init age)\n",
" (define current-age age) ; Can't get init args outside of instantiation (e.g during method calls)\n",
" (define/public (get-age) current-age) ; Method getting current age\n",
" )\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>(object:relic% 1000)</code>"
],
"text/plain": [
"(object:relic% 1000)"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; Create a class instance, passing the age field by name\n",
"(define some-relic (new relic% [age 1000]))\n",
"some_relic"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>1000</code>"
],
"text/plain": [
"1000"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; Invoke a method using message passing\n",
"(send some-relic get-age)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"(define named-relic%\n",
" (class relic%\n",
" (init name age)\n",
" (super-new [age age])\n",
" (define current-name name)\n",
" (define/public (get-name) current-name)\n",
" \n",
" )\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 127,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'(\"Quran\" 1000)</code>"
],
"text/plain": [
"'(\"Quran\" 1000)"
]
},
"execution_count": 127,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(define some-named-relic (new named-relic% [name \"Quran\"] [age 1000]))\n",
"(list (send some-named-relic get-name) (send some-named-relic get-age))"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>(object:named-relic% 1200 \"Bible\")</code>"
],
"text/plain": [
"(object:named-relic% 1200 \"Bible\")"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; Classes can be constructed by positional args\n",
"(define some-other-named-relic (instantiate named-relic% [\"Bible\" 1200]))\n",
"some-other-named-relic"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>(object:named-relic% 1200 \"Bible round\")</code>"
],
"text/plain": [
"(object:named-relic% 1200 \"Bible round\")"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; Square and round brackets are interchangeable\n",
"(define some-other-named-relic-round (instantiate named-relic% (\"Bible round\" 1200)))\n",
"some-other-named-relic-round"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {},
"outputs": [],
"source": [
";; Fields can be set\n",
"(define point%\n",
" (class object%\n",
" (init x y)\n",
" (super-new)\n",
" (define x_ x)\n",
" (define y_ y)\n",
" (define/public (get-x) x_)\n",
" (define/public (get-y) y_)\n",
" \n",
" (define/public (set-x x) (set! x_ x))\n",
" (define/public (set-y y) (set! y_ y))\n",
" )\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>(object:point% 1.0 2.0)</code>"
],
"text/plain": [
"(object:point% 1.0 2.0)"
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(define p1 (new point% [x 1.0] [y 2.0]))\n",
"p1"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>1.0</code>"
],
"text/plain": [
"1.0"
]
},
"execution_count": 86,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(send p1 get-x)"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>9</code>"
],
"text/plain": [
"9"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(send p1 set-x 9)\n",
"(send p1 get-x)"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>10</code>"
],
"text/plain": [
"10"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; Set p1.x to p1.x + 1\n",
"(send p1 set-x (+ (send p1 get-x) 1))\n",
"(send p1 get-x)"
]
},
{
"cell_type": "code",
"execution_count": 122,
"metadata": {},
"outputs": [],
"source": [
"(require racket/math)\n",
"(define (euclidean-distance x y) (sqrt (+ (sqr x) (sqr y))))\n",
"(define vector%\n",
" (class point%\n",
" (super-new)\n",
" (inherit get-x get-y); non-method defines are assumed private fields, so we inherit get-x and get-y to permit calling them like (get-x)\n",
" (define/public (get-length) (euclidean-distance (get-x) (get-y))) \n",
" ;; This is the same as:\n",
" (define/public (get-length2) (euclidean-distance (send this get-x) (send this get-y))) \n",
" )\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 128,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'(5.0 5.0)</code>"
],
"text/plain": [
"'(5.0 5.0)"
]
},
"execution_count": 128,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(define v1 (new vector% [x 3.0] [y 4.0]))\n",
"(list (send v1 get-length) (send v1 get-length2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## IO"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x \"y\" ~"
]
}
],
"source": [
";; NB string formatting\n",
"(define x \"x\")\n",
"(define y \"y\")\n",
"\n",
"(printf \"~a ~v ~~\" x y)"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"display x\n"
]
}
],
"source": [
";; ~a refers to display:\n",
"(display \"display \")\n",
"(displayln \"x\")"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\"print \"\"x\"\n"
]
}
],
"source": [
";; ~v refers to print:\n",
"(print \"print \")\n",
"(println \"x\")"
]
},
{
"cell_type": "code",
"execution_count": 176,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code><img style=\"display: inline; vertical-align: baseline; padding: 0pt; margin: 0pt; border: 0pt\" src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAcElEQVQYlY3QoRHCUBBF0ZOv\r\n",
"ImIpJj4yJUSE0igkFhiwVIFMClgEKzKI8N/MNfuu2ScioMGEBe9kyVuTjoILHhhxSkY8sytw\r\n",
"xh1tRNiDNrsZbhh+pZ084AobugOxw1p8E/6k4IX+wOnTqX+mbp7awT8daWL0QpmoRQAAAABJ\r\n",
"RU5ErkJggg==\r\n",
"\"/></code>"
],
"text/plain": [
"(pict '(prog #<procedure:...rivate/utils.rkt:282:8> 10) 10 10 10 0 '() #f #f)"
]
},
"execution_count": 176,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"(circle 10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Conditions"
]
},
{
"cell_type": "code",
"execution_count": 170,
"metadata": {},
"outputs": [],
"source": [
"(define x 100)"
]
},
{
"cell_type": "code",
"execution_count": 173,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>#&lt;eof&gt;</code>"
],
"text/plain": [
"#<eof>"
]
},
"execution_count": 173,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(read-line)"
]
},
{
"cell_type": "code",
"execution_count": 172,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x is positive!\n"
]
}
],
"source": [
"(if (positive? x)\n",
" (displayln \"x is positive!\")\n",
" (displayln \"x is not positive!\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Loops"
]
},
{
"cell_type": "code",
"execution_count": 138,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n"
]
}
],
"source": [
"(for ([i `(0 1 2 3)])\n",
" (println i))"
]
},
{
"cell_type": "code",
"execution_count": 139,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n"
]
}
],
"source": [
";; Simarly, using an in-range shorthand\n",
"(for ([i 4])\n",
" (println i))"
]
},
{
"cell_type": "code",
"execution_count": 140,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n"
]
}
],
"source": [
";; Simarly, using in-range\n",
"(for ([i (in-range 4)])\n",
" (println i))"
]
},
{
"cell_type": "code",
"execution_count": 141,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n",
"6\n",
"7\n",
"8\n",
"9\n"
]
}
],
"source": [
";; Simarly, using in-range\n",
"(for ([i (in-range 5 10)])\n",
" (println i))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x=0. y=0\n",
"x=1. y=1\n",
"x=2. y=2\n"
]
}
],
"source": [
";; Parallel execution, terminates for shortest\n",
"(for ([x 4] [y 3])\n",
" (printf \"x=~a. y=~a\\n\" x y))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x=0. y=0\n",
"x=0. y=1\n",
"x=0. y=2\n",
"x=1. y=0\n",
"x=1. y=1\n",
"x=1. y=2\n",
"x=2. y=0\n",
"x=2. y=1\n",
"x=2. y=2\n",
"x=3. y=0\n",
"x=3. y=1\n",
"x=3. y=2\n"
]
}
],
"source": [
";; Sequences can instead be nested using for*\n",
"(for* ([x 4] [y 3])\n",
" (printf \"x=~a. y=~a\\n\" x y))"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x=0. y=0\n",
"x=0. y=1\n",
"x=0. y=2\n",
"x=2. y=0\n",
"x=2. y=1\n",
"x=2. y=2\n",
"x=3. y=0\n",
"x=3. y=1\n",
"x=3. y=2\n"
]
}
],
"source": [
";; Only evaluate body when condition holds true\n",
"(for* ([x 4] [y 3] #:when (not (equal? x 1)))\n",
" (printf \"x=~a. y=~a\\n\" x y))"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x=0. y=0\n",
"x=0. y=1\n",
"x=0. y=2\n",
"x=2. y=0\n",
"x=2. y=1\n",
"x=2. y=2\n",
"x=3. y=0\n",
"x=3. y=1\n",
"x=3. y=2\n"
]
}
],
"source": [
";; Can also use \"(when ...)\" which evaluates to #<void> if the condition is false\n",
"(for* ([x 4] [y 3])\n",
" (when (not (equal? x 1)) \n",
" (printf \"x=~a. y=~a\\n\" x y)\n",
" )\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x=0. y=0\n",
"x=0. y=1\n",
"x=0. y=2\n",
"x=2. y=0\n",
"x=2. y=1\n",
"x=2. y=2\n",
"x=3. y=0\n",
"x=3. y=1\n",
"x=3. y=2\n",
"vs\n",
"x=0. y=0\n",
"x=2. y=2\n"
]
}
],
"source": [
";; Bindings separated by \"#:when\" clauses become *nested*, even in case of \"for\"\n",
"(for ([x 4] #:when (not (equal? x 1)) [y 3] )\n",
" (printf \"x=~a. y=~a\\n\" x y))\n",
"\n",
"(displayln \"vs\")\n",
"\n",
"(for ([x 4] [y 3] #:when (not (equal? x 1)))\n",
" (printf \"x=~a. y=~a\\n\" x y))"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'(0 1 2 3)</code>"
],
"text/plain": [
"'(0 1 2 3)"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; List comprehensions:\n",
"(for/list ([i 4]) i)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'((0 0) (0 1) (0 2) (1 0) (1 1) (1 2) (2 0) (2 1) (2 2) (3 0) (3 1) (3 2))</code>"
],
"text/plain": [
"'((0 0) (0 1) (0 2) (1 0) (1 1) (1 2) (2 0) (2 1) (2 2) (3 0) (3 1) (3 2))"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(for*/list ([i 4] [j 3]) (list i j))"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>#f</code>"
],
"text/plain": [
"#f"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; Equivalents to python's all() over a list comprehension\n",
"(for/and ([i 4]) (even? i))"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>#t</code>"
],
"text/plain": [
"#t"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; Equivalents to python's any() over a list comprehension\n",
"(for/or ([i 4]) (even? i))"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>#t</code>"
],
"text/plain": [
"#t"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; Showing the range semantics again\n",
"(for/and ([i (in-range 2 6 2)]) (even? i));; Showing the range semantics again\n",
"(for/and ([i (in-range 2 6 2)]) (or (even? i) (println i)))"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n"
]
}
],
"source": [
";; Endless loop (like itertools.count), using break to stop loop\n",
"(for ([i (in-naturals)]\n",
" #:break (equal? i 10))\n",
" (println i)\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 160,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'(1 1.4142135623730951 1.7320508075688772 2)</code>"
],
"text/plain": [
"'(1 1.4142135623730951 1.7320508075688772 2)"
]
},
"execution_count": 160,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(map sqrt (list 1 2 3 4))"
]
},
{
"cell_type": "code",
"execution_count": 163,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'(2 3 4 5)</code>"
],
"text/plain": [
"'(2 3 4 5)"
]
},
"execution_count": 163,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(map (lambda (x) (+ x 1)) \n",
" (list 1 2 3 4)\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 169,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>0</code>"
],
"text/plain": [
"0"
]
},
"execution_count": 169,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(and 1 2 3 4 \"bob\" 0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pairs"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'(\"x\" . \"y\")</code>"
],
"text/plain": [
"'(\"x\" . \"y\")"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; Create a pair\n",
"(define t1 (cons \"x\" \"y\"))\n",
"t1"
]
},
{
"cell_type": "code",
"execution_count": 108,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'(\"p\" . \"q\")</code>"
],
"text/plain": [
"'(\"p\" . \"q\")"
]
},
"execution_count": 108,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; Create from dot notation\n",
"(define t2 `(\"p\" . \"q\"))\n",
"t2"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>\"x\"</code>"
],
"text/plain": [
"\"x\""
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; First item\n",
"(car t1)"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>\"y\"</code>"
],
"text/plain": [
"\"y\""
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; Remaining items\n",
"(cdr t1)"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(car l1) = \"x\"\n",
"(cdr l1) = '(\"y\" \"z\")"
]
}
],
"source": [
";; car and cdr with lists\n",
"(define l1 (list \"x\" \"y\" \"z\"))\n",
"(printf \"(car l1) = ~v\\n\" (car l1))\n",
"(printf \"(cdr l1) = ~v\" (cdr l1))"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'(#t #t)</code>"
],
"text/plain": [
"'(#t #t)"
]
},
"execution_count": 98,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; Lists evaluate as pairs\n",
"(list (pair? t1) (pair? l1))"
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'(0 1 . 2)</code>"
],
"text/plain": [
"'(0 1 . 2)"
]
},
"execution_count": 109,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; Printing is wacky when joining a pair into a list\n",
"(cons 0 (cons 1 2))"
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'(0 1 . 2)</code>"
],
"text/plain": [
"'(0 1 . 2)"
]
},
"execution_count": 110,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; This equates to:\n",
"'(0 . (1 . 2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Lists"
]
},
{
"cell_type": "code",
"execution_count": 145,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'(\"jack\" \"jill\")</code>"
],
"text/plain": [
"'(\"jack\" \"jill\")"
]
},
"execution_count": 145,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(define names (list \"jack\" \"jill\"))\n",
"names"
]
},
{
"cell_type": "code",
"execution_count": 146,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>2</code>"
],
"text/plain": [
"2"
]
},
"execution_count": 146,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(length names)"
]
},
{
"cell_type": "code",
"execution_count": 151,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'(\"jack\" \"jill\" \"bob\" \"alice\")</code>"
],
"text/plain": [
"'(\"jack\" \"jill\" \"bob\" \"alice\")"
]
},
"execution_count": 151,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(define all-names (append names (list \"bob\" \"alice\")))\n",
"all-names"
]
},
{
"cell_type": "code",
"execution_count": 152,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>\"jack\"</code>"
],
"text/plain": [
"\"jack\""
]
},
"execution_count": 152,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(list-ref all-names 0)"
]
},
{
"cell_type": "code",
"execution_count": 153,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>\"jack\"</code>"
],
"text/plain": [
"\"jack\""
]
},
"execution_count": 153,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(car all-names)"
]
},
{
"cell_type": "code",
"execution_count": 154,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'(\"jill\" \"bob\" \"alice\")</code>"
],
"text/plain": [
"'(\"jill\" \"bob\" \"alice\")"
]
},
"execution_count": 154,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cdr all-names)"
]
},
{
"cell_type": "code",
"execution_count": 155,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'(\"alice\" \"bob\" \"jill\" \"jack\")</code>"
],
"text/plain": [
"'(\"alice\" \"bob\" \"jill\" \"jack\")"
]
},
"execution_count": 155,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(reverse all-names)"
]
},
{
"cell_type": "code",
"execution_count": 156,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>#f</code>"
],
"text/plain": [
"#f"
]
},
"execution_count": 156,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(member \"ryan\" all-names)"
]
},
{
"cell_type": "code",
"execution_count": 157,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'(\"jack\" \"jill\" \"bob\" \"alice\")</code>"
],
"text/plain": [
"'(\"jack\" \"jill\" \"bob\" \"alice\")"
]
},
"execution_count": 157,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(member \"jack\" all-names)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Quotes"
]
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'(+ 1 2)</code>"
],
"text/plain": [
"'(+ 1 2)"
]
},
"execution_count": 111,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; (quote datum) produces a _constant_ value corresponding to datum (the representation of the program fragment)\n",
"(quote (+ 1 2))"
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'((1 2) (3 4))</code>"
],
"text/plain": [
"'((1 2) (3 4))"
]
},
"execution_count": 112,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; Note that nested lists don't print with quotes\n",
"(list (list 1 2) (list 3 4))"
]
},
{
"cell_type": "code",
"execution_count": 114,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'(list (list 1 2) (list 3 4))</code>"
],
"text/plain": [
"'(list (list 1 2) (list 3 4))"
]
},
"execution_count": 114,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; But we can obtain this behaviour with (quote ...)\n",
"(quote (list (list 1 2) (list 3 4)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Symbols"
]
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'some-symbol</code>"
],
"text/plain": [
"'some-symbol"
]
},
"execution_count": 120,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; Identifiers wrapped with quotes are _symbols_\n",
"(define some-symbol (quote some-symbol))\n",
"some-symbol"
]
},
{
"cell_type": "code",
"execution_count": 121,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>\"some-symbol\"</code>"
],
"text/plain": [
"\"some-symbol\""
]
},
"execution_count": 121,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; Symbols are more like strings than identifiers:\n",
"(symbol->string some-symbol)"
]
},
{
"cell_type": "code",
"execution_count": 122,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'some-symbol</code>"
],
"text/plain": [
"'some-symbol"
]
},
"execution_count": 122,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(string->symbol \"some-symbol\")"
]
},
{
"cell_type": "code",
"execution_count": 126,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'(jack jill)</code>"
],
"text/plain": [
"'(jack jill)"
]
},
"execution_count": 126,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; Create a list of symbols\n",
"(quote (jack jill))"
]
},
{
"cell_type": "code",
"execution_count": 127,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'(jack jill)</code>"
],
"text/plain": [
"'(jack jill)"
]
},
"execution_count": 127,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";; This is analogous to\n",
"'(jack jill)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# The reader and expander layers\n",
"https://docs.racket-lang.org/guide/Pairs__Lists__and_Racket_Syntax.html\n",
"\n",
"The syntax of Racket is not defined directly in terms of character streams. Instead, the syntax is determined by two layers:\n",
"* a reader layer, which turns a sequence of characters into lists, symbols, and other constants; and\n",
"* an expander layer, which processes the lists, symbols, and other constants to parse them as an expression."
]
},
{
"cell_type": "code",
"execution_count": 136,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>'(+ 1 2 3)</code>"
],
"text/plain": [
"'(+ 1 2 3)"
]
},
"execution_count": 136,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"`(+ 1 . (2 3))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment