{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CS 201: Structs\n", "

\n", "\n", " \n", "

\n", "Racket documentation for structs.\n", "

\n", " More struct documentation." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "(require racket)\n", "(require racket/base)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We want to define a data structure for a person, comprising the person's name, age, and college. In racket, we could use a list, such as the following." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "(define person1 '(joe 25 cornell))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can access the slots using the standard selectors, such as car, cdr, first, etc." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "'joe" ], "text/plain": [ "'joe" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(car person1)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "25" ], "text/plain": [ "25" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(second person1)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "'cornell" ], "text/plain": [ "'cornell" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(caddr person1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The problem is that someone looking at your code -- including you -- does not know that (car person1) is the name. Also, we won't accidentally \n", "\n", "We solve this problem and make our code" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "cadddr: contract violation\n", " expected: (cons/c any/c (cons/c any/c (cons/c any/c pair?)))\n", " given: '(joe 25 cornell)\n" ] } ], "source": [ "(cadddr person1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The solution in racket is to use struct, which facilitates named access to data structures. Here is struct in action." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "(struct person (name age college) #:transparent)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Constructor: person\n", "\n", "The name of the struct is also the constructor." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "(define p1 (person 'john 23 'yale))\n", "(define p2 (person 'mary 20 'yale))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "(person 'john 23 'yale)" ], "text/plain": [ "(person 'john 23 'yale)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "(person 'mary 20 'yale)" ], "text/plain": [ "(person 'mary 20 'yale)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Selectors: person-name, person-age, person-college\n", "\n", "The selectors for the struct are the name with a hyphen prefixed to the slot names." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "'john" ], "text/plain": [ "'john" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(person-name p1)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "23" ], "text/plain": [ "23" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(person-age p1)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "'yale" ], "text/plain": [ "'yale" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(person-college p2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Comparison: equal? not =\n", "\n", "struct comparison uses equal? not =." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "#f" ], "text/plain": [ "#f" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(equal? p1 p2)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "#t" ], "text/plain": [ "#t" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(equal? p1 p1)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "#t" ], "text/plain": [ "#t" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(equal? p1 (person 'john 23 'yale))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "=: contract violation\n", " expected: number?\n", " given: (person 'john 23 'yale)\n", " argument position: 1st\n", " other arguments...:\n", " (person 'john 23 'yale)\n" ] } ], "source": [ "(= p1 (person 'john 23 'yale))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### predicate: person?\n", "\n", "The predicate for a struct is the name with a ? suffix." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "#t" ], "text/plain": [ "#t" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(person? p1)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "#f" ], "text/plain": [ "#f" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(person? 'p1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### structs for hw3 Turing Machine\n", "\n", "An instruction is a 5-tuple\n", "\n", "(struct ins (c-state c-symbol n-state n-symbol dir) #:transparent)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "(struct ins (c-state c-symbol n-state n-symbol dir) #:transparent)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A configuration is a current state, a head position, and a tape\n", "\n", "(struct conf (state ltape symbol rtape) #:transparent)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "(struct conf (state ltape symbol rtape) #:transparent)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A tape is just a list of symbols" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "(define tape1 '(1 1 0 1 0))\n", "(define tape2 '(b 1 1 b 0 1 1 b b))\n", "(define config1 (conf 'q1 '(0 1) 0 '(1 1)))" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/html": [ "'q1" ], "text/plain": [ "'q1" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(conf-state config1)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/html": [ "0" ], "text/plain": [ "0" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(conf-symbol config1) " ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/html": [ "'(0 1)" ], "text/plain": [ "'(0 1)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(conf-ltape config1)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/html": [ "'(1 1)" ], "text/plain": [ "'(1 1)" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(conf-rtape config1) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A Turing Machine that toggle's 0's and 1's" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "(define tm1 \n", " (list\n", " (ins 'q1 0 'q1 1 'R)\n", " (ins 'q1 1 'q1 0 'R)\n", " (ins 'q1 'b 'q2 'b 'L)\n", " (ins 'q2 0 'q2 0 'L)\n", " (ins 'q2 1 'q2 1 'L)\n", " (ins 'q2 'b 'q3 'b 'R)))" ] }, { "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.4" } }, "nbformat": 4, "nbformat_minor": 4 }