Add CT331 Programming Paradigms
This commit is contained in:
Binary file not shown.
@ -0,0 +1,22 @@
|
||||
#lang racket
|
||||
|
||||
;; a cons pair of two numbers
|
||||
(cons 1 2)
|
||||
|
||||
;; a list of 3 numbers using only the cons function
|
||||
;; this could be more easily done using the single quote `'` (i.e., `'(1 2 3)`) but i don't use it as it seemed against the spirit of the question
|
||||
(cons 1 (cons 2 (cons 3 empty)))
|
||||
|
||||
;; a list containing a string, a number, and a nested list of three numbers using only the cons function
|
||||
(cons "a string"
|
||||
(cons 0
|
||||
(cons (cons 1 (cons 2 (cons 3 empty))) empty)
|
||||
)
|
||||
)
|
||||
|
||||
;; a list containing a string, a number, and a nested list of three numbers, using only the list function
|
||||
(list "a string" 0 (list 1 2 3))
|
||||
|
||||
;; a list containing a string, a number, and a nested list of three numbers, using only the append function
|
||||
;; using `'` as the arguments of the `append` function must be themselves lists
|
||||
(append '("a string") '(0) '((1 2 3)))
|
@ -0,0 +1,93 @@
|
||||
#lang racket
|
||||
|
||||
(provide ins_beg)
|
||||
(provide ins_end)
|
||||
(provide count_top_level)
|
||||
(provide count_instances)
|
||||
(provide count_instances_tr)
|
||||
(provide count_instances_deep)
|
||||
|
||||
;; function to insert an element at the beginning of a list
|
||||
(define (ins_beg el lst)
|
||||
;; assuming that the second element is always a list
|
||||
(cons el lst)
|
||||
)
|
||||
|
||||
;; function to insert an element at the end of a list
|
||||
(define (ins_end el lst)
|
||||
;; making el into a list if it isn't already
|
||||
(append lst (cons el empty))
|
||||
)
|
||||
|
||||
;; function to count the number of top-level items in a list
|
||||
(define (count_top_level lst)
|
||||
(if (null? lst)
|
||||
0 ;; return 0 if we've reached the end of the list
|
||||
(+ 1 (count_top_level (cdr lst))) ;; return 1 plus the count_top_level of the second element of the cons pair (the rest of the list)
|
||||
)
|
||||
)
|
||||
|
||||
;; non-tail recursive function to count the number of times a given item occurs in a list (assuming items are atomic)
|
||||
(define (count_instances item lst)
|
||||
(if (null? lst)
|
||||
0 ;; return 0 if at the end of the list
|
||||
(+
|
||||
(if (equal? item (car lst))
|
||||
1 ;; if the item is equal to the first element of the list, add 1
|
||||
0 ;; if the item is not equal to the first element of the list, add 0
|
||||
)
|
||||
(count_instances item (cdr lst)) ;; recurse with the remainder of the list
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
;; helper function for count_instances_tr
|
||||
(define (count_instances_tr_helper item lst cnt)
|
||||
(cond
|
||||
;; return the count if the end of the list is reached (0 for empty list)
|
||||
((null? lst)
|
||||
cnt
|
||||
)
|
||||
;; if the first element of the list is equal to the item
|
||||
((eq? (car lst) item)
|
||||
;; recurse with the remainder of the list and an incremented count
|
||||
(count_instances_tr_helper item (cdr lst) (+ cnt 1))
|
||||
)
|
||||
;; if the first element of the list is not equal to the item
|
||||
(else
|
||||
;; recurse with the remainder of the list and an unchanged count
|
||||
(count_instances_tr_helper item (cdr lst) cnt)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
;; tail recursive function to count the number of times a given item occurs in a list (assuming items are atomic)
|
||||
(define (count_instances_tr item lst)
|
||||
;; calling helper function with the list and the count so far (0)
|
||||
(count_instances_tr_helper item lst 0)
|
||||
)
|
||||
|
||||
;; function to count the number of times an item occurs in a list and its sub-lists
|
||||
(define (count_instances_deep item lst)
|
||||
(cond
|
||||
;; return nothing if we've reached the end of the list
|
||||
((null? lst)
|
||||
0
|
||||
)
|
||||
|
||||
;; if the first item is a list, recurse through the first element and then the rest and return the sum of the two results
|
||||
((pair? (car lst))
|
||||
(+ (count_instances_deep item (car lst)) (count_instances_deep item (cdr lst)))
|
||||
)
|
||||
|
||||
;; if the first element is equal to the item, add 1 to the count and recurse with the rest of the list
|
||||
((eq? item (car lst)) ; If the first element is equal to the item, increment count
|
||||
(+ 1 (count_instances_deep item (cdr lst)))
|
||||
)
|
||||
|
||||
;; else if the first element is not equal to the item, recurse with the rest of the list
|
||||
(else
|
||||
(count_instances_deep item (cdr lst))
|
||||
)
|
||||
)
|
||||
)
|
@ -0,0 +1,145 @@
|
||||
#lang racket
|
||||
|
||||
;; function to display the contents of a binary search tree in sorted order
|
||||
(define (display_contents bst)
|
||||
(cond
|
||||
;; if the binary search tree is null, print an empty string (nothing)
|
||||
[(null? bst) (display "")]
|
||||
|
||||
;; if the binary search tree has nodes
|
||||
[else
|
||||
;; display the contents of the left sub-tree of the current node
|
||||
(display_contents (cadr bst))
|
||||
|
||||
;; display the current node
|
||||
(display (car bst))
|
||||
(newline)
|
||||
|
||||
;; display the contents of the right sub-tree of the current node
|
||||
(display_contents (caddr bst))
|
||||
]
|
||||
)
|
||||
)
|
||||
|
||||
;; function to search a tree and tell whether a given item is presesnt in a given tree
|
||||
(define (search_tree item bst)
|
||||
(cond
|
||||
;; return false if we've reached the end of the tree without finding a match
|
||||
((null? bst) #f)
|
||||
|
||||
;; return true if the current node is equal to the item
|
||||
((equal? item (car bst)) #t)
|
||||
|
||||
;; else return whether the item was found in the left sub-tree or the right sub-tree
|
||||
(else
|
||||
(or
|
||||
(search_tree item (cadr bst)) ;; search left sub-tree
|
||||
(search_tree item (caddr bst)) ;; search right sub-tree
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
;; function to insert an item into a binary search tree
|
||||
(define (insert_item item bst)
|
||||
(cond
|
||||
;; if there are no nodes in the tree, create a new tree with the item as the root
|
||||
((null? bst)
|
||||
(list item '() '())
|
||||
)
|
||||
|
||||
;; if the item is less than the current node, insert it into the left-hand side of the tree
|
||||
((< item (car bst))
|
||||
;; create new bst with same root node, same right-hand side, but a left-hand side that has had the item inserted
|
||||
(list (car bst) (insert_item item (cadr bst)) (caddr bst))
|
||||
)
|
||||
|
||||
;; if the item is greater than the current node, insert it into the right-hand side of the tree
|
||||
((> item (car bst))
|
||||
;; create new bst with same root node, same left-hand side, but a right-hand side that has had the item inserted
|
||||
(list (car bst) (cadr bst) (insert_item item (caddr bst)))
|
||||
)
|
||||
|
||||
;; else the item is equal to the current node, so do nothing
|
||||
(else bst)
|
||||
)
|
||||
)
|
||||
|
||||
;; function to insert a list of items into a binary search tree
|
||||
(define (insert_list lst bst)
|
||||
(if (null? lst)
|
||||
;; if the list is null, just return the bst with no changes
|
||||
bst
|
||||
|
||||
;; otherwise, recurse with the remainder of the list and the binary tree produced by inserting the first item of the list into bst
|
||||
(insert_list (cdr lst) (insert_item (car lst) bst))
|
||||
)
|
||||
)
|
||||
|
||||
;; tree-sort function
|
||||
(define (tree_sort lst)
|
||||
;; inserting the list into a tree structure to sort it and then displaying the contents of that tree
|
||||
(display_contents (insert_list lst '()))
|
||||
)
|
||||
|
||||
;; function to insert an item into a binary search tree based off a sorting function
|
||||
;; the sorting function should return accept two items and arguments, and return true if they were passed in order, and false otherwise or if they are equal
|
||||
(define (insert_item_custom item bst sorter)
|
||||
(cond
|
||||
;; if there are no nodes in the tree, create a new tree with the item as the root
|
||||
((null? bst)
|
||||
(list item '() '())
|
||||
)
|
||||
|
||||
;; if the item is goes before the current node, insert it into the left-hand side of the tree
|
||||
((sorter item (car bst))
|
||||
;; create new bst with same root node, same right-hand side, but a left-hand side that has had the item inserted
|
||||
(list (car bst) (insert_item_custom item (cadr bst) sorter) (caddr bst))
|
||||
)
|
||||
|
||||
;; if the item goes after the current node, insert it into the right-hand side of the tree
|
||||
((sorter (car bst) item)
|
||||
;; create new bst with same root node, same left-hand side, but a right-hand side that has had the item inserted
|
||||
(list (car bst) (cadr bst) (insert_item_custom item (caddr bst) sorter))
|
||||
)
|
||||
|
||||
;; else the item is equal to the current node, so do nothing
|
||||
(else bst)
|
||||
)
|
||||
)
|
||||
|
||||
;; sorter function which states whether the two arguments were supplied in strictly ascending order (i.e., if item == item2, return false)
|
||||
(define (sort_ascending item1 item2)
|
||||
(if (< item1 item2)
|
||||
#t
|
||||
#f
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
;; sorter function which states whether the two arguments were supplied in strictly descending order (i.e., if item == item2, return false)
|
||||
(define (sort_descending item1 item2)
|
||||
(if (> item1 item2)
|
||||
#t
|
||||
#f
|
||||
)
|
||||
)
|
||||
|
||||
;; sorter function which states whether the two arguments were supplied in strictly ascending order based on the final digit (i.e., if item == item2, return false)
|
||||
(define (sort_ascending_last item1 item2)
|
||||
(if (< (modulo item1 10) (modulo item2 10))
|
||||
#t
|
||||
#f
|
||||
)
|
||||
)
|
||||
|
||||
;; function to insert a list of items into a binary search tree in the order determined by a sorting function
|
||||
(define (insert_list_custom lst bst sorter)
|
||||
(if (null? lst)
|
||||
;; if the list is null, just return the bst with no changes
|
||||
bst
|
||||
|
||||
;; otherwise, recurse with the remainder of the list and the binary tree produced by inserting the first item of the list into bst
|
||||
(insert_list_custom (cdr lst) (insert_item_custom (car lst) bst sorter) sorter)
|
||||
)
|
||||
)
|
Binary file not shown.
@ -0,0 +1,142 @@
|
||||
%! TeX program = lualatex
|
||||
\documentclass[a4paper]{article}
|
||||
|
||||
% packages
|
||||
\usepackage{microtype} % Slightly tweak font spacing for aesthetics
|
||||
\usepackage[english]{babel} % Language hyphenation and typographical rules
|
||||
\usepackage[final, colorlinks = false, urlcolor = cyan]{hyperref}
|
||||
\usepackage{changepage} % adjust margins on the fly
|
||||
\usepackage{fontspec}
|
||||
|
||||
\usepackage{minted}
|
||||
\usemintedstyle{algol_nu}
|
||||
\usepackage{xcolor}
|
||||
|
||||
\usepackage{pgfplots}
|
||||
\pgfplotsset{width=\textwidth,compat=1.9}
|
||||
|
||||
\usepackage{caption}
|
||||
\newenvironment{code}{\captionsetup{type=listing}}{}
|
||||
\captionsetup[listing]{skip=0pt}
|
||||
\setlength{\abovecaptionskip}{5pt}
|
||||
\setlength{\belowcaptionskip}{5pt}
|
||||
|
||||
\usepackage[yyyymmdd]{datetime}
|
||||
\renewcommand{\dateseparator}{--}
|
||||
\setmainfont{EB Garamond}
|
||||
\setmonofont[Scale=MatchLowercase]{Deja Vu Sans Mono}
|
||||
|
||||
\usepackage{titlesec}
|
||||
% \titleformat{\section}{\LARGE\bfseries}{}{}{}[\titlerule]
|
||||
% \titleformat{\subsection}{\Large\bfseries}{}{0em}{}
|
||||
% \titlespacing{\subsection}{0em}{-0.7em}{0em}
|
||||
%
|
||||
% \titleformat{\subsubsection}{\large\bfseries}{}{0em}{$\bullet$ }
|
||||
% \titlespacing{\subsubsection}{1em}{-0.7em}{0em}
|
||||
|
||||
% margins
|
||||
\addtolength{\hoffset}{-2.25cm}
|
||||
\addtolength{\textwidth}{4.5cm}
|
||||
\addtolength{\voffset}{-3.25cm}
|
||||
\addtolength{\textheight}{5cm}
|
||||
\setlength{\parskip}{0pt}
|
||||
\setlength{\parindent}{0in}
|
||||
% \setcounter{secnumdepth}{0}
|
||||
|
||||
\begin{document}
|
||||
\hrule \medskip
|
||||
\begin{minipage}{0.295\textwidth}
|
||||
\raggedright
|
||||
\footnotesize
|
||||
Name: Andrew Hayes \\
|
||||
E-mail: \href{mailto://a.hayes18@universityofgalway.ie}{\texttt{a.hayes18@universityofgalway.ie}} \hfill\\
|
||||
ID: 21321503 \hfill
|
||||
\end{minipage}
|
||||
\begin{minipage}{0.4\textwidth}
|
||||
\centering
|
||||
\vspace{0.4em}
|
||||
\Large
|
||||
\textbf{CT331} \\
|
||||
\end{minipage}
|
||||
\begin{minipage}{0.295\textwidth}
|
||||
\raggedleft
|
||||
\today
|
||||
\end{minipage}
|
||||
\medskip\hrule
|
||||
\begin{center}
|
||||
\normalsize
|
||||
Assignment 2: Functional Programming with Scheme
|
||||
\end{center}
|
||||
\hrule
|
||||
|
||||
\section{Question 1}
|
||||
\subsection{Part (A): Code}
|
||||
\begin{code}
|
||||
\inputminted[texcl, mathescape, breaklines, frame=single]{racket}{../code/assignment_q1.rkt}
|
||||
\caption{\texttt{assignment\_q1.rkt}}
|
||||
\end{code}
|
||||
|
||||
\subsection{Part (B): Comments}
|
||||
\begin{figure}[H]
|
||||
\centering
|
||||
\includegraphics[width=\textwidth]{./images/question1.png}
|
||||
\caption{Output of \texttt{assignment\_q1.rkt}}
|
||||
\end{figure}
|
||||
|
||||
Comments on each line of output:
|
||||
\begin{enumerate}
|
||||
\item The \mintinline{racket}{cons} function creates a \mintinline{racket}{cons} pair, which is not always a ``list''.
|
||||
A list is a \mintinline{racket}{cons} pair in which the second element is another itself another list or is \mintinline{racket}{empty}.
|
||||
When a \mintinline{racket}{cons} pair that is not a list is printed, its elements are delimited by a ``\verb|.|'', as can be seen from the first line of
|
||||
output.
|
||||
|
||||
\item The second section of code produces a list of three numbers using only the \mintinline{racket}{cons} function:
|
||||
first we create a one-element list with \mintinline{racket}{(cons 3 empty)}, then we create a two-element list by making a \mintinline{racket}{cons} pair
|
||||
of \mintinline{racket}{2} and the already-created one-element list, and finally we create a three-element list by making a \mintinline{racket}{cons} pair
|
||||
of \mintinline{racket}{1} and the two-element list.
|
||||
This could of course be achieved far more simply by just using \mintinline{racket}{(cons 1 '(2 3))} or even justs \mintinline{racket}{'(1 2 3)} but I
|
||||
felt that this would be against the spirit of the exercise.
|
||||
|
||||
\item To create a nested list using only \mintinline{racket}{cons} in the third section of code, we make the \mintinline{racket}{'(1 2 3)} as previously,
|
||||
\mintinline{racket}{cons} it with \mintinline{racket}{empty} to make a nested list, and then \mintinline{racket}{cons} it with \mintinline{racket}{0}, and
|
||||
\mintinline{racket}{cons} that with a string literal.
|
||||
|
||||
\item Like \mintinline{racket}{cons}, \mintinline{racket}{list} can take either atomics or lists as arguments.
|
||||
To create the list using only the \mintinline{racket}{list} function, we can simply make a list of \mintinline{racket}{(list 1 2 3)}, and then create a
|
||||
list consisting of \mintinline{racket}{"a string"}, \mintinline{racket}{0}, \& the aforementioned list.
|
||||
This is much simpler than using \mintinline{racket}{cons} because \mintinline{racket}{list} can take as many arguments as we want, while
|
||||
\mintinline{racket}{cons} can only take two arguments.
|
||||
|
||||
\item Although I opted not to make use of the ``\mintinline{racket}{'}'' operator to create lists for the previous exercises, I make use of it here as there is
|
||||
no other way to create a list using only \mintinline{racket}{append} and nothing else, as \mintinline{racket}{append} only accepts lists as arguments.
|
||||
We make a list consisting of only one element (\mintinline{racket}{"a string"}), another list consisting of only one element (\mintinline{racket}{0}),
|
||||
and finally a list consisting of three elements \mintinline{racket}{'(1 2 3)} and append them into one to create the desired list.
|
||||
\end{enumerate}
|
||||
|
||||
\section{Question 2}
|
||||
\begin{code}
|
||||
\inputminted[breaklines, frame=single]{racket}{../code/assignment_q2.rkt}
|
||||
\caption{\texttt{assignment\_q2.rkt}}
|
||||
\end{code}
|
||||
|
||||
\section{Question 3}
|
||||
\begin{code}
|
||||
\inputminted[breaklines, frame=single]{racket}{../code/assignment_q3.rkt}
|
||||
\caption{\texttt{assignment\_q3.rkt}}
|
||||
\end{code}
|
||||
|
||||
It is worth noting here that the function \mintinline{racket}{sort_ascending_last} operates in a manner that may be undesirable.
|
||||
The function determines whether the two numbers passed to it as arguments were passed in strictly ascending order based on the final
|
||||
digit, i.e. it returns \mintinline{racket}{#t} if the final digit of the first argument is less than the final digit of the second
|
||||
argument, and \mintinline{racket}{#f} otherwise.
|
||||
Because this function considers only the final digit of the numbers, it considers two numbers to be equal if they share a final digit,
|
||||
e.g. it considers the numbers \mintinline{racket}{99} \& \mintinline{racket}{9} to be the same.
|
||||
Therefore, if one attempts to insert those two values into the binary search tree using this function as the ``sorter'', only the
|
||||
former value will get inserted, as binary search trees do not allow duplicate values, and the \mintinline{racket}{sort_ascending_last} function
|
||||
considers those two values to be equal.
|
||||
However, I thought it would be incorrect for the function to consider the $n-1$\textsuperscript{th} digits in the case of the $n$\textsuperscript{th} digits being identical, as
|
||||
a) the assignment did not ask for that and b) that would really just be no different to sorting the numbers in ascending order.
|
||||
|
||||
|
||||
|
||||
\end{document}
|
@ -0,0 +1,22 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\}]
|
||||
\PYG{o}{\PYGZsh{}}\PYG{n+nv}{lang}\PYG{+w}{ }\PYG{n+nv}{racket}
|
||||
|
||||
\PYG{c+c1}{;; a cons pair of two numbers}
|
||||
\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; a list of 3 numbers using only the cons function}
|
||||
\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{3}\PYG{+w}{ }\PYG{n+nv}{empty}\PYG{p}{)))}\PYG{+w}{ }
|
||||
|
||||
\PYG{c+c1}{;; a list containing a string, a number, and a nested list of three numbers using only the cons function}
|
||||
\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}a string\PYGZdq{}}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{+w}{ }
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{3}\PYG{+w}{ }\PYG{n+nv}{empty}\PYG{p}{)))}\PYG{+w}{ }\PYG{n+nv}{empty}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; a list containing a string, a number, and a nested list of three numbers, using only the list function}
|
||||
\PYG{p}{(}\PYG{n+nb}{list}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}a string\PYGZdq{}}\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{list}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{+w}{ }\PYG{l+m+mi}{3}\PYG{p}{))}
|
||||
|
||||
\PYG{c+c1}{;; a list containing a string, a number, and a nested list of three numbers, using only the append function}
|
||||
\PYG{p}{(}\PYG{n+nb}{append}\PYG{+w}{ }\PYG{o}{\PYGZsq{}}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}a string\PYGZdq{}}\PYG{p}{)}\PYG{+w}{ }\PYG{o}{\PYGZsq{}}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{)}\PYG{+w}{ }\PYG{o}{\PYGZsq{}}\PYG{p}{((}\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{+w}{ }\PYG{l+m+mi}{3}\PYG{p}{)))}
|
||||
\end{Verbatim}
|
@ -0,0 +1,22 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\}]
|
||||
\PYG{err}{\PYGZsh{}}\PYG{n+nv}{lang}\PYG{+w}{ }\PYG{n+nv}{racket}
|
||||
|
||||
\PYG{c+c1}{;; a cons pair of two numbers}
|
||||
\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; a list of 3 numbers using only the cons function}
|
||||
\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{3}\PYG{+w}{ }\PYG{n+nv}{empty}\PYG{p}{)))}\PYG{+w}{ }
|
||||
|
||||
\PYG{c+c1}{;; a list containing a string, a number, and a nested list of three numbers using only the cons function}
|
||||
\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}a string\PYGZdq{}}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{+w}{ }
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{3}\PYG{+w}{ }\PYG{n+nv}{empty}\PYG{p}{)))}\PYG{+w}{ }\PYG{n+nv}{empty}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; a list containing a string, a number, and a nested list of three numbers, using only the list function}
|
||||
\PYG{p}{(}\PYG{n+nb}{list}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}a string\PYGZdq{}}\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{list}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{+w}{ }\PYG{l+m+mi}{3}\PYG{p}{))}
|
||||
|
||||
\PYG{c+c1}{;; a list containing a string, a number, and a nested list of three numbers, using only the append function}
|
||||
\PYG{p}{(}\PYG{n+nb}{append}\PYG{+w}{ }\PYG{o}{\PYGZsq{}}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}a string\PYGZdq{}}\PYG{p}{)}\PYG{+w}{ }\PYG{o}{\PYGZsq{}}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{)}\PYG{+w}{ }\PYG{o}{\PYGZsq{}}\PYG{p}{((}\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{+w}{ }\PYG{l+m+mi}{3}\PYG{p}{)))}
|
||||
\end{Verbatim}
|
@ -0,0 +1,22 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\}]
|
||||
\PYG{k+kn}{\PYGZsh{}lang }\PYG{n+nn}{racket}
|
||||
|
||||
\PYG{c+c1}{;; a cons pair of two numbers}
|
||||
\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; a list of 3 numbers using only the cons function}
|
||||
\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{3}\PYG{+w}{ }\PYG{n+nb}{empty}\PYG{p}{)))}\PYG{+w}{ }
|
||||
|
||||
\PYG{c+c1}{;; a list containing a string, a number, and a nested list of three numbers using only the cons function}
|
||||
\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+s+s2}{\PYGZdq{}a string\PYGZdq{}}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{+w}{ }
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{3}\PYG{+w}{ }\PYG{n+nb}{empty}\PYG{p}{)))}\PYG{+w}{ }\PYG{n+nb}{empty}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; a list containing a string, a number, and a nested list of three numbers, using only the list function}
|
||||
\PYG{p}{(}\PYG{n+nb}{list}\PYG{+w}{ }\PYG{l+s+s2}{\PYGZdq{}a string\PYGZdq{}}\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{list}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{+w}{ }\PYG{l+m+mi}{3}\PYG{p}{))}
|
||||
|
||||
\PYG{c+c1}{;; a list containing a string, a number, and a nested list of three numbers, using only the append function}
|
||||
\PYG{p}{(}\PYG{n+nb}{append}\PYG{+w}{ }\PYG{o}{\PYGZsq{}}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}a string\PYGZdq{}}\PYG{p}{)}\PYG{+w}{ }\PYG{o}{\PYGZsq{}}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{)}\PYG{+w}{ }\PYG{o}{\PYGZsq{}}\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{+w}{ }\PYG{l+m+mi}{3}\PYG{p}{)))}
|
||||
\end{Verbatim}
|
@ -0,0 +1,3 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\}]
|
||||
\PYG{n+nb}{cons}
|
||||
\end{Verbatim}
|
@ -0,0 +1,3 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\}]
|
||||
\PYG{l+m+mi}{2}
|
||||
\end{Verbatim}
|
@ -0,0 +1,24 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
|
||||
\PYG{k+kn}{\PYGZsh{}lang }\PYG{n+nn}{racket}
|
||||
|
||||
\PYG{c+c1}{;; a cons pair of two numbers}
|
||||
\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; a list of 3 numbers using only the cons function}
|
||||
\PYG{c+c1}{;; this could be more easily done using the single quote `'` (i.e., `'(1 2 3)`) but i don't use it as it seemed against the spirit of the question}
|
||||
\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{3}\PYG{+w}{ }\PYG{n+nb}{empty}\PYG{p}{)))}\PYG{+w}{ }
|
||||
|
||||
\PYG{c+c1}{;; a list containing a string, a number, and a nested list of three numbers using only the cons function}
|
||||
\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+s+s2}{\PYGZdq{}a string\PYGZdq{}}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{+w}{ }
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{3}\PYG{+w}{ }\PYG{n+nb}{empty}\PYG{p}{)))}\PYG{+w}{ }\PYG{n+nb}{empty}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; a list containing a string, a number, and a nested list of three numbers, using only the list function}
|
||||
\PYG{p}{(}\PYG{n+nb}{list}\PYG{+w}{ }\PYG{l+s+s2}{\PYGZdq{}a string\PYGZdq{}}\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{list}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{+w}{ }\PYG{l+m+mi}{3}\PYG{p}{))}
|
||||
|
||||
\PYG{c+c1}{;; a list containing a string, a number, and a nested list of three numbers, using only the append function}
|
||||
\PYG{c+c1}{;; using `'` as the arguments of the `append` function must be themselves lists}
|
||||
\PYG{p}{(}\PYG{n+nb}{append}\PYG{+w}{ }\PYG{o}{\PYGZsq{}}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}a string\PYGZdq{}}\PYG{p}{)}\PYG{+w}{ }\PYG{o}{\PYGZsq{}}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{)}\PYG{+w}{ }\PYG{o}{\PYGZsq{}}\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{+w}{ }\PYG{l+m+mi}{3}\PYG{p}{)))}
|
||||
\end{Verbatim}
|
@ -0,0 +1,3 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\}]
|
||||
\PYG{n+no}{\PYGZsh{}f}
|
||||
\end{Verbatim}
|
@ -0,0 +1,3 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\}]
|
||||
\PYG{n+nb}{append}
|
||||
\end{Verbatim}
|
@ -0,0 +1,3 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\}]
|
||||
\PYG{n+nb}{list}
|
||||
\end{Verbatim}
|
@ -0,0 +1,28 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
|
||||
\PYG{k+kn}{\PYGZsh{}lang }\PYG{n+nn}{racket}
|
||||
|
||||
\PYG{p}{(}\PYG{k}{provide}\PYG{+w}{ }\PYG{n}{ins\PYGZus{}beg}\PYG{p}{)}
|
||||
\PYG{p}{(}\PYG{k}{provide}\PYG{+w}{ }\PYG{n}{ins\PYGZus{}end}\PYG{p}{)}
|
||||
\PYG{p}{(}\PYG{k}{provide}\PYG{+w}{ }\PYG{n}{count\PYGZus{}top\PYGZus{}level}\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; function to insert an element at the beginning of a list}
|
||||
\PYG{p}{(}\PYG{k}{define}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{ins\PYGZus{}beg}\PYG{+w}{ }\PYG{n}{el}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; assuming that the second element is always a list}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{n}{el}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}
|
||||
\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; function to insert an element at the end of a list}
|
||||
\PYG{p}{(}\PYG{k}{define}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{ins\PYGZus{}end}\PYG{+w}{ }\PYG{n}{el}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; making el into a list if it isn't already}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{append}\PYG{+w}{ }\PYG{n}{lst}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{n}{el}\PYG{+w}{ }\PYG{n+nb}{empty}\PYG{p}{))}
|
||||
\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; function to count the number of top-level items in a list}
|
||||
\PYG{p}{(}\PYG{k}{define}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{count\PYGZus{}top\PYGZus{}level}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{null?}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{+w}{ }\PYG{c+c1}{;; return 0 if we've reached the end of the list}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{count\PYGZus{}top\PYGZus{}level}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cdr}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)))}\PYG{+w}{ }\PYG{c+c1}{;; return 1 plus the count_top_level of the second element of the cons pair (the rest of the list)}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{p}{)}
|
||||
|
||||
\end{Verbatim}
|
@ -0,0 +1,95 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\}]
|
||||
\PYG{k+kn}{\PYGZsh{}lang }\PYG{n+nn}{racket}
|
||||
|
||||
\PYG{p}{(}\PYG{k}{provide}\PYG{+w}{ }\PYG{n}{ins\PYGZus{}beg}\PYG{p}{)}
|
||||
\PYG{p}{(}\PYG{k}{provide}\PYG{+w}{ }\PYG{n}{ins\PYGZus{}end}\PYG{p}{)}
|
||||
\PYG{p}{(}\PYG{k}{provide}\PYG{+w}{ }\PYG{n}{count\PYGZus{}top\PYGZus{}level}\PYG{p}{)}
|
||||
\PYG{p}{(}\PYG{k}{provide}\PYG{+w}{ }\PYG{n}{count\PYGZus{}instances}\PYG{p}{)}
|
||||
\PYG{p}{(}\PYG{k}{provide}\PYG{+w}{ }\PYG{n}{count\PYGZus{}instances\PYGZus{}tr}\PYG{p}{)}
|
||||
\PYG{p}{(}\PYG{k}{provide}\PYG{+w}{ }\PYG{n}{count\PYGZus{}instances\PYGZus{}deep}\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; function to insert an element at the beginning of a list}
|
||||
\PYG{p}{(}\PYG{k}{define}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{ins\PYGZus{}beg}\PYG{+w}{ }\PYG{n}{el}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; assuming that the second element is always a list}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{n}{el}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}
|
||||
\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; function to insert an element at the end of a list}
|
||||
\PYG{p}{(}\PYG{k}{define}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{ins\PYGZus{}end}\PYG{+w}{ }\PYG{n}{el}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; making el into a list if it isn\PYGZsq{}t already}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{append}\PYG{+w}{ }\PYG{n}{lst}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{n}{el}\PYG{+w}{ }\PYG{n+nb}{empty}\PYG{p}{))}
|
||||
\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; function to count the number of top\PYGZhy{}level items in a list}
|
||||
\PYG{p}{(}\PYG{k}{define}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{count\PYGZus{}top\PYGZus{}level}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{null?}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{+w}{ }\PYG{c+c1}{;; return 0 if we\PYGZsq{}ve reached the end of the list}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{count\PYGZus{}top\PYGZus{}level}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cdr}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)))}\PYG{+w}{ }\PYG{c+c1}{;; return 1 plus the count\PYGZus{}top\PYGZus{}level of the second element of the cons pair (the rest of the list)}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; non\PYGZhy{}tail recursive function to count the number of times a given item occurs in a list (assuming items are atomic)}
|
||||
\PYG{p}{(}\PYG{k}{define}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{count\PYGZus{}instances}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{null?}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{+w}{ }\PYG{c+c1}{;; return 0 if at the end of the list}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{+}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{equal?}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{car}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{))}
|
||||
\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{c+c1}{;; if the item is equal to the first element of the list, add 1}
|
||||
\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{+w}{ }\PYG{c+c1}{;; if the item is not equal to the first element of the list, add 0}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n}{count\PYGZus{}instances}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cdr}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{))}\PYG{+w}{ }\PYG{c+c1}{;; recurse with the remainder of the list}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; helper function for count\PYGZus{}instances\PYGZus{}tr}
|
||||
\PYG{p}{(}\PYG{k}{define}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{count\PYGZus{}instances\PYGZus{}tr\PYGZus{}helper}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{n}{lst}\PYG{+w}{ }\PYG{n}{cnt}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{k}{cond}
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; return the count if the end of the list is reached (0 for empty list)}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{null?}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{n}{cnt}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; if the first element of the list is equal to the item }
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{eq?}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{car}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}\PYG{+w}{ }\PYG{n}{item}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; recurse with the remainder of the list and an incremented count}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n}{count\PYGZus{}instances\PYGZus{}tr\PYGZus{}helper}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cdr}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{+}\PYG{+w}{ }\PYG{n}{cnt}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{p}{))}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; if the first element of the list is not equal to the item}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{k}{else}
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; recurse with the remainder of the list and an unchanged count}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n}{count\PYGZus{}instances\PYGZus{}tr\PYGZus{}helper}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cdr}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}\PYG{+w}{ }\PYG{n}{cnt}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; tail recursive function to count the number of times a given item occurs in a list (assuming items are atomic)}
|
||||
\PYG{p}{(}\PYG{k}{define}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{count\PYGZus{}instances\PYGZus{}tr}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; calling helper function with the list and the count so far (0)}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n}{count\PYGZus{}instances\PYGZus{}tr\PYGZus{}helper}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{n}{lst}\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{p}{)}
|
||||
\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; function to count the number of times an item occurs in a list and its sub\PYGZhy{}lists}
|
||||
\PYG{p}{(}\PYG{k}{define}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{count\PYGZus{}instances\PYGZus{}deep}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{k}{cond}
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; return nothing if we\PYGZsq{}ve reached the end of the list}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{null?}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{l+m+mi}{0}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; if the first item is a list, recurse through the first element and then the rest and return the sum of the two results}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{pair?}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{car}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{))}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{+}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{count\PYGZus{}instances\PYGZus{}deep}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{car}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{))}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{count\PYGZus{}instances\PYGZus{}deep}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cdr}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)))}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; if the first element is equal to the item, add 1 to the count and recurse with the rest of the list}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{eq?}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{car}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{))}\PYG{+w}{ }\PYG{c+c1}{; If the first element is equal to the item, increment count}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{count\PYGZus{}instances\PYGZus{}deep}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cdr}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)))}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; else if the first element is not equal to the item, recurse with the rest of the list}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{k}{else}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n}{count\PYGZus{}instances\PYGZus{}deep}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cdr}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{))}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{p}{)}
|
||||
\end{Verbatim}
|
@ -0,0 +1,3 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\}]
|
||||
\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{\PYGZsq{}}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{+w}{ }\PYG{l+m+mi}{3}\PYG{p}{))}
|
||||
\end{Verbatim}
|
@ -0,0 +1,147 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\}]
|
||||
\PYG{k+kn}{\PYGZsh{}lang }\PYG{n+nn}{racket}
|
||||
|
||||
\PYG{c+c1}{;; function to display the contents of a binary search tree in sorted order}
|
||||
\PYG{p}{(}\PYG{k}{define}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{display\PYGZus{}contents}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{k}{cond}
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; if the binary search tree is null, print an empty string (nothing)}
|
||||
\PYG{+w}{ }\PYG{p}{[}\PYG{p}{(}\PYG{n+nb}{null?}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{display}\PYG{+w}{ }\PYG{l+s+s2}{\PYGZdq{}\PYGZdq{}}\PYG{p}{)]}
|
||||
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; if the binary search tree has nodes}
|
||||
\PYG{+w}{ }\PYG{p}{[}\PYG{k}{else}\PYG{+w}{ }
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; display the contents of the left sub\PYGZhy{}tree of the current node}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n}{display\PYGZus{}contents}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cadr}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{))}
|
||||
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; display the current node}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{display}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{car}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{))}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{newline}\PYG{p}{)}
|
||||
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; display the contents of the right sub\PYGZhy{}tree of the current node}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n}{display\PYGZus{}contents}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{caddr}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{))}
|
||||
\PYG{+w}{ }\PYG{p}{]}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; function to search a tree and tell whether a given item is presesnt in a given tree}
|
||||
\PYG{p}{(}\PYG{k}{define}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{search\PYGZus{}tree}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{k}{cond}
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; return false if we\PYGZsq{}ve reached the end of the tree without finding a match}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{null?}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{)}\PYG{+w}{ }\PYG{n+no}{\PYGZsh{}f}\PYG{p}{)}
|
||||
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; return true if the current node is equal to the item}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{equal?}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{car}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{))}\PYG{+w}{ }\PYG{n+no}{\PYGZsh{}t}\PYG{p}{)}
|
||||
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; else return whether the item was found in the left sub\PYGZhy{}tree or the right sub\PYGZhy{}tree}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{k}{else}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{k}{or}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n}{search\PYGZus{}tree}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cadr}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{))}\PYG{+w}{ }\PYG{c+c1}{;; search left sub\PYGZhy{}tree}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n}{search\PYGZus{}tree}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{caddr}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{))}\PYG{+w}{ }\PYG{c+c1}{;; search right sub\PYGZhy{}tree}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; function to insert an item into a binary search tree}
|
||||
\PYG{p}{(}\PYG{k}{define}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{insert\PYGZus{}item}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{k}{cond}
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; if there are no nodes in the tree, create a new tree with the item as the root}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{null?}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{list}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{o}{\PYGZsq{}}\PYG{p}{()}\PYG{+w}{ }\PYG{o}{\PYGZsq{}}\PYG{p}{())}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; if the item is less than the current node, insert it into the left\PYGZhy{}hand side of the tree}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{\PYGZlt{}}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{car}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{))}
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; create new bst with same root node, same right\PYGZhy{}hand side, but a left\PYGZhy{}hand side that has had the item inserted}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{list}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{car}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{insert\PYGZus{}item}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cadr}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{))}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{caddr}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{))}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; if the item is greater than the current node, insert it into the right\PYGZhy{}hand side of the tree}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{\PYGZgt{}}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{car}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{))}
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; create new bst with same root node, same left\PYGZhy{}hand side, but a right\PYGZhy{}hand side that has had the item inserted}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{list}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{car}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cadr}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{insert\PYGZus{}item}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{caddr}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{)))}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; else the item is equal to the current node, so do nothing}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{k}{else}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; function to insert a list of items into a binary search tree}
|
||||
\PYG{p}{(}\PYG{k}{define}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{insert\PYGZus{}list}\PYG{+w}{ }\PYG{n}{lst}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{null?}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; if the list is null, just return the bst with no changes}
|
||||
\PYG{+w}{ }\PYG{n}{bst}
|
||||
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; otherwise, recurse with the remainder of the list and the binary tree produced by inserting the first item of the list into bst}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n}{insert\PYGZus{}list}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cdr}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{insert\PYGZus{}item}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{car}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{))}
|
||||
\PYG{+w}{ }\PYG{p}{)}\PYG{+w}{ }
|
||||
\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; tree\PYGZhy{}sort function}
|
||||
\PYG{p}{(}\PYG{k}{define}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{tree\PYGZus{}sort}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; inserting the list into a tree structure to sort it and then displaying the contents of that tree }
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n}{display\PYGZus{}contents}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{insert\PYGZus{}list}\PYG{+w}{ }\PYG{n}{lst}\PYG{+w}{ }\PYG{o}{\PYGZsq{}}\PYG{p}{()))}
|
||||
\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; function to insert an item into a binary search tree based off a sorting function}
|
||||
\PYG{c+c1}{;; the sorting function should return accept two items and arguments, and return true if they were passed in order, and false otherwise or if they are equal}
|
||||
\PYG{p}{(}\PYG{k}{define}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{insert\PYGZus{}item\PYGZus{}custom}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{n}{bst}\PYG{+w}{ }\PYG{n}{sorter}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{k}{cond}
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; if there are no nodes in the tree, create a new tree with the item as the root}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{null?}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{list}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{o}{\PYGZsq{}}\PYG{p}{()}\PYG{+w}{ }\PYG{o}{\PYGZsq{}}\PYG{p}{())}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; if the item is goes before the current node, insert it into the left\PYGZhy{}hand side of the tree}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{p}{(}\PYG{n}{sorter}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{car}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{))}
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; create new bst with same root node, same right\PYGZhy{}hand side, but a left\PYGZhy{}hand side that has had the item inserted}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{list}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{car}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{insert\PYGZus{}item\PYGZus{}custom}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cadr}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{)}\PYG{+w}{ }\PYG{n}{sorter}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{caddr}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{))}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; if the item goes after the current node, insert it into the right\PYGZhy{}hand side of the tree}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{p}{(}\PYG{n}{sorter}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{car}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{)}\PYG{+w}{ }\PYG{n}{item}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; create new bst with same root node, same left\PYGZhy{}hand side, but a right\PYGZhy{}hand side that has had the item inserted}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{list}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{car}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cadr}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{insert\PYGZus{}item\PYGZus{}custom}\PYG{+w}{ }\PYG{n}{item}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{caddr}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{)}\PYG{+w}{ }\PYG{n}{sorter}\PYG{p}{))}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; else the item is equal to the current node, so do nothing}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{k}{else}\PYG{+w}{ }\PYG{n}{bst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; sorter function which states whether the two arguments were supplied in strictly ascending order (i.e., if item == item2, return false)}
|
||||
\PYG{p}{(}\PYG{k}{define}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{sort\PYGZus{}ascending}\PYG{+w}{ }\PYG{n}{item1}\PYG{+w}{ }\PYG{n}{item2}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{\PYGZlt{}}\PYG{+w}{ }\PYG{n}{item1}\PYG{+w}{ }\PYG{n}{item2}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{n+no}{\PYGZsh{}t}
|
||||
\PYG{+w}{ }\PYG{n+no}{\PYGZsh{}f}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{p}{)}
|
||||
|
||||
|
||||
\PYG{c+c1}{;; sorter function which states whether the two arguments were supplied in strictly descending order (i.e., if item == item2, return false)}
|
||||
\PYG{p}{(}\PYG{k}{define}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{sort\PYGZus{}descending}\PYG{+w}{ }\PYG{n}{item1}\PYG{+w}{ }\PYG{n}{item2}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{\PYGZgt{}}\PYG{+w}{ }\PYG{n}{item1}\PYG{+w}{ }\PYG{n}{item2}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{n+no}{\PYGZsh{}t}
|
||||
\PYG{+w}{ }\PYG{n+no}{\PYGZsh{}f}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; sorter function which states whether the two arguments were supplied in strictly ascending order based on the final digit (i.e., if item == item2, return false)}
|
||||
\PYG{p}{(}\PYG{k}{define}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{sort\PYGZus{}ascending\PYGZus{}last}\PYG{+w}{ }\PYG{n}{item1}\PYG{+w}{ }\PYG{n}{item2}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{\PYGZlt{}}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{modulo}\PYG{+w}{ }\PYG{n}{item1}\PYG{+w}{ }\PYG{l+m+mi}{10}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{modulo}\PYG{+w}{ }\PYG{n}{item2}\PYG{+w}{ }\PYG{l+m+mi}{10}\PYG{p}{))}
|
||||
\PYG{+w}{ }\PYG{n+no}{\PYGZsh{}t}
|
||||
\PYG{+w}{ }\PYG{n+no}{\PYGZsh{}f}
|
||||
\PYG{+w}{ }\PYG{p}{)}
|
||||
\PYG{p}{)}
|
||||
|
||||
\PYG{c+c1}{;; function to insert a list of items into a binary search tree in the order determined by a sorting function}
|
||||
\PYG{p}{(}\PYG{k}{define}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{insert\PYGZus{}list\PYGZus{}custom}\PYG{+w}{ }\PYG{n}{lst}\PYG{+w}{ }\PYG{n}{bst}\PYG{+w}{ }\PYG{n}{sorter}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{null?}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; if the list is null, just return the bst with no changes}
|
||||
\PYG{+w}{ }\PYG{n}{bst}
|
||||
|
||||
\PYG{+w}{ }\PYG{c+c1}{;; otherwise, recurse with the remainder of the list and the binary tree produced by inserting the first item of the list into bst}
|
||||
\PYG{+w}{ }\PYG{p}{(}\PYG{n}{insert\PYGZus{}list\PYGZus{}custom}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{cdr}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{insert\PYGZus{}item\PYGZus{}custom}\PYG{+w}{ }\PYG{p}{(}\PYG{n+nb}{car}\PYG{+w}{ }\PYG{n}{lst}\PYG{p}{)}\PYG{+w}{ }\PYG{n}{bst}\PYG{+w}{ }\PYG{n}{sorter}\PYG{p}{)}\PYG{+w}{ }\PYG{n}{sorter}\PYG{p}{)}
|
||||
\PYG{+w}{ }\PYG{p}{)}\PYG{+w}{ }
|
||||
\PYG{p}{)}
|
||||
\end{Verbatim}
|
@ -0,0 +1,3 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\}]
|
||||
\PYG{n}{sort\PYGZus{}ascending\PYGZus{}last}
|
||||
\end{Verbatim}
|
@ -0,0 +1,3 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\}]
|
||||
\PYG{l+m+mi}{9}
|
||||
\end{Verbatim}
|
@ -0,0 +1,3 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\}]
|
||||
\PYG{n+nb}{empty}
|
||||
\end{Verbatim}
|
@ -0,0 +1,3 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\}]
|
||||
\PYG{l+m+mi}{0}
|
||||
\end{Verbatim}
|
@ -0,0 +1,3 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\}]
|
||||
\PYG{l+s+s2}{\PYGZdq{}a string\PYGZdq{}}
|
||||
\end{Verbatim}
|
@ -0,0 +1,3 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\}]
|
||||
\PYG{p}{(}\PYG{n+nb}{list}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{+w}{ }\PYG{l+m+mi}{3}\PYG{p}{)}
|
||||
\end{Verbatim}
|
@ -0,0 +1,3 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\}]
|
||||
\PYG{l+m+mi}{1}
|
||||
\end{Verbatim}
|
@ -0,0 +1,3 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\}]
|
||||
\PYG{l+m+mi}{99}
|
||||
\end{Verbatim}
|
@ -0,0 +1,3 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\}]
|
||||
\PYG{o}{\PYGZsq{}}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{+w}{ }\PYG{l+m+mi}{3}\PYG{p}{)}
|
||||
\end{Verbatim}
|
@ -0,0 +1,3 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\}]
|
||||
\PYG{p}{(}\PYG{n+nb}{cons}\PYG{+w}{ }\PYG{l+m+mi}{3}\PYG{+w}{ }\PYG{n+nb}{empty}\PYG{p}{)}
|
||||
\end{Verbatim}
|
@ -0,0 +1,3 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\}]
|
||||
\PYG{n+no}{\PYGZsh{}t}
|
||||
\end{Verbatim}
|
@ -0,0 +1,3 @@
|
||||
\begin{Verbatim}[commandchars=\\\{\}]
|
||||
\PYG{o}{\PYGZsq{}}
|
||||
\end{Verbatim}
|
@ -0,0 +1,76 @@
|
||||
|
||||
\makeatletter
|
||||
\def\PYG@reset{\let\PYG@it=\relax \let\PYG@bf=\relax%
|
||||
\let\PYG@ul=\relax \let\PYG@tc=\relax%
|
||||
\let\PYG@bc=\relax \let\PYG@ff=\relax}
|
||||
\def\PYG@tok#1{\csname PYG@tok@#1\endcsname}
|
||||
\def\PYG@toks#1+{\ifx\relax#1\empty\else%
|
||||
\PYG@tok{#1}\expandafter\PYG@toks\fi}
|
||||
\def\PYG@do#1{\PYG@bc{\PYG@tc{\PYG@ul{%
|
||||
\PYG@it{\PYG@bf{\PYG@ff{#1}}}}}}}
|
||||
\def\PYG#1#2{\PYG@reset\PYG@toks#1+\relax+\PYG@do{#2}}
|
||||
|
||||
\@namedef{PYG@tok@c}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}}
|
||||
\@namedef{PYG@tok@cp}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}}
|
||||
\@namedef{PYG@tok@cs}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}}
|
||||
\@namedef{PYG@tok@k}{\let\PYG@bf=\textbf}
|
||||
\@namedef{PYG@tok@kd}{\let\PYG@bf=\textbf\let\PYG@it=\textit}
|
||||
\@namedef{PYG@tok@nb}{\let\PYG@bf=\textbf\let\PYG@it=\textit}
|
||||
\@namedef{PYG@tok@bp}{\let\PYG@bf=\textbf\let\PYG@it=\textit}
|
||||
\@namedef{PYG@tok@nn}{\let\PYG@bf=\textbf\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@nc}{\let\PYG@bf=\textbf\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@nf}{\let\PYG@bf=\textbf\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@nv}{\let\PYG@bf=\textbf\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@no}{\let\PYG@bf=\textbf\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@ow}{\let\PYG@bf=\textbf}
|
||||
\@namedef{PYG@tok@s}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@err}{\def\PYG@bc##1{{\setlength{\fboxsep}{\string -\fboxrule}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}}}
|
||||
\@namedef{PYG@tok@kc}{\let\PYG@bf=\textbf}
|
||||
\@namedef{PYG@tok@kn}{\let\PYG@bf=\textbf}
|
||||
\@namedef{PYG@tok@kp}{\let\PYG@bf=\textbf}
|
||||
\@namedef{PYG@tok@kr}{\let\PYG@bf=\textbf}
|
||||
\@namedef{PYG@tok@kt}{\let\PYG@bf=\textbf}
|
||||
\@namedef{PYG@tok@fm}{\let\PYG@bf=\textbf\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@vc}{\let\PYG@bf=\textbf\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@vg}{\let\PYG@bf=\textbf\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@vi}{\let\PYG@bf=\textbf\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@vm}{\let\PYG@bf=\textbf\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@sa}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@sb}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@sc}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@dl}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@sd}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@s2}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@se}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@sh}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@si}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@sx}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@sr}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@s1}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@ss}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
|
||||
\@namedef{PYG@tok@ch}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}}
|
||||
\@namedef{PYG@tok@cm}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}}
|
||||
\@namedef{PYG@tok@cpf}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}}
|
||||
\@namedef{PYG@tok@c1}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}}
|
||||
|
||||
\def\PYGZbs{\char`\\}
|
||||
\def\PYGZus{\char`\_}
|
||||
\def\PYGZob{\char`\{}
|
||||
\def\PYGZcb{\char`\}}
|
||||
\def\PYGZca{\char`\^}
|
||||
\def\PYGZam{\char`\&}
|
||||
\def\PYGZlt{\char`\<}
|
||||
\def\PYGZgt{\char`\>}
|
||||
\def\PYGZsh{\char`\#}
|
||||
\def\PYGZpc{\char`\%}
|
||||
\def\PYGZdl{\char`\$}
|
||||
\def\PYGZhy{\char`\-}
|
||||
\def\PYGZsq{\char`\'}
|
||||
\def\PYGZdq{\char`\"}
|
||||
\def\PYGZti{\char`\~}
|
||||
% for compatibility with earlier versions
|
||||
\def\PYGZat{@}
|
||||
\def\PYGZlb{[}
|
||||
\def\PYGZrb{]}
|
||||
\makeatother
|
||||
|
Binary file not shown.
After Width: | Height: | Size: 20 KiB |
Reference in New Issue
Block a user