\documentclass[11pt]{article} \input{exer-defs} %\renewcommand{\thepage}{} \begin{document} \begin{center}\large\bf CIS 705 --- Programming Languages --- Spring 2009 \end{center} \begin{center}\Large\bf Assignment 2 \end{center} \begin{center}\large\bf Due by 2:30 p.m.\ on Thursday, February 26 \end{center} \begin{center} The context for this assignment is Chapter~2 of \emph{DAWOC} and\\the core and standard OCaml libraries. \end{center} \section*{Definitions} \begin{itemize} \item The \emph{sum} of a list of integers $\mathit{xs}$ is the result of adding together all of the elements of $\mathit{xs}$ (yielding $0$, if $\mathit{xs}$ is empty). Here, as elsewhere in this assignment, you can ignore the possibility of overflow. \item A list $\mathit{xs}$ is a \emph{sublist} of a list $\mathit{ys}$ iff there are lists $\mathit{us}$ and $\mathit{vs}$ such that $\mathit{us} \mathbin{\mathtt{@}} \mathit{xs} \mathbin{\mathtt{@}} \mathit{vs} = \mathit{ys}$. \item A list $\mathit{xs}$ is a \emph{proper sublist} of a list $\mathit{ys}$ iff $\mathit{xs}$ is a sublist of $\mathit{ys}$, and $\mathit{xs}\neq\mathit{ys}$. \item A list of integers $\mathit{xs}$ is \emph{balanced} iff \begin{itemize} \item the sum of $\mathit{xs}$ is $0$, \item $\mathit{xs}$ is nonempty and \item no proper, nonempty sublist of $\mathit{xs}$ has a sum of $0$. \end{itemize} \end{itemize} \section*{Finding Balanced Sublists} The goal of this assignment is to write an OCaml program for finding the balanced sublists of a list of integers. You should define a function \begin{verbatim} val balanced : int list -> int list list \end{verbatim} such that $\texttt{balanced}\;\mathit{xs}$ is the list of all balanced sublists of $\mathit{xs}$, listened in strictly ascending order according to OCaml's standard ordering (as implemented by \texttt{compare} and \texttt{<}). For example, \texttt{balanced[1;1;-1;1]} should evaluate to \texttt{[[-1;1];[1;-1]]}. \section*{Assessment} Your program will be assessed according to three criteria: \begin{itemize} \item \textbf{Correctness}. Test your program carefully, but also try to prove to yourself that it is correct. (You don't have to submit evidence of testing, or to write your proof down.) \item \textbf{Style}. Program in a strictly functional style, without using the imperative features of Chapter~3 of \emph{DAWOC}. Format your program in a way that makes its structure clear. Choose meaningful names for functions, and choose other identifiers with care. Document your functions by abstractly explaining their input/output behavior. (For a given function, say that if we know that its input has some property, that its output will have some other property). \item \textbf{Efficiency}. Try to make your program efficient, but don't sacrifice correctness or style in doing so. \end{itemize} \section*{Submission} Begin your program with a comment containing your name. Submit your program by emailing it to me. I will acknowledge receiving it. Make sure that you retain an electronic copy of your program. \end{document}