2025-01-19 15:11:36 -08:00

159 lines
4.1 KiB
TeX
Executable File

\section{Turing}
\definition{}
An \textit{esoteric programming langauge} is a programming langauge made for fun. \par
We'll work with two such languages today: \textit{Turing} and \textit{Befunge}.
\definition{}
\textit{Turing} is one of the most famous esoteric langauges, and is extremely minimalist. \par
It consists only of eight symbols, a data pointer, and an instruction pointer.
Turing's eight symbols are as follows:
\begin{itemize}[itemsep=2mm]
\item \texttt{>} : move the instruction pointer right
\item \texttt{<} : move the instruction pointer left
\item \texttt{+} : increment the current memory cell
\item \texttt{-} : decrement the current memory cell
\item \texttt{.} : output the value of the current cell as a character
\item \texttt{,} : consume one character from input and store it at the current cell
\item \texttt{[} : Jump to the matching \texttt{]} if the current cell is zero. \par
otherwise, execute the next instruction.
\item \texttt{]} : Jump back to the matching \texttt{[} if the current cell is not zero. \par
otherwise, execute the next instruction.
\item All other characters are ignored.
\end{itemize}
\problem{}
Go to \href{https://langs.betalupi.com/}{\texttt{langs.betalupi.com}} and open the Turing editor. \par
Clear the pre-set program in the left side of the screen and replace it with the following:
\begin{center}
\texttt{>+>++>+++>++++>+++++[[-]<]}
\end{center}
\begin{itemize}
\item What does this program do?
\item What does the snippet \texttt{[-]} do?
\item Why is the first cell left as \texttt{0}?
\end{itemize}
\vfill
\pagebreak
\remark{}
Run the program \texttt{+[+]}. Notice that the cell's value \textit{wraps}
from \texttt{127} to \texttt{-128}! \par
\vspace{2mm}
Note that \texttt{[+]} has the same effect as \texttt{[-]}.
\vfill
\problem{}
Write a program that moves the value of the current cell three cells to the right.
\problem{}
Write a program that \textbf{copies} the value of the current cell into the next cell.
\begin{solution}
Use a third \say{scratch} cell.
\end{solution}
\problem{}<bfadd>
Write a program that adds the value of the first cell and the second cell,
leaving the result in the second cell.
\problem{}
Solve \ref{bfadd} again, but multiply the two cells instead of adding.
\problem{}
Write a program that computes the square of the current cell.
\vfill
\pagebreak
\definition{}
Turing uses ASCII to map numbers to characters. \par
You may use
\href{https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html}{\texttt{cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html}}
for reference.
\vspace{5mm}
\problem{}
Write a program that prints the letter \texttt{a}.
\problem{}
Write a program that prints the alphabet as shown below: \par
\begin{center}
\texttt{abcdefghijklmnopqrstuvwxyz}
\end{center}
\problem{}
Modify your program so that it prints the alphabet with alternating case:
\begin{center}
\texttt{aBcDeFgHiJkLmNoPqRsTuVwXyZ}
\end{center}
\problem{}
Write a program that repeats the user's input exactly as it is entered.
\begin{solution}
\texttt{,[.,]}
\end{solution}
\problem{}
Write a program that repeats the user's input in reverse \par
\hint{You may use as many memory cells as you need---the editor will add more as you use them.}
\vfill
\pagebreak
\problem{}
Write a program that prints \say{Hello World}
\problem{}
Write a program that finds the first memory cell that is non-zero.
\problem{}
Write a program that initializes the tape with the following sequence:
\begin{center}
\texttt{3 6 12 24 48 96 192 128 0}
\end{center}
\problem{}
Write a program that decodes run-length encoding.
That is, it turns input like
\begin{center}
\texttt{a3j4k2d5}
\end{center}
into the decoded output
\begin{center}
\texttt{aaajjjjkkddddd}
\end{center}
\vfill
\problem{Bonus}
Write a program that prints a Turing program that prints the original program's input.
\problem{Bonus}
Write a program that outputs all squares between 0 and 400 %10,000
\problem{Bonus}
Write a program that prints the Fibonacci numbers
\begin{solution}
\href{https://brainfuck.org/fib\_explained.b}{\texttt{https://brainfuck.org/fib\_explained.b}}
\end{solution}
\vfill
\pagebreak