diff --git a/Makefile b/Makefile index caaf0176..bffde837 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ # You should only have to modify RELEASED and SOLUTIONS. # Also change SRC once per semester. -RELEASED = mentor09 -SOLUTIONS = mentor09 +RELEASED = mentor11 +SOLUTIONS = mentor11 DST = made SRC = src/fa24 diff --git a/made/mentor11.pdf b/made/mentor11.pdf new file mode 100644 index 00000000..9d2fdb9b Binary files /dev/null and b/made/mentor11.pdf differ diff --git a/made/mentor11.py b/made/mentor11.py new file mode 100644 index 00000000..080bd4ae --- /dev/null +++ b/made/mentor11.py @@ -0,0 +1,77 @@ +(if (/ 1 0) 1 0) +(if 1 1 (/ 1 0)) +(if 0 (/ 1 0) 1) +(and 1 #f (/ 1 0)) +(and 1 2 3) +(or #f #f 0 #f (/ 1 0)) +(or #f #f (/ 1 0) 3 4) +(and (and) (or)) + + +scm> (define c 2) +scm> (eval 'c) +scm> '(cons 1 nil) +scm> (eval '(cons 1 nil)) +scm> (eval (list 'if '(even? c) 1 2)) + + +; The hailstone sequence starting at seed = 10 would be +; 10 => 5 => 16 => 8 => 4 => 2 => 1 + +; Doctests +> (hailstone 10 0) +10 +> (hailstone 10 1) +5 +> (hailstone 10 2) +16 +> (hailstone 5 1) +16 + +(define (hailstone seed n) + + + + + + + + + + +) +def hailstone(seed, n): + if n == 0: + return seed + if seed % 2 == 0: + return hailstone(seed//2, n - 1) + else: + return hailstone(3*seed + 1, n - 1) + + + True or False: 3.14 is an atomic expression. + True or False: pi is an atomic expression. + True or False: - is an atomic expression. + True or False: // is an atomic expression. + True or False: 'b' is an atomic expression. + True or False: "is this atomic?" is an atomic expression. + + + scm> (+ 1 (* 3 4)) + scm> (* 1 (+ 3 4)) + scm> (/ 2) + scm> (- 2) + scm> (* 4 (+ 3 (- 2 (/ 1)))) + + + scm> (if 2 3 4) + scm> (if 0 3 4) + scm> (- 5 (if #f 3 4)) + scm> (cond ((< -5 -7) 3) + (else 4)) + + + scm> (and #t (= 3 3) (> (- 61 42) (+ 61 42))) + scm> (or #f (< 3 3) (< (- 61 42) (+ 61 42))) + + diff --git a/made/mentor11_meta.pdf b/made/mentor11_meta.pdf new file mode 100644 index 00000000..627f79b7 Binary files /dev/null and b/made/mentor11_meta.pdf differ diff --git a/made/mentor11_sol.pdf b/made/mentor11_sol.pdf new file mode 100644 index 00000000..47ef942b Binary files /dev/null and b/made/mentor11_sol.pdf differ diff --git a/made/mentor11_sol.py b/made/mentor11_sol.py new file mode 100644 index 00000000..3444fa02 --- /dev/null +++ b/made/mentor11_sol.py @@ -0,0 +1,114 @@ +(if (/ 1 0) 1 0) +Error: Zero Division +(if 1 1 (/ 1 0)) +1 +(if 0 (/ 1 0) 1) +Error: Zero Division +(and 1 #f (/ 1 0)) +#f +(and 1 2 3) +3 +(or #f #f 0 #f (/ 1 0)) +0 +(or #f #f (/ 1 0) 3 4) +Error: Zero Division +(and (and) (or)) +#f + + +scm> (define c 2) +c +scm> (eval 'c) +2 +scm> '(cons 1 nil) +(cons 1 nil) +scm> (eval '(cons 1 nil)) +(1) +scm> (eval (list 'if '(even? c) 1 2)) +1 + + +; The hailstone sequence starting at seed = 10 would be +; 10 => 5 => 16 => 8 => 4 => 2 => 1 + +; Doctests +> (hailstone 10 0) +10 +> (hailstone 10 1) +5 +> (hailstone 10 2) +16 +> (hailstone 5 1) +16 + +(define (hailstone seed n) + + + + + + + + + + +) +(define (hailstone seed n) + (if (= n 0) + seed + (if (= 0 (remainder seed 2)) + (hailstone + (quotient seed 2) + (- n 1)) + (hailstone + (+ 1 (* seed 3)) + (- n 1))))) + +; Alternative solution with cond + +(define (hailstone seed n) + (cond + ((= n 0) seed) + ((= 0 (remainder seed 2)) + (hailstone + (quotient seed 2) + (- n 1))) + (else + (hailstone + (+ 1 (* seed 3)) + (- n 1))))) +def hailstone(seed, n): + if n == 0: + return seed + if seed % 2 == 0: + return hailstone(seed//2, n - 1) + else: + return hailstone(3*seed + 1, n - 1) + + + True or False: 3.14 is an atomic expression. + True or False: pi is an atomic expression. + True or False: - is an atomic expression. + True or False: // is an atomic expression. + True or False: 'b' is an atomic expression. + True or False: "is this atomic?" is an atomic expression. + + + scm> (+ 1 (* 3 4)) + scm> (* 1 (+ 3 4)) + scm> (/ 2) + scm> (- 2) + scm> (* 4 (+ 3 (- 2 (/ 1)))) + + + scm> (if 2 3 4) + scm> (if 0 3 4) + scm> (- 5 (if #f 3 4)) + scm> (cond ((< -5 -7) 3) + (else 4)) + + + scm> (and #t (= 3 3) (> (- 61 42) (+ 61 42))) + scm> (or #f (< 3 3) (< (- 61 42) (+ 61 42))) + + diff --git a/src/fa24/mentor11.tex b/src/fa24/mentor11.tex new file mode 100644 index 00000000..60c54dd8 --- /dev/null +++ b/src/fa24/mentor11.tex @@ -0,0 +1,35 @@ +\documentclass{exam} +\usepackage{../commonheader} +\lstset{language=Scheme} + +\discnumber{9} +\title{\textsc{Introduction to Scheme}} +\date{November 4 -- November 8, 2024} + +\begin{document} +\maketitle +\begin{guide} + \textbf{Recommended Timeline} + \begin{itemize} + \item Scheme Mini-Lecture: 10-15 mins + \item WWSD: 10-15 mins + \item Hailstone: 10 mins + \end{itemize} + +\begin{meta} + Since it was midterm week and students basically have not had lecture, this worksheet is going just be a short intro to scheme. + + Monday sections students will just have had lecture and may not have completely covered all topics on this worksheets so go slow as you need. There is plenty of time allocated in the worksheet for going slow. +\end{meta} +\end{guide} + + +\section{Scheme} +\subimport{../../topics/scheme/text/}{scheme-guided-overview.tex} +\begin{questions} + \subimport{../../topics/scheme/easy/wwsd/}{if-and-or.tex} + \subimport{../../topics/scheme/easy/wwsd/}{define-eval.tex} + \subimport{../../topics/scheme/easy/}{hailstone.tex} +\end{questions} + +\end{document} \ No newline at end of file diff --git a/topics/hof/hard/compound.tex b/topics/hof/hard/compound.tex index e70cdb19..e32e7a3c 100644 --- a/topics/hof/hard/compound.tex +++ b/topics/hof/hard/compound.tex @@ -20,12 +20,17 @@ """ def g(x, n): new_comp = ____________________________ + while n > 0: print(____________________________) + new_comp = (lambda save_comp: \ _______________________)(____________) + ___________________________________________________ + return ______________________________________________ + return ___________________________________ \end{lstlisting}