\documentclass{beamer} %include lhs2TeX.fmt \author{Piyush P Kurur\\ Office no: 224\\ Dept. of Comp. Sci. and Engg.\\ IIT Kanpur} \newcommand{\Token}[2][]{\node(#2#1)[token]{#2};} \newcommand{\Symbol}[2][]{\node(#2#1)[symbol]{#2};} \newcommand{\Point}[1]{\node(#1)[point]{#1};} \usepackage{tikz} \usetikzlibrary{positioning,shapes,chains,fit} \usetikzlibrary{shapes.symbols} \usetikzlibrary{matrix} \usetikzlibrary{backgrounds} \usetikzlibrary{shapes.geometric} \usepackage{multicol} \usepackage{algorithm2e} \usepackage{pgfkeys} \pgfkeys {% /tikz/redbox/.style={% shape=rectangle,% minimum size=1.5em,% top color = white, % bottom color=red!50!black!50, inner sep=0pt, draw=black }, /tikz/greybox/.style={ shape=rectangle,% minimum width=7em,% minimum height=1.5em, fill=black!20, inner sep=0pt, draw=black }, /tikz/blackbox/.style={ shape=rectangle,% minimum width=7em,% minimum height=1.5em, fill=black, inner sep=0pt, draw=black }, /tikz/whitebox/.style={ shape=rectangle, minimum width=7em, minimum height=1.5em, draw=black }, /tikz/bluebox/.style={ shape=rectangle, minimum width=7em, minimum height=1.5em, fill=blue!40, draw=black }, /tikz/whitetape/.style={ shape=rectangle, minimum width=4em, minimum height=1.5em, draw=black }, /tikz/bluearr/.style={ ->, line width=0.25em, draw=blue!40 }, /tikz/greenarr/.style={ ->, line width=0.25em, draw=green!40!black!60 } } \newcommand{\arraythree}[4]{ \begin{tikzpicture}[node distance=0] \pgfsetstrokeopacity{#4}; \node[#1](a0#3){#2}; \node[#1, right=of a0#3](a1#3){#2}; \node[#1, right=of a1#3](a1#3){#2}; \end{tikzpicture} } \newcommand{\arraytwo}[4]{ \begin{tikzpicture}[node distance=0] \pgfsetstrokeopacity{#4}; \node[#1](a0#3){#2}; \node[#1,right=of a0#3]{#2}; \end{tikzpicture} } \newcommand{\Array}[0]{ \arraythree{box}{}{a}{1} } \newcommand{\AArray}[0]{% \arraythree{bbox}{\Array}{b}{0.5}} \newcommand{\AAArray}[0]{% \arraytwo{bbbox}{\AArray}{c}{0.25}} \newcommand{\T}[1]{\ensuremath{T\left(#1\right)}} \newcommand{\BigOh}[1]{\ensuremath{O\left(#1\right)}} \title{Fundamentals of Computing: Lecture 25} \date{October 5, 2009} \begin{document} \begin{frame} \maketitle \end{frame} \begin{frame} \frametitle{Summary of last class} \pause \begin{itemize} \item A structure is a way of constructing product types. \pause \begin{spec} struct structure-name{ field declarations }; \end{spec} \pause \item If |foo| is of type |strunct Foo| and bar is a field of the structure |Foo| then the |bar| field of |foo| can be accessed via the expression |foo.bar| \pause \item Fields are both l-values as well as r-values. \pause \item Structures are passed as values to function which is unlike Java. \end{itemize} \end{frame} \begin{frame} \frametitle{Examples} \pause \begin{spec} struct Vector2D { double x; double y; }; \end{spec} \pause \begin{spec} typedef struct{ double x; double y; } Vector2D; \end{spec} \pause \begin{spec} struct Vector2D{ double x; double y; } origin = {0,0}; \end{spec} \end{frame} \begin{frame} Structures are \emph{not} passed as reference to functions. \pause \begin{code} #line 214 "lecture23.lhs" #include void printVector(struct Vector2D); void shiftByOneUnit(struct Vector2D u); int main () { printf("Before shift: ");printVector(origin); shiftByOneUnit(origin); printf("After shift: ");printVector(origin); } void shiftByOneUnit(struct Vector2D u){ u.x = u.x + 1; } void printVector(struct Vector2D u){ printf("(%f,%f)\n",u.x,u.y); } \end{code} \end{frame} \begin{frame} \frametitle{Recursive data types} \begin{itemize} \item List. \pause A list is either an empty list or an element followed by a list. \pause \item Binary trees. \pause A binary tree is either an empty Tree or a root node with two children left subtree and right subtree. \pause \item General trees (some times called Rose trees). \pause Either an empty tree or a node with a Forest of subtrees. \end{itemize} \pause \begin{block}{Mathematically} \begin{eqnarray*} L (a) &=& \bot + a \times L (a) \\ BT (a) &=& \bot + a \times BT (a) \times BT (a) \\ T (a) &=& \bot + a \times F (a) \\ F (a) &=& L (T (a)) \end{eqnarray*} \end{block} \end{frame} \begin{frame} \frametitle{In Haskell this would be} \begin{spec} data List a = Empty | Cons a (List a) data BinTree a = Empty | Node (BinTree a) a (BinTree a) data Tree a = Empty | Node a [Tree a] \end{spec} \end{frame} \begin{frame} \frametitle{How does one simulate this in |C|?} \pause An ugly dance involving structs and pointers. \pause \begin{spec} typedef struct Node Node; struct Node { int datum; Node * next; }; \end{spec} \pause \begin{spec} typedef Node *List; \end{spec} \end{frame} \begin{frame} \frametitle{The |head| and |tail| function} \begin{spec} data List a = Empty | Cons a (List a) head (Cons x _) = x tail (Cons _ xs) = xs \end{spec} \only<2-3>{ \begin{spec} int head(List l) { if( l == NULL) {error("head of an empty list");} else return (*l).datum } List tail (List l) { if( l == NULL) {error("tail of an empty list");} else return (*l).next } \end{spec} } \only<4->{ \begin{spec} int head(List l) { if( l == NULL) {error("head of an empty list");} else return l -> datum } List tail (List l) { if( l == NULL) {error("tail of an empty list");} else return l -> next } \end{spec} } \uncover<3->{Since |(*foo).bar| often comes in practice} \end{frame} \end{document}