Files
uni/third/semester1/CT331: Programming Paradigms/assignments/assignment2/latex/_minted-CT331-Assignment-2/4F731F09E1CEA031D270F0F3CD510B3F5F05F44EA61ED24E098FB0159115E6FF.pygtex

29 lines
2.0 KiB
Plaintext

\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}