\documentclass[noanswers,addpoints,x11names,11pt]{exam}
\usepackage[xetex,text={6.5in,9in},centering]{geometry}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{caption2}
\usepackage{soul}
\usepackage{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{datetime}
\usepackage{enumerate}
\usepackage{xltxtra}

\usepackage{graphicx}
\usepackage[xetex]{hyperref}
\usepackage{makecell}
\usepackage{multicol}
\usepackage{multirow}

\usepackage{enumitem}
% compact itemized and then enumerated
\setlist{leftmargin=*,
  noitemsep,
  topsep=5pt,
  parsep=0pt,
  partopsep=0pt,
}

\setlist[enumerate]{leftmargin=*,
  itemsep=2pt,
  topsep=0pt,
  parsep=0pt,
  partopsep=0pt,
}

\setlist[description]{leftmargin=*,
  itemsep=2pt,
  topsep=0pt,
  parsep=0pt,
  partopsep=0pt,
}

\usepackage{tabu}
\usepackage{textcomp}
\usepackage{tikz}
\usetikzlibrary{arrows,automata,decorations.pathmorphing,backgrounds,positioning,fit}
\usepackage{pgf-umlcd}

\usepackage{fontspec}
\IfFontExistsTF{Gentium Book Plus}{
  \setmainfont{Gentium Book Plus}
}{
  \setmainfont{Times New Roman}
}

\IfFontExistsTF{Comic Code}{
  \setmonofont{Comic Code}[
  Scale = 0.85,
  ]
}{
}

\usepackage{float}
\usepackage{listings}
\lstset{
  basicstyle=\ttfamily,
  numberstyle=\footnotesize,
  language=java,
}

\newcommand{\checkpoint}{{\Large $\checkmark$} }

% classname
\newcommand{\semester}{Spring 2025}
\newcommand{\className}{Programming Languages}
\newcommand{\classNumber}{CIS~443}
\newcommand{\classFullName}{\classNumber~\className}

\newcommand{\assignmentNumber}{01}
\newcommand{\assignmentName}{Quadratic}
\newcommand{\assignmentDescription}{Using REPL}

\newcommand{\assignmentTitle}{\texttt{p\assignmentName} --- \assignmentDescription}

\newcommand{\gitRepoName}{p\assignmentNumber-\assignmentName}

\renewcommand{\dateseparator}{-}
\yyyymmdddate

% /classname

\pagestyle{headandfoot}
\firstpageheader{\classFullName\\\semester}{\assignmentName\\~}{\today\\~}
\runningheader{\assignmentName}{}{\today}

\newenvironment{showExample}[1]{
  \begingroup
  \renewcommand{\solutiontitle}{\noindent\textbf{Example:}\enspace}
  \printanswers
  \begin{solution}[#1]
  }{
  \end{solution}
  \endgroup
}

\definecolor{javafile}{rgb}{0.90, 0.87, 0.7}
\definecolor{schemefile}{rgb}{0.70, 0.87, 0.7}
\definecolor{inputfile}{rgb}{1.0, 0.7, 0.6}
\definecolor{configfile}{rgb}{0.75, 0.84, 0.66}
\definecolor{outputlisting}{rgb}{0.66, 0.75, 0.84}

\def\changemargin#1#2{\list{}{\rightmargin#2\leftmargin#1}\item[]}
\let\endchangemargin=\endlist

\begin{document}
\pointsinrightmargin{}

\begin{center}
  \textbf{\assignmentTitle{}}
\end{center}

\section*{Learning Outcomes}
After completing this program, students will be able to
\begin{itemize}
\item Use \lstinline{define} to define named values.
\item Use \lstinline{quote} and its tick mark shorthand to enter
  literal values.
\item Use \lstinline{cons}, \lstinline{car}, \lstinline{cdr}, to build
  and manipulate Scheme lists.
\end{itemize}

\section*{Racket}
I will \textbf{only} test code using \lstinline{racket} with the
following command:

\begin{lstlisting}[language={},backgroundcolor=\color{outputlisting}]
$ racket -I simply-scheme -f <your-file.scm> -i
\end{lstlisting}

\noindent
\textbf{If} your code

\begin{description}
\item[Loads with errors] I stop grading and give a 0.0.
\item[Fails to run due to interpreter errors] I stop grading and give
  0.0.
\item[Produces incorrect results] I grade with the generated results
  against the requirements in the assignment.
\end{description}

\textbf{You} are obligated to test your code on an appropriate
interpreter. Racket 8.15 is installed in the lab and
\lstinline{drracket} is available on most major platforms.

\section*{Procedure}
Read the \textbf{whole} assignment. This is important for
  \emph{every} assignment: it puts the task into your brain so that it
  can begin working on answering the questions. Of particular interest
  when you read are the SLO (first section above); gives you the links
  between the assignment to the big picture (learning computer
  science).

\subsection*{Expressions in Scheme}

Scheme is a (relatively) functional language. Each \emph{expression}
is written in a \textbf{prefix} notation: the first element in a list
is interpreted as a \emph{function} to be applied to the remaining
elements in the list:

\begin{lstlisting}[language={scheme},backgroundcolor=\color{schemefile}]
> 17
17
> (first '(a b c))
'a
> (* (+ 5 3) (- 11 4))
56
\end{lstlisting}

As shown in the third example, list evaluation continues,
\emph{recursively}, if any parameters are themselves lists.
Numeric literals evaluate to their own value.

\subsection*{Setting Symbols in Scheme}

\textbf{Note} the following bit deviates from the pedagogy of
\emph{Simply Scheme} by showing you how to set symbols to have
non-function values. It violates the idea of \emph{functional purity}.

Scheme has a \emph{special form} (which you can treat just like a
\textbf{function} for the moment), \lstinline{define}, which associate
a value with a name. If, in Java, you would write something
like \lstinline{a = 17} or \lstinline{b = (5 + 3) * (11 - 4)}, you
would use the following in Scheme:

\begin{lstlisting}[language={scheme},backgroundcolor=\color{schemefile}]
> (define a 17)
> (define b (* (+ 5 3) (- 11 4)))
> a
17
> (+ b 9)
65
\end{lstlisting}

\textbf{Note} that \lstinline{define} is also used to define named
functions.

It is odd that \lstinline{define} does not return a value. This is a
peculiarity of Scheme: each Scheme implementation can return whatever
they want for \lstinline{define}; Chicken Scheme chose nothing.

\subsection*{Lists in Scheme}

When we give a list to Scheme, it assumes the first
element names a \emph{function} and the rest of the list can be
treated as \emph{parameters}. This is what happens whenever Scheme
\textbf{evaluates} a list.

Sometimes we want to give Scheme an expression (variable name, list)
\emph{without} it being evaluated. This is done by using
the \lstinline{quote} special form around the expression:

\begin{lstlisting}[language={scheme},backgroundcolor=\color{schemefile}]
> (quote (* (+ 5 3) (- 11 4)))
(* (+ 5 3) (- 11 4))
\end{lstlisting}

Typing the \lstinline{quote} around an expression is tedious
\emph{and} it adds another set of parentheses, so there is a shorthand
for it using the tick mark, \lstinline{'}.

\begin{lstlisting}[language={scheme},backgroundcolor=\color{schemefile}]
> '(* (+ 5 3) (- 11 4))
(* (+ 5 3) (- 11 4))
\end{lstlisting}

\begin{questions}

\question{} You will be turning in a Scheme source file through the
  classroom management system. Put code at the top of the file to set
  symbols that identify you:

\begin{parts}
\part{} Set the symbol \lstinline{full-name} to a list of your first and last
names. Each of the component names will be \emph{symbols} in the list.


\part{} Set the symbol \lstinline{age} to your age, in years, at your last
birthday. (If, for any reason, you do not want to share your actual
age, put in a semi-plausible age and I will never know the difference.)

\part{} Set the symbol \lstinline{age-in-months} to the equivalent age in
  months, using Scheme to \emph{calculate} the number of months you were
old on that date.

\part{} Set the symbol \lstinline{expression-for-age-in-months} to the
expression that you used to calculate your age in months. It should be
a list containing a valid Scheme expression.
\end{parts}

\subsection*{Lists}
Everything in Scheme is either an \emph{atom} or a \emph{list}. An
atom is a symbol, a number, or a string. A list is either empty or a
\emph{head} followed by a list. \textbf{Simply Scheme} manipulates
both lists and symbol names the same way.

Every expression you have typed in to your file or the REPL is either
an atom or a list (these are not being typed into the REPL):

\begin{lstlisting}[language={scheme},backgroundcolor=\color{schemefile}]
(* (+ 5 3) (- 11 4))  ; a list that evaluates to a number
(define kilo (* (+ 5 3) (- 11 4))) ; a list that sets the value of
                                   ; a symbol to a number
kilo ; an atom, a symbol, that evaluates to its defined value
999 ; an atom, a number, that evaluates to itself

(define pets '(knight scooby rex fido willy))
; a list that sets a symbol's value to a list

pets ; evaluates to the list (defined vaule of symbol)
\end{lstlisting}

How can we manipulate the list
\lstinline{'(knight scooby rex fido willy)}? Using \lstinline{simply-scheme}:

\begin{lstlisting}[language={scheme},backgroundcolor=\color{schemefile}]
> (first '(knight scooby rex fido willy))
'knight
> (butfirst '(knight scooby rex fido willy))
'(scooby rex fido willy)
> (last '(knight scooby rex fido willy))
'willy
> (butlast '(knight scooby rex fido willy))
'(knight scooby rex fido)
> (butlast 'willy)
> 'will
\end{lstlisting}

The opposite of taking a word (atom) or a sentence (list) apart is to
assemble one:

\begin{lstlisting}[language={scheme},backgroundcolor=\color{schemefile}]
> (sentence 'garfield '(knight scooby rex fido willy))
'(garfield knight scooby rex fido willy)
> (word 'te 'le 'phone)
'telephone
\end{lstlisting}

Notice that \lstinline{garfield} had to be quoted because all
parameters to a function (all elements after the first in an evaluated
list) will be evaluated. So, without the quote, \lstinline{garfield}
becomes whatever it is \lstinline{define}d to be. With the quote, it
is just the symbol.

The \emph{empty list} is \lstinline{()} and it evaluates to itself.

We can build a list, say \lstinline{(cat in the hat)} with
\lstinline{sentence}:

\begin{lstlisting}[language={scheme},backgroundcolor=\color{schemefile}]
> (sentence 'cat 'in 'the 'hat)
'(cat in the hat)
\end{lstlisting}

\question{} Add code to your
\lstinline{.scm}:
\begin{itemize}
\item Use the \lstinline{first} and \lstinline{butfirst} (and/or
  \lstinline{last}/\lstinline{butlast}) to extract
  \lstinline{(+ 5 3)} from the list\\ \lstinline{(* (+ 5 3) (- 11 4))}.

\item Extract the
  \lstinline{11} from the list above. You will need to get the third
  element from the top list and then get the second element from that.

\item Build the sentence \lstinline{(one fish two fish red fish blue fish)}
  using quotes \textbf{only} on individual symbols.

\item Define \lstinline{k} as the list \lstinline{(oh the places)}.
  Then evaluate the following two expressions and explain
  the difference in results:
\begin{lstlisting}[language={scheme},backgroundcolor=\color{schemefile}]
(sentence 'k '(you will go))
(sentence k '(you will go))
\end{lstlisting}

\question{} Add this to your file.
\begin{lstlisting}[language={scheme},backgroundcolor=\color{schemefile}]
(define better-pets '((cat knight) (dog scooby) (fish rex) (moose fido) (orca willy)))
\end{lstlisting}
  \begin{itemize}
  \item Write a Scheme expression that uses \lstinline{better-pets} and
  evaluates to \lstinline{(moose fido)}.
\item Write an expression that evaluates to the type of animal
  \lstinline{willy} is. Not a function.
\item Write an expression that evaluates to the name (not a
  \emph{list} with the name but just the name) of the fish.
  \end{itemize}
\end{itemize}
\end{questions}


\section*{Submit through Classroom Management System}
Submit your \emph{commented} (name, answers to questions) Scheme code
through the classroom management system.
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
