Files
uni/third/semester1/CT331: Programming Paradigms/assignments/assignment3/latex/_minted-CT331-Assignment-3/DC7A695E1290A2BA9F1C16CD2FB60E2B70E65A2BB389682C5F0235C03AA763AA.pygtex

15 lines
1.4 KiB
Plaintext

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZpc{} call the helper predicate with the list to be reversed and an empty Accumulator to build up}
\PYG{n+nf}{reverseList}\PYG{p}{(}\PYG{n+nv}{List}\PYG{p}{,} \PYG{n+nv}{Reversed}\PYG{p}{)} \PYG{p}{:\PYGZhy{}} \PYG{n+nf}{reverseListHelper}\PYG{p}{(}\PYG{n+nv}{List}\PYG{p}{,} \PYG{p}{[],} \PYG{n+nv}{Reversed}\PYG{p}{).}
\PYG{c+c1}{\PYGZpc{} base case fact: when the list to reverse is empty, the accumulator is the reversed list}
\PYG{n+nf}{reverseListHelper}\PYG{p}{([],} \PYG{n+nv}{Accumulator}\PYG{p}{,} \PYG{n+nv}{Accumulator}\PYG{p}{).}
\PYG{c+c1}{\PYGZpc{} recurse with the tail after prepending the head to the accumulator}
\PYG{n+nf}{reverseListHelper}\PYG{p}{([}\PYG{n+nv}{Head} \PYG{p}{|} \PYG{n+nv}{Tail}\PYG{p}{],} \PYG{n+nv}{Accumulator}\PYG{p}{,} \PYG{n+nv}{Reversed}\PYG{p}{)} \PYG{p}{:\PYGZhy{}} \PYG{n+nf}{reverseListHelper}\PYG{p}{(}\PYG{n+nv}{Tail}\PYG{p}{,} \PYG{p}{[}\PYG{n+nv}{Head} \PYG{p}{|} \PYG{n+nv}{Accumulator}\PYG{p}{],} \PYG{n+nv}{Reversed}\PYG{p}{).}
\PYG{l+s+sAtom}{?\PYGZhy{}} \PYG{n+nf}{reverseList}\PYG{p}{([}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{],} \PYG{n+nv}{X}\PYG{p}{).}
\PYG{l+s+sAtom}{?\PYGZhy{}} \PYG{n+nf}{reverseList}\PYG{p}{([}\PYG{l+m+mi}{1}\PYG{p}{],} \PYG{n+nv}{X}\PYG{p}{).}
\PYG{l+s+sAtom}{?\PYGZhy{}} \PYG{n+nf}{reverseList}\PYG{p}{([],} \PYG{n+nv}{X}\PYG{p}{).}
\end{Verbatim}