Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fa24/mentor11 #217

Merged
merged 3 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Binary file added made/mentor11.pdf
Binary file not shown.
77 changes: 77 additions & 0 deletions made/mentor11.py
Original file line number Diff line number Diff line change
@@ -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)))


Binary file added made/mentor11_meta.pdf
Binary file not shown.
Binary file added made/mentor11_sol.pdf
Binary file not shown.
114 changes: 114 additions & 0 deletions made/mentor11_sol.py
Original file line number Diff line number Diff line change
@@ -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)))


35 changes: 35 additions & 0 deletions src/fa24/mentor11.tex
Original file line number Diff line number Diff line change
@@ -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}
5 changes: 5 additions & 0 deletions topics/hof/hard/compound.tex
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@
"""
def g(x, n):
new_comp = ____________________________

while n > 0:
print(____________________________)

new_comp = (lambda save_comp: \
_______________________)(____________)

___________________________________________________

return ______________________________________________

return ___________________________________
\end{lstlisting}

Expand Down