2023-02-05 21:02:05 -08:00
|
|
|
\section{Challenges}
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-10-04 09:42:09 -07:00
|
|
|
Do \ref{Yfac} first, then finish the rest in any order.
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\problem{}<Yfac>
|
2023-10-04 09:42:09 -07:00
|
|
|
Design a recursive factorial function using $Y$.
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\vfill
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\problem{}
|
2023-10-04 09:42:09 -07:00
|
|
|
Design a non-recursive factorial function. \par
|
2023-10-05 11:23:03 -07:00
|
|
|
\note{This one is easier than \ref{Yfac}, but I don't think it will help you solve it.}
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\problem{}
|
2023-10-04 09:42:09 -07:00
|
|
|
Using pairs, make a \say{list} data structure. Define a GET function, so that $\text{GET}~L~n$ reduces to the nth item in the list.
|
|
|
|
$\text{GET}~L~0$ should give the first item in the list, and $\text{GET}~L~1$, the \textit{second}. \par
|
2023-02-05 21:02:05 -08:00
|
|
|
Lists have a defined length, so you should be able to tell when you're on the last element.
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\problem{}
|
2023-10-05 11:23:03 -07:00
|
|
|
Solve \ref{decrement} without using $H$.
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\problem{}
|
|
|
|
Write a MOD $a$ $b$ function that reduces to the remainder of $a \div b$.
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\begin{solution}
|
|
|
|
\textbf{Factorial with recursion:}
|
|
|
|
\vspace{3ex}
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
$\text{FAC} = \lm yn.[Z~n][1][\text{MULT}~n~(y~(\text{D}~n))]$
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\linehack{}
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\textbf{Factorial without recursion:}
|
|
|
|
\vspace{3ex}
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
$\text{FAC}_0 = \lm p .
|
|
|
|
\Bigl\langle~~
|
|
|
|
\Bigl[D~(p~t)\Bigr]
|
|
|
|
~,~
|
|
|
|
\Bigl[\text{MULT}~(p~T)~(p~F)\Bigr]
|
|
|
|
~~\Bigr\rangle
|
|
|
|
$
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\vspace{2ex}
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
$
|
|
|
|
\text{FAC} = \lm n .
|
|
|
|
\bigl( n~\text{FAC}_0~\langle n, 1 \rangle \bigr)
|
|
|
|
$
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\linehack{}
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\textbf{Lists:}
|
|
|
|
\vspace{3ex}
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
One possible implementation is
|
|
|
|
$\Bigl\langle
|
|
|
|
\langle \text{is last} ~,~ \text{item} \rangle
|
|
|
|
~,~
|
|
|
|
\text{next}...
|
|
|
|
\Bigr\rangle$, where:
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\vspace{1ex}
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-10-04 09:42:09 -07:00
|
|
|
\say{is last} is a boolean, true iff this is the last item in the list. \par
|
|
|
|
\say{item} is the thing you're storing \par
|
|
|
|
\say{next...} is another one of these list fragments.
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-10-05 11:23:03 -07:00
|
|
|
It doesn't matter what \say{next} is in the last list fragment. A dedicated \say{is last} slot allows us to store arbitrary functions in this list.
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\vspace{1ex}
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-10-04 09:42:09 -07:00
|
|
|
Here, $\text{GET} = \lm nL.[(n~L~F)~T~F$]
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
This will break if $n$ is out of range.
|
|
|
|
\end{solution}
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\vfill
|
2022-11-13 13:02:25 -08:00
|
|
|
|
2023-02-05 21:02:05 -08:00
|
|
|
\problem{Bonus}
|
2023-10-04 09:42:09 -07:00
|
|
|
Play with \textit{Lamb}, an automatic lambda expression evaluator. \par
|
2023-02-05 21:02:05 -08:00
|
|
|
\url{https://git.betalupi.com/Mark/lamb}
|