<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.ubc.ca/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=SteveWolfman</id>
	<title>UBC Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.ubc.ca/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=SteveWolfman"/>
	<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/Special:Contributions/SteveWolfman"/>
	<updated>2026-08-10T16:44:49Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.9</generator>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2013WT1&amp;diff=249830</id>
		<title>Course:CPSC311/2013WT1</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2013WT1&amp;diff=249830"/>
		<updated>2013-08-29T20:58:44Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: /* Handy List of All Our Pages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== CPSC 311 2013W1: Definition of Programming Languages ==&lt;br /&gt;
&lt;br /&gt;
Programming languages is a challenging course, littered with deep ideas linked in fascinating ways and occasionally shrouded in strange terminology.  Therefore, you have this wiki to help each other (and the teaching staff!) keep up with course issues. &lt;br /&gt;
&lt;br /&gt;
Furthermore, we&#039;ll have open-book exams, but there&#039;s no textbook.  So, what are we to do?  Well, you make appendices for the midterm, and as long as they&#039;re reasonable in length, we&#039;ll print them out and provide them with the exam.  Obviously, contributing to these would be a Bonus Point Accumulating Activity (i.e., Awesome).&lt;br /&gt;
&lt;br /&gt;
The course wiki is also generally available for anyone to contribute to the class&#039;s learning.  &lt;br /&gt;
&lt;br /&gt;
You may want to start by looking at [[Course:CPSC311/|previous offerings]].&lt;br /&gt;
&lt;br /&gt;
== Handy List of All Our Pages ==&lt;br /&gt;
&lt;br /&gt;
(Empty so far.  How sad!)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;dpl&amp;gt;&lt;br /&gt;
titlematch={{PAGENAME}}/%&lt;br /&gt;
namespace={{NAMESPACE}}&lt;br /&gt;
replaceintitle=$CPSC311/2011WT1/$,&lt;br /&gt;
shownamespace=false&lt;br /&gt;
&amp;lt;/dpl&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:CPSC]]&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2013WT1&amp;diff=249829</id>
		<title>Course:CPSC311/2013WT1</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2013WT1&amp;diff=249829"/>
		<updated>2013-08-29T20:58:17Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: Creating the course wiki.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== CPSC 311 2013W1: Definition of Programming Languages ==&lt;br /&gt;
&lt;br /&gt;
Programming languages is a challenging course, littered with deep ideas linked in fascinating ways and occasionally shrouded in strange terminology.  Therefore, you have this wiki to help each other (and the teaching staff!) keep up with course issues. &lt;br /&gt;
&lt;br /&gt;
Furthermore, we&#039;ll have open-book exams, but there&#039;s no textbook.  So, what are we to do?  Well, you make appendices for the midterm, and as long as they&#039;re reasonable in length, we&#039;ll print them out and provide them with the exam.  Obviously, contributing to these would be a Bonus Point Accumulating Activity (i.e., Awesome).&lt;br /&gt;
&lt;br /&gt;
The course wiki is also generally available for anyone to contribute to the class&#039;s learning.  &lt;br /&gt;
&lt;br /&gt;
You may want to start by looking at [[Course:CPSC311/|previous offerings]].&lt;br /&gt;
&lt;br /&gt;
== Handy List of All Our Pages ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;dpl&amp;gt;&lt;br /&gt;
titlematch={{PAGENAME}}/%&lt;br /&gt;
namespace={{NAMESPACE}}&lt;br /&gt;
replaceintitle=$CPSC311/2011WT1/$,&lt;br /&gt;
shownamespace=false&lt;br /&gt;
&amp;lt;/dpl&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:CPSC]]&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/&amp;diff=249827</id>
		<title>Course:CPSC311/</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/&amp;diff=249827"/>
		<updated>2013-08-29T20:54:17Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CPSC 311 Course Offerings&lt;br /&gt;
&lt;br /&gt;
* [[Course:CPSC311/2013WT1|2013 Winter 1]]&lt;br /&gt;
* [[Course:CPSC311/2011WT1|2011 Winter 1]]&lt;br /&gt;
* [[Course:CPSC311/2010WT1|2010 Winter 1]]&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/&amp;diff=249826</id>
		<title>Course:CPSC311/</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/&amp;diff=249826"/>
		<updated>2013-08-29T20:53:38Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CPSC 311 Course Offerings&lt;br /&gt;
&lt;br /&gt;
* [[Course:CPSC311/2013WT1/|2013 Winter 1]]&lt;br /&gt;
* [[Course:CPSC311/2011WT1/|2011 Winter 1]]&lt;br /&gt;
* [[Course:CPSC311/2010WT1/|2010 Winter 1]]&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/&amp;diff=249825</id>
		<title>Course:CPSC311/</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/&amp;diff=249825"/>
		<updated>2013-08-29T20:53:17Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: Index page for CPSC 311&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CPSC 311 Course Offerings&lt;br /&gt;
&lt;br /&gt;
* [[Course:CPSC311/2013WT1/][2013 Winter 1]]&lt;br /&gt;
* [[Course:CPSC311/2011WT1/][2011 Winter 1]]&lt;br /&gt;
* [[Course:CPSC311/2010WT1/][2010 Winter 1]]&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Final_Exam_Appendix&amp;diff=127555</id>
		<title>Course:CPSC311/2011WT1/Final Exam Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Final_Exam_Appendix&amp;diff=127555"/>
		<updated>2011-12-09T17:22:37Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Final Appendix (Wiki, Student-Generated; credits at end in print version) =&lt;br /&gt;
&lt;br /&gt;
==Formalizing Constraint Generation==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Expression at Node !! Generated Constraints&lt;br /&gt;
|-&lt;br /&gt;
| n, where n is a numeral  ||  [[n]] = number&lt;br /&gt;
|-&lt;br /&gt;
| true  || [[true]]=boolean&lt;br /&gt;
|-&lt;br /&gt;
| false || [[false]]=boolean&lt;br /&gt;
|-&lt;br /&gt;
| (add1 e) || [[(add1 e)]]=number [[e]]=number&lt;br /&gt;
|-&lt;br /&gt;
| (+ e1 e2) || [[(+ e1 e2)]]=number [[e1]] = number [[e2]] = number&lt;br /&gt;
|-&lt;br /&gt;
| (zero? e) || [[(zero? e)]]=boolean [[e]] = number&lt;br /&gt;
|-&lt;br /&gt;
| (ncons e1 e2) ||  [[(ncons e1 e2)]]=list(num) [[e1]] = number [[e2]] = list(num)&lt;br /&gt;
|-&lt;br /&gt;
| (nﬁrst e) || [[(nﬁrst e)]]=number [[e]] = list(num)&lt;br /&gt;
|-&lt;br /&gt;
| (nrest e) || [[(nrest e)]]=list(num) [[e]] = list(num)&lt;br /&gt;
|-&lt;br /&gt;
| (nempty? e) || [[(nempty? e)]]=boolean [[e]] = list(num)&lt;br /&gt;
|-&lt;br /&gt;
| nempty || [[nempty]]=list(num)&lt;br /&gt;
|-&lt;br /&gt;
| (if c t e) || [[(if c t e)]]=[[t]] [[(if c t e)]]=[[e]] [[c]]=boolean&lt;br /&gt;
|-&lt;br /&gt;
| (lambda (x) b) || [[(lambda (x) b)]]= [[x]]→[[b]]&lt;br /&gt;
|-&lt;br /&gt;
| (f a) || [[f ]] = [[a]]→[[(f a)]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Unification Algorithm ==&lt;br /&gt;
&lt;br /&gt;
Begin with an empty substitution. Push all the constraints&lt;br /&gt;
onto a stack. If the stack is empty, return the substitution; otherwise, pop the constraint X = Y off the stack:&lt;br /&gt;
&lt;br /&gt;
# If X and Y are identical identiﬁers, do nothing.&lt;br /&gt;
# If X is an identiﬁer, replace all occurrences of X by Y both on the stack and in the substitution, and add X 7→ Y to the substitution.&lt;br /&gt;
# If Y is an identiﬁer, replace all occurrences of Y by X both on the stack and in the substitution, and add Y 7→ X to the substitution.&lt;br /&gt;
# If X is of the form C(X1,...,Xn) for some constructor C, 4 and Y is of the form C(Y1,...,Yn) (i.e., it has the same constructor), then push Xi = Yi for all 1 ≤ i ≤ n onto the stack.&lt;br /&gt;
# Otherwise, X and Y do not unify. Report an error&lt;br /&gt;
&lt;br /&gt;
= Midterm 1 Material =&lt;br /&gt;
==Multiple Argument Function Conversion Example==&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 (with (f (fun (x y z) (+ x y)))&lt;br /&gt;
   (f 1 2))&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;==&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
  (with (f (fun (x) (fun (y) (fun (z) (+ x y)))))&lt;br /&gt;
    ((f 1) 2)&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Definitions == &lt;br /&gt;
&#039;&#039;&#039;Abstract syntax&#039;&#039;&#039; - idealized syntax, designed for convenient use by the underlying interpreter/compiler; result of parsing a concrete syntax expression &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Concrete syntax&#039;&#039;&#039; - syntax written by the user; consumed by the parser &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Closure&#039;&#039;&#039; - a wrapper that contains an expression (function/ binding) and its environment; expression saved until it needs to be used &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;CPS&#039;&#039;&#039; stands for Continuation-Passing Style&amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Scope&#039;&#039;&#039;: a section of the program code, either as it appears in text or during execution &amp;lt;br/&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Static Scope:&#039;&#039;&#039; a subtree of the abstract syntax tree; an entire expression in the code as it appears in the program text&amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Dynamic Scope:&#039;&#039;&#039; a subtree of the dynamic execution tree; all the code which is dynamically evaluated &amp;quot;underneath&amp;quot; (in the stack) a given expression&amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;First-order Functions&#039;&#039;&#039; are not values in the language. They can only be defined in a designated portion of the program, where they must be given names for use in the remainder of the program.  &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Higher-order Functions&#039;&#039;&#039; can return other functions as values and take other functions as parameters. &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;First-class Functions&#039;&#039;&#039; are values with all the rights of other values. In particular, they can be supplied as the&lt;br /&gt;
value of arguments to functions, returned by functions as answers, constructed as values by expressions, and stored in data structures. &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Deferred Substitution&#039;&#039;&#039; Every time we encounter a substitution (in the form of a with or application), we augment the&lt;br /&gt;
repository with one more entry, recording the identiﬁer&#039;s name and the value (if eager) or expression (if&lt;br /&gt;
lazy) it should eventually be substituted with. We continue to evaluate without actually performing the&lt;br /&gt;
substitution. To prevent dynamic scoping, use closures to store the environment in which the functions are created&amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Cache calculated substitution&#039;&#039;&#039; values are saved the first time they are calculated.  Can be implemented by adding a mutable field to thunkVs to store the cached values.&lt;br /&gt;
&lt;br /&gt;
== Eager vs. Lazy Evaluation ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lazy With&#039;&#039;&#039;- we reduce the named expression to a value only when we need to &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Lazy Functions&#039;&#039;&#039;- we do not reduce the argument to a value until necessary&amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Strictness Points&#039;&#039;&#039;- the points where the implementation of a lazy language forces an expression to reduce to a value (example: in a function application, need to evaluate exactly what function to invoke, can&#039;t just leave it as a function closure) &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Eager&#039;&#039;&#039;- the named expression is reduced to a value before binding it in the environment. &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Recursion implementation by patching the environment ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Fixed-point of a function:&#039;&#039;&#039; a value that, when input to a function, is also output from the function.&lt;br /&gt;
&lt;br /&gt;
interp the bound expression with a fake environment binding, and then patch that binding to point to the result of said interp afterwards using set! mutation.  This creates a cyclic environment that addresses the needs of recursion. The cyclicity ensures that there is always &amp;quot;one more binding&amp;quot; for the recursive function&lt;br /&gt;
when we need it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(rec (bound-id named-exp body)&lt;br /&gt;
  (local ([define new-env (anEnv bound-id (numV 0) env)]&lt;br /&gt;
          [define named-value (interp named-exp new-env)])&lt;br /&gt;
    (begin&lt;br /&gt;
      (set-anEnv-val! new-env named-value)  ;; Patches false out for the correct value&lt;br /&gt;
      (interp body new-env))))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Racket syntax and General Examples==&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;BNF&#039;&#039;&#039;&lt;br /&gt;
  &amp;lt;CFWAE&amp;gt; ::= &amp;lt;num&amp;gt;&lt;br /&gt;
    | {+ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {- &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {* &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {/ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | &amp;lt;id&amp;gt;&lt;br /&gt;
    | {if0 &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {with {&amp;lt;id&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {fun {&amp;lt;id&amp;gt; ...} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {&amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; ...}&lt;br /&gt;
    | {rec {&amp;lt;symbol&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Conditional chains&#039;&#039;&#039;&lt;br /&gt;
  (cond&lt;br /&gt;
      [(number? v) ...]&lt;br /&gt;
      [(boolean? v) ...]&lt;br /&gt;
      [(string? v) ...]&lt;br /&gt;
      [else false])&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Map&#039;&#039;&#039; performs a procedure on all elements of the list in order&lt;br /&gt;
  (map (lambda (x) (* 2 x)) [1, 2, 3]) &lt;br /&gt;
  outputs [2, 4, 6]&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Pattern matching&#039;&#039;&#039;&amp;lt;br&amp;gt;&lt;br /&gt;
  &#039;...&#039; or &#039;___&#039; means 0 or more&amp;lt;br&amp;gt;&lt;br /&gt;
  &#039;..n&#039; or &#039;__n&amp;quot; means n or more&amp;lt;br&amp;gt;&lt;br /&gt;
  &#039;_&#039; matches anything&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
==&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
  (match data&lt;br /&gt;
   [pattern  action]&lt;br /&gt;
      [(? number?) (foo)]              ;; check if data is number &lt;br /&gt;
      [(list a b c) (foo a b c) ]      ;; back-reference of list elements&lt;br /&gt;
      [((and n (? number?))) (foo n)]  ;; check data and back-reference it as n&lt;br /&gt;
      [else (foo)])&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Begin&#039;&#039;&#039; evaluate expressions in order.  Results are ignored except for that of last expression, which is the evaluated value. &lt;br /&gt;
In this case, the side-effect of printing &amp;quot;Hello World&amp;quot; occurs, however the expression overall evaluates to 4.&lt;br /&gt;
  (begin&lt;br /&gt;
    (+ 5 2) (printf &amp;quot;Hello World!\n&amp;quot;) (+ 3 1))&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td width=&amp;quot;40%&amp;quot;&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Functions&#039;&#039;&#039;&amp;lt;br&amp;gt;&lt;br /&gt;
(lambda (x) x) ; first-class value, not used here&amp;lt;br&amp;gt;&lt;br /&gt;
(define f (lambda (x) x))&amp;lt;br&amp;gt;&lt;br /&gt;
(define (f x) x)&amp;lt;br&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Type-case&#039;&#039;&#039; is a conditional construct specific to a particular define-type&lt;br /&gt;
  (type-case WAE expr&lt;br /&gt;
    [num (n) expr]&lt;br /&gt;
    ...)&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Racket code specific to implementation of our languages ==&lt;br /&gt;
&#039;&#039;&#039;Environment:&#039;&#039;&#039; - repository of deferred substitutions&lt;br /&gt;
 (define-type Env&lt;br /&gt;
      [mtEnv]&lt;br /&gt;
      [anEnv (name symbol?)(value CFWAE-Value?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Expression Closures:&#039;&#039;&#039;&lt;br /&gt;
 (define-type CFWAE-Value&lt;br /&gt;
      [numV (num number?)]&lt;br /&gt;
      [exprV (body CFWAE?)(env Env?)] ;; used to hold an unevaluated expression and its environment (used in lazy interp)&lt;br /&gt;
      [closureV (param symbol?)(body CFWAE?)(env Env?)]) ;; used to close over the existing environment to ensure static scope&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Define-type&#039;&#039;&#039; (From assignment 1)&lt;br /&gt;
 (define-type WAE&lt;br /&gt;
  [num (n number?)]&lt;br /&gt;
  [binop (op procedure?) (lhs WAE?) (rhs WAE?)]&lt;br /&gt;
  [with (b Binding?) (body WAE?)]&lt;br /&gt;
  [with* (lob (listof Binding?)) (body WAE?)]&lt;br /&gt;
  [id (name symbol?)])&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Strict code&#039;&#039;&#039; (from lecture)&lt;br /&gt;
 (define (strict val)&lt;br /&gt;
  (if (expV? val)&lt;br /&gt;
      (strict (interp (expV-exp val) (expV-env val)))&lt;br /&gt;
      val))&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Midterm 2 Material =&lt;br /&gt;
&lt;br /&gt;
==State==&lt;br /&gt;
* Entails mutation&lt;br /&gt;
*State is &amp;quot;threaded through&amp;quot; a program in the sense that the state that results from evaluating a given function is the state in which the next sequential function is to be evaluated in.&lt;br /&gt;
*State is inherently dynamic and so isn&#039;t supported by solely the environment, as it has static scope in our implementations.&lt;br /&gt;
** The environment protects static scope while the store tracks the dynamic changes &lt;br /&gt;
** The environment maps identifiers to either locations (or values if there is no store) while the store maps locations to values.&lt;br /&gt;
*Closures do not remember state as function are to be applied using the state that exists where they are applied.&lt;br /&gt;
&lt;br /&gt;
== Mutability implementation data structures ==&lt;br /&gt;
To support mutability in a language, we need two data structures:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Environment:&#039;&#039;&#039; This is our regular substitution list required to support static scope.&lt;br /&gt;
* Maps either:&lt;br /&gt;
** Identifiers to values: Makes sense when we are differentiating between mutable and immutable identifiers.  If it is immutable, it will simply map to a regular value.  If it is mutable, it will map to a boxV value with a store location stored within.&lt;br /&gt;
** Identifiers to store locations: &#039;&#039;Only&#039;&#039; store locations are stored within the environment.  Thus, even for an immutable value, there is always a two-step lookup: the environment maps the identifier to a store location, and the store maps the store location to a value.&lt;br /&gt;
* Identifiers in the environment have &#039;&#039;lexical scope&#039;&#039;: a particular environment belongs to a particular expression scope.&lt;br /&gt;
&#039;&#039;&#039;Store:&#039;&#039;&#039; This is where we keep track of dynamic variable values.&lt;br /&gt;
* Maps location to a value (which can be a boxV pointing to another location itself).&lt;br /&gt;
* The store is &amp;quot;threaded through&amp;quot; the evaluation across sub-expressions.&lt;br /&gt;
* Mutation occurs when we change a value in the store.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Whereas the interpreter employs the same environment for&lt;br /&gt;
both arms of an addition, for instance, it cascades the store from one arm to the next and then back out&lt;br /&gt;
alongside the resulting value.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Meta vs Syntactic Interpreters==&lt;br /&gt;
&#039;&#039;&#039;Syntactic Interpreter&#039;&#039;&#039; - An interpreter that uses the interpreting language to represent only terms of the interpreted language, implementing all the corresponding behavior explicitly&lt;br /&gt;
*doesn&#039;t use Scheme&#039;s implementation of things (like numbers)&lt;br /&gt;
*doesn&#039;t matter how well the interpreting and interpreted languages correspond&lt;br /&gt;
&#039;&#039;&#039;Meta Interpreter&#039;&#039;&#039; - An interpreter that uses language features of the interpreting language to directly implement behavior of the interpreted language&lt;br /&gt;
*easy to write when there is a strong match between interpreted and interpreting language&lt;br /&gt;
*uses Scheme&#039;s implementation of things (closures, procedure applications, numbers, etc)&lt;br /&gt;
&#039;&#039;&#039;Meta-Circular Interpreter&#039;&#039;&#039; - A meta interpreter in which the interpreting and interpreted language are the same&lt;br /&gt;
*Only uses Scheme implementation of things&lt;br /&gt;
&lt;br /&gt;
==Continuations==&lt;br /&gt;
&#039;&#039;&#039;What are they?&#039;&#039;&#039;&lt;br /&gt;
*stack is represented procedurally&lt;br /&gt;
*remember only the result and what is left to do (passes the state along or back to caller)&lt;br /&gt;
*in the presence of tail call optimization, shifts used space from the stack onto the heap (hard to tell whether a particular program requires more heap space or stack space)&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Why?&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Why Not?&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td width=&amp;quot;38%&amp;quot;&amp;gt;&lt;br /&gt;
*Saves on memory and computation (ex. tail recursion summation vs augmenting recursion summation)&lt;br /&gt;
*A way of simulating continuations in a language that lacks native support (useful for web applications, for instance)&lt;br /&gt;
*Makes consumption of stack space explicit; much easier to tell if a function is tail recursive&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
*get rid of machine optimizations by forcing the structure of stack into continuation&lt;br /&gt;
*memory wastage if no tail call optimization (so useless frames are retained on stack even though all calls are tail calls)&lt;br /&gt;
*need access to the source of entire program (in particular, the caller of every CPS function must be CPS&#039;ed)&lt;br /&gt;
*security: what if someone outside knew how to read the stack procedure?&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Continuation Passing Style (end in /k)==&lt;br /&gt;
&#039;&#039;&#039;Main Ideas&#039;&#039;&#039;&lt;br /&gt;
*every function takes an extra argument (its continuation)&lt;br /&gt;
*every argument in a function call must be an identifier, a primitive (such as if), or a lambda expression (not a call to a non-primitive function).&lt;br /&gt;
*every call is a tail call&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Continuation Passing Style rules&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
(1) Convert each function f to f/k and take an extra continuation&lt;br /&gt;
argument.&lt;br /&gt;
&lt;br /&gt;
(2) Find the &amp;quot;next step to take&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
(3) If that step is in tail position, great!  If it&#039;s a value,&lt;br /&gt;
apply the continuation to it.  If it&#039;s a function call, pass the&lt;br /&gt;
continuation to the function call.&lt;br /&gt;
&lt;br /&gt;
(4) If that step is not in tail position, cut it from its current&lt;br /&gt;
location, replace it with a placeholder value (it!), write it in&lt;br /&gt;
tail position (at the front), and give it a lambda that takes that&lt;br /&gt;
placeholder as a parameter and includes the whole rest of the&lt;br /&gt;
function as its body.  Then, repeat from (2) on the whole rest of&lt;br /&gt;
the function!&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example from midterm 2&#039;&#039;&#039;&amp;lt;br&amp;gt;&lt;br /&gt;
We have the following, and want to convert it to CPS&lt;br /&gt;
(assuming (all-pos/k) and (range/k) are correctly implemented,&lt;br /&gt;
and represent CPS versions of all-pos and range, respectively:&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 (define (test-all-pos)&lt;br /&gt;
  (if (all-pos (range -5 5))&lt;br /&gt;
      &#039;fail&lt;br /&gt;
      &#039;pass))&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
==&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 (define (test-all-pos/k k)&lt;br /&gt;
  (range/k -5 5&lt;br /&gt;
   (lambda (range-result)&lt;br /&gt;
    (all-pos/k range-result&lt;br /&gt;
     (lambda (allpos-result)&lt;br /&gt;
      (k (if allpos-result&lt;br /&gt;
            &#039;fail&lt;br /&gt;
            &#039;pass))))))&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&amp;lt;td width=&amp;quot;15%&amp;quot;&amp;gt;&lt;br /&gt;
Note that this assumes &#039;&#039;&#039;if&#039;&#039;&#039; is a primitive that need not be converted into CPS.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 (define (map f l)&lt;br /&gt;
  (if (empty? l)&lt;br /&gt;
     empty&lt;br /&gt;
     (cons (f (first l)) &lt;br /&gt;
              (map f (rest l)))))&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
==&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 (define (map/k f/k list k)&lt;br /&gt;
  (if (empty? list)&lt;br /&gt;
      (k empty)&lt;br /&gt;
      (f/k (first list)&lt;br /&gt;
           (lambda (f-result)&lt;br /&gt;
             (map/k f/k (rest list)&lt;br /&gt;
                    (lambda (r-result)&lt;br /&gt;
                      (k (cons f-result r-result))))))))&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td width=&amp;quot;15%&amp;quot;&amp;gt;&lt;br /&gt;
Note that this assumes &#039;&#039;&#039;empty, first, rest, and cons&#039;&#039;&#039; are all primitives that do not need to be converted.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Variables==&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;th&amp;gt;Call-by-value&amp;lt;/th&amp;gt;&lt;br /&gt;
 &amp;lt;th&amp;gt;Call-by-reference&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td width=30%&amp;gt;&lt;br /&gt;
*evaluated argument is held in a new location,&lt;br /&gt;
**Therefore, changes to the content of that location in the store don&#039;t affect the actual parameter&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
*pass a reference to the actual argument, not the value&lt;br /&gt;
*updates to the reference within the called procedure will become visible to the calling context&lt;br /&gt;
*cheaper to use (no additional allocation) but introduce problems &lt;br /&gt;
*to implement:&lt;br /&gt;
**create a closure and give it the location of the actual argument&lt;br /&gt;
**uses l-value: env lookup without store lookup&lt;br /&gt;
**any mutations to the formal parameter are now changes to the same location as the actual parameter&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;th&amp;gt;Stateful&amp;lt;/th&amp;gt;&lt;br /&gt;
 &amp;lt;th&amp;gt;Stateless&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
*server maintains state information&lt;br /&gt;
*easier to program&lt;br /&gt;
**don&#039;t need setup and breakdown of state at each interaction&lt;br /&gt;
*ex: FTP&lt;br /&gt;
**interp of each command is relative to history of past commands&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
*does not retain record of prior communication&lt;br /&gt;
*Web application must completely restore state of the computation for each interaction&lt;br /&gt;
*server can handle higher loads&lt;br /&gt;
*server can ignore clients who don&#039;t appear to be active&lt;br /&gt;
*must transmit enough data to resume computation&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Web Programs==&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td width=&amp;quot;37%&amp;quot;&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Receiver&#039;&#039;&#039; - a procedure of one argument representing the pending computation&lt;br /&gt;
*any computation not mentioned in the receiver never gets performed because of the program&#039;s termination after each iteration&lt;br /&gt;
&#039;&#039;&#039;Lifting&#039;&#039;&#039; - make nested procedures into top-level procedures&lt;br /&gt;
* e.g. lambdas become defines&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Make a program Web-Ready&#039;&#039;&#039;&lt;br /&gt;
* 1. Generate receivers that capture pending computations&lt;br /&gt;
* 2. Pass values to receivers instead of returning them&lt;br /&gt;
&#039;&#039;&#039;Implications&#039;&#039;&#039;&lt;br /&gt;
* 1. Order of evaluation&lt;br /&gt;
* 2. Transformation is global&lt;br /&gt;
**all procedures in program must consume an extra receiver&lt;br /&gt;
* 3. Sequentializes the program&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Escaper Lambdas Example==&lt;br /&gt;
From assignment 7 (midterm 2 review), we want to describe the continuation of the&lt;br /&gt;
bolded expression (namely (* (- x 32) 5) ) below:&lt;br /&gt;
&lt;br /&gt;
 (define (fahrenheit-celcius-converter type x)&lt;br /&gt;
   (case type&lt;br /&gt;
     [&#039;Fahrenheit (/ &#039;&#039;&#039;(* (- x 32) 5)&#039;&#039;&#039; 9)]&lt;br /&gt;
     [&#039;Celcius    (+ 32 (/ (* x 9) 5))]))&lt;br /&gt;
 &lt;br /&gt;
   (+ (fahrenheit-celcius-converter &#039;Fahrenheit 32) &lt;br /&gt;
      (fahrenheit-celcius-converter &#039;Fahrenheit 212))&lt;br /&gt;
&lt;br /&gt;
We can use escaper lambdas to do this. The continuation of the bolded expression in&lt;br /&gt;
the first evaluation of the function is:&lt;br /&gt;
 (lambda^ (value) (+ (/ value 9) (fahrenheit-celcius-converter &#039;Fahrenheit 212)))&lt;br /&gt;
&lt;br /&gt;
And the second continuation is:     (lambda^ (value) (+ 0 (/ value 9)))&lt;br /&gt;
&lt;br /&gt;
= Post-Midterm 2 Material =&lt;br /&gt;
&lt;br /&gt;
==Lambda Calculus==&lt;br /&gt;
Consists of:&lt;br /&gt;
*1-parameter functions &lt;br /&gt;
*1-argument function applications&lt;br /&gt;
*identifiers&lt;br /&gt;
&lt;br /&gt;
Shrinking of a language is achieved by showing that a given component of the language may be replaced by a combination of other components making the component unnecessary. (ex. numbers are unnecessary as they can be represented by a function that represents zero and a successor function) By creating a simplified language it is easier to construct proofs showing that the language has desired properties. (ex. correctly implements tail-call optimization, never accesses an array out of bounds, ...)&lt;br /&gt;
&lt;br /&gt;
Lists:&lt;br /&gt;
 cons_ = (lambda (f) (lambda (r) (lambda (selector) ((selector f) r))))&lt;br /&gt;
 head_ = (lambda (p) (p (lambda (f) (lambda (r) f))))&lt;br /&gt;
 rest_ = (lambda (p) (p (lambda (f) (lambda (r) r))))&lt;br /&gt;
&lt;br /&gt;
Booleans:&lt;br /&gt;
 if_ = (c t e) ;;only works in lazy eval&lt;br /&gt;
 if_eager = (c (lambda (d) t) (lambda (d) e)) ;;works in eager&lt;br /&gt;
 false = (lambda (t f) f)&lt;br /&gt;
 true = (lambda (t f) t)&lt;br /&gt;
&lt;br /&gt;
e.g. (if false (/1 0) 2)&lt;br /&gt;
     =&amp;gt; (false (/1 0) 2)&lt;br /&gt;
     =&amp;gt; ((lambda (t f) f) (/1 0) 2) ;; gets applied to function&lt;br /&gt;
     =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
Representing zero, succession and sum using lambda calculus:&lt;br /&gt;
 zero_ = (lambda (f) (lambda (x) x)) &lt;br /&gt;
 one_ = (lambda (f) (lambda (x) (f x))) &lt;br /&gt;
 two_ = (lambda (f) (lambda (x) (f(f x)))) ;; the f&#039;s keep on increasing with the number&lt;br /&gt;
 succ_ = (lambda (n) (lambda (f) (lambda (x) (f ((n f) x)))))   ;; apply the function f n times&lt;br /&gt;
 sum_ = (lambda (m)  (lambda (n)  ((n succ) m)))&lt;br /&gt;
 dec_ = (lambda (n) (first ((n (lambda (p) (cons (rest p) (succ (rest p))))) (cons zero zero)))))&lt;br /&gt;
 sub_ = (lambda (x) (lambda (y) ((y dec_) x)))&lt;br /&gt;
 mul_ = (lambda (x) (lambda (y) ((x (sum_ y)) zero)))&lt;br /&gt;
&lt;br /&gt;
==Eliminating Recursion==&lt;br /&gt;
&lt;br /&gt;
While reducing the language closer to the lambda calculus, we needed a fixed-point function&lt;br /&gt;
to avoid the problem of having unbound errors in recursion definitions (eventually would get&lt;br /&gt;
to some expression of the form (f f))&lt;br /&gt;
&lt;br /&gt;
These are the lecture notes that show how we created the fixed-point function and used it in&lt;br /&gt;
implementing factorial:&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 (define _fixmaker&lt;br /&gt;
   (lambda (fixmaker)&lt;br /&gt;
     (lambda (f)&lt;br /&gt;
       (f (lambda (x) (((fixmaker fixmaker) f) x))))))&lt;br /&gt;
 &lt;br /&gt;
 ;; Then, we&#039;ll give it itself:&lt;br /&gt;
 (define _fix&lt;br /&gt;
   ((lambda (f) (f f))&lt;br /&gt;
    _fixmaker))&lt;br /&gt;
 &lt;br /&gt;
 ;; Now, we can use fix to define recursive functions:&lt;br /&gt;
 (define _fac&lt;br /&gt;
   (_fix (lambda (fac)&lt;br /&gt;
           (lambda (n)&lt;br /&gt;
             (((_if (_zero? n))&lt;br /&gt;
               (lambda (IGNORE) _one))&lt;br /&gt;
              (lambda (IGNORE) ((_mul n) (fac (_pred n)))))))))&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
Here is the Y-combinator all of its lambadic glory:&lt;br /&gt;
&lt;br /&gt;
 (lambda (p)&lt;br /&gt;
   ((lambda (f)&lt;br /&gt;
      (f f))&lt;br /&gt;
    (lambda (f)&lt;br /&gt;
      (p (f f)))))&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Type Checking==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;th&amp;gt;Advantages&amp;lt;/th&amp;gt;&lt;br /&gt;
 &amp;lt;th&amp;gt;More Advantages&amp;lt;/th&amp;gt;&lt;br /&gt;
 &amp;lt;th&amp;gt;Disadvantages&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Safety&#039;&#039;&#039;:  A type checker can guarantee certain errors won&#039;t happen in the program before we run it.&amp;lt;br&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Readability&#039;&#039;&#039;:  Type declarations give information about the code to a programmer, and is never outdated in a program that compiles (unlike comments). This in itself is a form of &#039;&#039;&#039;Documentation.&#039;&#039;&#039;&amp;lt;br&amp;gt;&lt;br /&gt;
* Exploit types to make programs run faster, use less space, etc&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Efficiency&#039;&#039;&#039;:  Type declarations document static properties at compile time, so we don&#039;t have to check them each time code is visited during run time. &amp;lt;br&amp;gt;&lt;br /&gt;
*Reduce time spent debugging.&amp;lt;br&amp;gt;&lt;br /&gt;
*Catch errors in code that is not executed by the programmer (good if test suite is weak)&lt;br /&gt;
* Prohibits certain kinds of terrible coding styles&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;td width=&amp;quot;20%&amp;quot;&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Loses Expressiveness&#039;&#039;&#039;: some things will never produce errors but can&#039;t be type-checked will be rejected.&amp;lt;br&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;More Work at Start&#039;&#039;&#039;: upfront work is necessary before runtime testing.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Type&#039;&#039;&#039; - any property that can be established without executing the program. Types record kind of value not the precise value&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;A type system&#039;&#039;&#039; is that part of a programming language&lt;br /&gt;
(definition and implementation) that concerns itself with&lt;br /&gt;
making sure that no operations are performed on&lt;br /&gt;
inappropriate (types of) arguments.&lt;br /&gt;
&lt;br /&gt;
A language is &#039;&#039;&#039;“Type Safe”&#039;&#039;&#039; (or just “Safe”) if it has a type&lt;br /&gt;
system that ensures that no operations can be performed&lt;br /&gt;
on inappropriate arguments without this being detected. &lt;br /&gt;
The property of type safety can be achieved by static&lt;br /&gt;
checking (=detecting before runtime), dynamic checking&lt;br /&gt;
(=detecting at runtime) or a combination of both!&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Type Judgements&#039;&#039;&#039; - Collection of rules. One type rule for every syntactic construct. At least one type rule applies to every sub-term. A type error occurs when we are unable to construct a type judgment tree&lt;br /&gt;
* Relation between type judgements for functions and applications:&lt;br /&gt;
** Function declaration: assume arg has right type, guarantee that the body will have the promised type&lt;br /&gt;
** Function application: guarantee the argument has the right type for function, assume the result will have the type the function promises&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot; width=&amp;quot;100%&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Function&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Application&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Recursion&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td align=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;u&amp;gt;Г[i &amp;lt;- τ1] |- b : τ2&amp;lt;/u&amp;gt;&amp;lt;br&amp;gt;Г |- fun {i : τ1} : τ2 b} : (τ1 -&amp;gt;τ2&amp;lt;/td&amp;gt;     &lt;br /&gt;
&amp;lt;td align=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;u&amp;gt;Г |- e1: τ1 -&amp;gt; τ2,       Г |- e2: τ1&amp;lt;/u&amp;gt;&amp;lt;br&amp;gt;  Г |- {e1 e2}: τ2   &amp;lt;/td&amp;gt;                   &lt;br /&gt;
&amp;lt;td align=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;u&amp;gt;Г[i &amp;lt;- τi] |- b : τ,        Г[i &amp;lt;- τi] |- v : τi&amp;lt;/u&amp;gt;&amp;lt;br&amp;gt; Г |- {rec {i : τi v} b} : τ&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Read &amp;quot;Г |- exp: τ&amp;quot; as &amp;quot;Exp has type τ.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
*Type Procedures (^) (polymorphic function)&lt;br /&gt;
e.g. map : (for all) a, b. list(a) × (a -&amp;gt; b) -&amp;gt; list(b)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Soundness:&#039;&#039;&#039;&lt;br /&gt;
* Def: A type system is called “type-sound” or simply “sound” iff for every program that passes the type checker and for every possible execution of an expression in that program, it is true that the (runtime) value of that expression is an element of the (static) type of the expression.&lt;br /&gt;
* A program &#039;&#039;&#039;P&#039;&#039;&#039; typechecks without error --&amp;gt; No possible runs of &#039;&#039;&#039;P&#039;&#039;&#039; have &amp;quot;forbidden errors&amp;quot;&lt;br /&gt;
* Typechecker says &#039;&#039;&#039;P&#039;&#039;&#039; has an error &amp;lt;-- Some possible runs of &#039;&#039;&#039;P&#039;&#039;&#039; have &amp;quot;forbidden errors&amp;quot;&lt;br /&gt;
* Soundness = &amp;quot;The type system gives an F to all invalid programs&amp;quot;&lt;br /&gt;
 ;; this is type sound&lt;br /&gt;
 (define (type-of exp)&lt;br /&gt;
   (error &amp;quot;I don&#039;t know how to type-check!&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Metavariables&#039;&#039;&#039; - Not program variables. Stand in for program text&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Typing Control&#039;&#039;&#039; &lt;br /&gt;
* Cannot write an infinitely long type&lt;br /&gt;
* Strongly Normalizing: No matter what program you write in a strongly normalized language, it will always terminate&lt;br /&gt;
* Typing Recursion: extend the environment for body to initiate recursion, extend again for v to sustain it&lt;br /&gt;
&lt;br /&gt;
* Datatype Variant Tags --  With static type checker, only need to store variant since type is guaranteed by type checker. Better space conception&lt;br /&gt;
&lt;br /&gt;
==Type Inferencing==&lt;br /&gt;
* Constraints are generated by traversing the abstract syntax tree and determining what types expressions are required to have by the operations that act with them.&lt;br /&gt;
&lt;br /&gt;
: -&amp;gt; Label expressions &amp;amp; add all constraints for the type of that particular expression&lt;br /&gt;
* The unification algorithm is applied on the generated constraints:  If the constraints unify a type can be determined for the expression otherwise type inferencing fails on the expression.&lt;br /&gt;
&lt;br /&gt;
==Unification Algorithm==&lt;br /&gt;
&lt;br /&gt;
 1. If X and Y are identical identifiers, do nothing.&lt;br /&gt;
 2. If X is an identifier, replace all occurrences of X by Y both on the stack and in the substitution, and&lt;br /&gt;
    add X |-&amp;gt; Y to the substitution.&lt;br /&gt;
 3. If Y is an identifier, replace all occurrences of Y by X both on the stack and in the substitution, and&lt;br /&gt;
    add Y |-&amp;gt; X to the substitution.&lt;br /&gt;
 4. If X is of the form C(X1,...,Xn) for some constructor C, and Y is of the form C(Y1,...,Yn) (i.e., it&lt;br /&gt;
    has the same constructor), then push Xi=Yi for all 1 &amp;lt;= i &amp;lt;= n onto the stack.&lt;br /&gt;
 5. Otherwise, X and Y do not unify. Report an error.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Occurs Check&#039;&#039;&#039; - Throw an error if the replacee is in the replacer (ie - recursion)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Principal Type&#039;&#039;&#039; - Imposes only enough constraints needed for type soundness, and no more&lt;br /&gt;
&lt;br /&gt;
==Explicit Polymorphism==&lt;br /&gt;
 (define length&lt;br /&gt;
   &amp;lt; Λ (τ)&lt;br /&gt;
    (lambda (l : list (τ)) : number&lt;br /&gt;
      (cond&lt;br /&gt;
        [(Empty? &amp;lt;τ&amp;gt; l) 0]&lt;br /&gt;
        [(Cons? &amp;lt;τ&amp;gt; l) (add1 (length &amp;lt;τ&amp;gt; (Rest &amp;lt;τ&amp;gt;  l)))]))&amp;gt;)&lt;br /&gt;
* The expression (Rest &amp;lt;τ&amp;gt; l) first applies Rest to τ, resulting in an actual rest procedure that applies to lists of values of type τ&lt;br /&gt;
* This procedure consumes l as an argument and proceeds as it would in the type system free case&lt;br /&gt;
* Every type-parameterized procedure, such as Rest or length, is a generator of infinitely many procedures that each operate on specific types.&lt;br /&gt;
* Type Elaborator : The phase that performs type applications.&lt;br /&gt;
&lt;br /&gt;
==Implicit Polymorphism==&lt;br /&gt;
* Want to get new identifiers for the ones bound by the closures but not the ones in its lexical scope (lexical scope variables are shared between all applications of the closure). Only get fresh type variable for types introduced by let or letrec&lt;br /&gt;
&amp;lt;u&amp;gt;Г|- v : τ&#039;       Г[x &amp;lt;- CLOSE(τ&#039;, Г)]|- b : τ&amp;lt;/u&amp;gt;&amp;lt;br&amp;gt;                        &lt;br /&gt;
Г |- (let ([xv])b):τ&lt;br /&gt;
&lt;br /&gt;
* to get fresh type variables:&lt;br /&gt;
&amp;lt;u&amp;gt;Г|- e :  CLOSE(τ&#039;, Г&#039;)&amp;lt;/u&amp;gt;&amp;lt;br&amp;gt;                    &lt;br /&gt;
Г |- e : τ&lt;br /&gt;
* where τ is the same as τ&#039;, except the renaming applies only to type variables in τ&#039; that are not bound by Г&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Final_Exam_Appendix&amp;diff=127359</id>
		<title>Course:CPSC311/2011WT1/Final Exam Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Final_Exam_Appendix&amp;diff=127359"/>
		<updated>2011-12-07T22:35:20Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Do not remove:&#039;&#039;&#039; This reference sheet is the appendix for CPSC 311 2011W1 final exam.  Only the first 10 printed pages are guaranteed to be printed; so be compact!  Deadline for edits: 3PM 8 Dec 2011.&lt;br /&gt;
&lt;br /&gt;
= Final Appendix (Wiki, Student-Generated; credits at end in print version) =&lt;br /&gt;
&lt;br /&gt;
==Formalizing Constraint Generation==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Expression at Node !! Generated Constraints&lt;br /&gt;
|-&lt;br /&gt;
| n, where n is a numeral  ||  [[n]] = number&lt;br /&gt;
|-&lt;br /&gt;
| true  || [[true]]=boolean&lt;br /&gt;
|-&lt;br /&gt;
| false || [[false]]=boolean&lt;br /&gt;
|-&lt;br /&gt;
| (add1 e) || [[(add1 e)]]=number [[e]]=number&lt;br /&gt;
|-&lt;br /&gt;
| (+ e1 e2) || [[(+ e1 e2)]]=number [[e1]] = number [[e2]] = number&lt;br /&gt;
|-&lt;br /&gt;
| (zero? e) || [[(zero? e)]]=boolean [[e]] = number&lt;br /&gt;
|-&lt;br /&gt;
| (ncons e1 e2) ||  [[(ncons e1 e2)]]=list(num) [[e1]] = number [[e2]] = list(num)&lt;br /&gt;
|-&lt;br /&gt;
| (nﬁrst e) || [[(nﬁrst e)]]=number [[e]] = list(num)&lt;br /&gt;
|-&lt;br /&gt;
| (nrest e) || [[(nrest e)]]=list(num) [[e]] = list(num)&lt;br /&gt;
|-&lt;br /&gt;
| (nempty? e) || [[(nempty? e)]]=boolean [[e]] = list(num)&lt;br /&gt;
|-&lt;br /&gt;
| nempty || [[nempty]]=list(num)&lt;br /&gt;
|-&lt;br /&gt;
| (if c t e) || [[(if c t e)]]=[[t]] [[(if c t e)]]=[[e]] [[c]]=boolean&lt;br /&gt;
|-&lt;br /&gt;
| (lambda (x) b) || [[(lambda (x) b)]]= [[x]]→[[b]]&lt;br /&gt;
|-&lt;br /&gt;
| (f a) || [[f ]] = [[a]]→[[(f a)]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Unification Algorithm ==&lt;br /&gt;
&lt;br /&gt;
Begin with an empty substitution. Push all the constraints&lt;br /&gt;
onto a stack. If the stack is empty, return the substitution; otherwise, pop the constraint X = Y off the stack:&lt;br /&gt;
&lt;br /&gt;
# If X and Y are identical identiﬁers, do nothing.&lt;br /&gt;
# If X is an identiﬁer, replace all occurrences of X by Y both on the stack and in the substitution, and add X 7→ Y to the substitution.&lt;br /&gt;
# If Y is an identiﬁer, replace all occurrences of Y by X both on the stack and in the substitution, and add Y 7→ X to the substitution.&lt;br /&gt;
# If X is of the form C(X1,...,Xn) for some constructor C, 4 and Y is of the form C(Y1,...,Yn) (i.e., it has the same constructor), then push Xi = Yi for all 1 ≤ i ≤ n onto the stack.&lt;br /&gt;
# Otherwise, X and Y do not unify. Report an erro&lt;br /&gt;
&lt;br /&gt;
= Midterm 1 Material =&lt;br /&gt;
==Multiple Argument Function Conversion Example==&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 (with (f (fun (x y z) (+ x y)))&lt;br /&gt;
   (f 1 2))&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;==&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
  (with (f (fun (x) (fun (y) (fun (z) (+ x y)))))&lt;br /&gt;
    ((f 1) 2)&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Definitions == &lt;br /&gt;
&#039;&#039;&#039;Abstract syntax&#039;&#039;&#039; - idealized syntax, designed for convenient use by the underlying interpreter/compiler; result of parsing a concrete syntax expression &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Concrete syntax&#039;&#039;&#039; - syntax written by the user; consumed by the parser &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Closure&#039;&#039;&#039; - a wrapper that contains an expression (function/ binding) and its environment; expression saved until it needs to be used &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;CPS&#039;&#039;&#039; stands for Continuation-Passing Style (a.k.a. Tail Call Elimination)&amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Static Scope:&#039;&#039;&#039; In a language with static scope, the scope of an identifier&#039;s binding is a syntactically delimited region.  A typical region would be the body of a function or other binding construct.&amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Dynamic Scope:&#039;&#039;&#039; The scope of an identifier&#039;s binding is the entire remainder of the execution during which that binding is in effect.  &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;First-order Functions&#039;&#039;&#039; are not values in the language. They can only be defined in a designated portion of the program, where they must be given names for use in the remainder of the program.  &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Higher-order Functions&#039;&#039;&#039; can return other functions as values and take other functions as parameters. &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;First-class Functions&#039;&#039;&#039; are values with all the rights of other values. In particular, they can be supplied as the&lt;br /&gt;
value of arguments to functions, returned by functions as answers, constructed as values by expressions, and stored in data structures. &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Deferred Substitution&#039;&#039;&#039; Every time we encounter a substitution (in the form of a with or application), we augment the&lt;br /&gt;
repository with one more entry, recording the identiﬁer&#039;s name and the value (if eager) or expression (if&lt;br /&gt;
lazy) it should eventually be substituted with. We continue to evaluate without actually performing the&lt;br /&gt;
substitution. To prevent dynamic scoping, use closures to store the environment in which the functions are created&amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Cache calculated substitution&#039;&#039;&#039; values are saved the first time they are calculated.  By including a 3rd field, that is either false or contains the calculated value&lt;br /&gt;
&lt;br /&gt;
== Eager vs. Lazy Evaluation ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lazy With&#039;&#039;&#039;- we reduce the named expression to a value only when we need to &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Lazy Functions&#039;&#039;&#039;- we do not reduce the argument to a value until necessary&amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Strictness Points&#039;&#039;&#039;- the points where the implementation of a lazy language forces an expression to reduce to a value (example: in a function application, need to evaluate exactly what function to invoke, can&#039;t just leave it as a function closure) &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Eager&#039;&#039;&#039;- the named expression is reduced to a value before being substituting it. &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Recursion implementation by patching the environment ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Fixed-point of a function:&#039;&#039;&#039; a value that, when input to a function, is also output from the function.&lt;br /&gt;
&lt;br /&gt;
interp the bound expression with a fake environment binding, and then patch that binding to point to the result of said interp afterwards using set! mutation.  This creates a cyclic environment that addresses the needs of recursion. The cyclicity ensures that there is always &amp;quot;one more binding&amp;quot; for the recursive function&lt;br /&gt;
when we need it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(rec (bound-id named-exp body)&lt;br /&gt;
  (local ([define new-env (anEnv bound-id (numV 0) env)]&lt;br /&gt;
          [define named-value (interp named-exp new-env)])&lt;br /&gt;
    (begin&lt;br /&gt;
      (set-anEnv-val! new-env named-value)  ;; Patches false out for the correct value&lt;br /&gt;
      (interp body new-env))))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Racket syntax and General Examples==&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;BNF&#039;&#039;&#039;&lt;br /&gt;
  &amp;lt;CFWAE&amp;gt; ::= &amp;lt;num&amp;gt;&lt;br /&gt;
    | {+ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {- &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {* &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {/ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | &amp;lt;id&amp;gt;&lt;br /&gt;
    | {if0 &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {with {&amp;lt;id&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {fun {&amp;lt;id&amp;gt; ...} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {&amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; ...}&lt;br /&gt;
    | {rec {&amp;lt;symbol&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Conditional chains&#039;&#039;&#039;&lt;br /&gt;
  (cond&lt;br /&gt;
      [(number? v) ...]&lt;br /&gt;
      [(boolean? v) ...]&lt;br /&gt;
      [(string? v) ...]&lt;br /&gt;
      [else false])&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Map&#039;&#039;&#039; performs a procedure on all elements of the list in order&lt;br /&gt;
  (map (lamda (x) (* 2 x)) [1, 2, 3]) &lt;br /&gt;
  outputs [2, 4, 6]&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Pattern matching&#039;&#039;&#039;&amp;lt;br&amp;gt;&lt;br /&gt;
  &#039;...&#039; or &#039;___&#039; means 0 or more&amp;lt;br&amp;gt;&lt;br /&gt;
  &#039;..n&#039; or &#039;__n&amp;quot; means n or more&amp;lt;br&amp;gt;&lt;br /&gt;
  &#039;_&#039; matches anything&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
==&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
  (match data&lt;br /&gt;
   [pattern  action]&lt;br /&gt;
      [(? number?) (foo)]              ;; check if data is number &lt;br /&gt;
      [(list a b c) (foo a b c) ]      ;; back-reference of list elements&lt;br /&gt;
      [((and n (? number?))) (foo n)]  ;; check data and back-reference it as n&lt;br /&gt;
      [else (foo)])&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Begin&#039;&#039;&#039; evaluate expressions in order.  Results are ignored except for that of last expression, which is the evaluated value. &lt;br /&gt;
In this case, the side-effect of printing &amp;quot;Hello World&amp;quot; occurs, however the expression overall evaluates to 4.&lt;br /&gt;
  (begin&lt;br /&gt;
    (+ 5 2) (printf &amp;quot;Hello World!\n&amp;quot;) (+ 3 1))&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td width=&amp;quot;40%&amp;quot;&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Functions&#039;&#039;&#039;&amp;lt;br&amp;gt;&lt;br /&gt;
(lambda (x) x) ; first-class value, not used here&amp;lt;br&amp;gt;&lt;br /&gt;
(define f (lambda (x) x))&amp;lt;br&amp;gt;&lt;br /&gt;
(define (f x) x)&amp;lt;br&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Type-case&#039;&#039;&#039; is a conditional construct specific to a particular define-type&lt;br /&gt;
  (type-case WAE expr&lt;br /&gt;
    [num (n) expr]&lt;br /&gt;
    ...)&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Racket code specific to implementation of our languages ==&lt;br /&gt;
&#039;&#039;&#039;Environment:&#039;&#039;&#039; - repository of deferred substitutions&lt;br /&gt;
 (define-type Env&lt;br /&gt;
      [mtEnv]&lt;br /&gt;
      [anEnv (name symbol?)(value CFWAE-Value?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Expression Closures:&#039;&#039;&#039;&lt;br /&gt;
 (define-type CFWAE-Value&lt;br /&gt;
      [numV (num number?)]&lt;br /&gt;
      [exprV (body CFWAE?)(env Env?)] ;; used to hold an unevaluated expression and its environment (used in lazy interp)&lt;br /&gt;
      [closureV (param symbol?)(body CFWAE?)(env Env?)]) ;; used to close over the existing environment to ensure static scope&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Define-type&#039;&#039;&#039; (From assignment 1)&lt;br /&gt;
 (define-type WAE&lt;br /&gt;
  [num (n number?)]&lt;br /&gt;
  [binop (op procedure?) (lhs WAE?) (rhs WAE?)]&lt;br /&gt;
  [with (b Binding?) (body WAE?)]&lt;br /&gt;
  [with* (lob (listof Binding?)) (body WAE?)]&lt;br /&gt;
  [id (name symbol?)])&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Strict code&#039;&#039;&#039; (from lecture)&lt;br /&gt;
 (define (strict val)&lt;br /&gt;
  (if (expV? val)&lt;br /&gt;
      (strict (interp (expV-exp val) (expV-env val)))&lt;br /&gt;
      val))&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Midterm 2 Material =&lt;br /&gt;
&lt;br /&gt;
==State==&lt;br /&gt;
* Entails mutation&lt;br /&gt;
*State is &amp;quot;threaded through&amp;quot; a program in the sense that the state that results from evaluating a given function is the state in which the next sequential function is to be evaluated in.&lt;br /&gt;
*State is inherently dynamic and so isn&#039;t supported by solely the environment, as it has static scope in our implementations.&lt;br /&gt;
** The environment protects static scope while the store tracks the dynamic changes &lt;br /&gt;
** The environment maps identifiers to either locations (or values if there is no store) while the store maps locations to values.&lt;br /&gt;
*Closures do not remember state as function are to be applied using the state that exists where they are applied.&lt;br /&gt;
&lt;br /&gt;
== Mutability implementation data structures ==&lt;br /&gt;
To support mutability in a language, we need two data structures:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Environment:&#039;&#039;&#039; This is our regular substitution list required to support static scope.&lt;br /&gt;
* Maps either:&lt;br /&gt;
** Identifiers to values: Makes sense when we are differentiating between mutable and immutable identifiers.  If it is immutable, it will simply map to a regular value.  If it is mutable, it will map to a boxV value with a store location stored within.&lt;br /&gt;
** Identifiers to store locations: &#039;&#039;Only&#039;&#039; store locations are stored within the environment.  Thus, even for an immutable value, there is always a two-step lookup: the environment maps the identifier to a store location, and the store maps the store location to a value.&lt;br /&gt;
* Identifiers in the environment have &#039;&#039;lexical scope&#039;&#039;: a particular environment belongs to a particular expression scope.&lt;br /&gt;
&#039;&#039;&#039;Store:&#039;&#039;&#039; This is where we keep track of dynamic variable values.&lt;br /&gt;
* Maps location to a value (which can be a boxV pointing to another location itself).&lt;br /&gt;
* The store is &amp;quot;threaded through&amp;quot; the evaluation across sub-expressions.&lt;br /&gt;
* Mutation occurs when we change a value in the store.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Whereas the interpreter employs the same environment for&lt;br /&gt;
both arms of an addition, for instance, it cascades the store from one arm to the next and then back out&lt;br /&gt;
alongside the resulting value.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Meta vs Syntactic Interpreters==&lt;br /&gt;
&#039;&#039;&#039;Syntactic Interpreter&#039;&#039;&#039; - An interpreter that uses the interpreting language to represent only terms of the interpreted language, implementing all the corresponding behavior explicitly&lt;br /&gt;
*doesn&#039;t use Scheme&#039;s implementation of things (like numbers)&lt;br /&gt;
*doesn&#039;t matter how well the interpreting and interpreted languages correspond&lt;br /&gt;
&#039;&#039;&#039;Meta Interpreter&#039;&#039;&#039; - An interpreter that uses language features of the interpreting language to directly implement behaviour of the interpreted language&lt;br /&gt;
*easy to write when there is a strong match between interpreted and interpreting language&lt;br /&gt;
*uses Scheme&#039;s implementation of things (closures, procedure applications, numbers, etc)&lt;br /&gt;
&#039;&#039;&#039;Meta-Circular Interpreter&#039;&#039;&#039; - A meta interpreter in which the interpreting and interpreted language are the same&lt;br /&gt;
*Only uses Scheme implementation of things&lt;br /&gt;
&lt;br /&gt;
==Continuations==&lt;br /&gt;
&#039;&#039;&#039;What are they?&#039;&#039;&#039;&lt;br /&gt;
*stack is represented procedurally&lt;br /&gt;
*remember only the result and what is left to do (passes the state along or back to caller)&lt;br /&gt;
*roughly the same idea as tail recursion&lt;br /&gt;
*A cps function shifts used space from the stack onto the heap (hard to tell whether a particular program requires more heap space or stack space)&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Why?&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Why Not?&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td width=&amp;quot;38%&amp;quot;&amp;gt;&lt;br /&gt;
*Saves on memory and computation (ex. tail recursion summation vs augmenting recursion summation)&lt;br /&gt;
*A way of recording state when the processing computer is not always available (ex. web-servers)&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
*get rid of machine optimizations by forcing the structure of stack into continuation&lt;br /&gt;
*memory wastage if language needlessly creates stack frames&lt;br /&gt;
*need access to the source of entire program otherwise CPS translator may fail&lt;br /&gt;
*security: what if someone outside knew how to read the stack procedure?&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Continuation Passing Style (end in /k)==&lt;br /&gt;
&#039;&#039;&#039;Main Ideas&#039;&#039;&#039;&lt;br /&gt;
*every function takes an extra argument (its continuation)&lt;br /&gt;
*every argument in a function call must be either a variable or a lambda expression (not a more complex expression).&lt;br /&gt;
*every call is a tail call&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example from midterm 2&#039;&#039;&#039;&amp;lt;br&amp;gt;&lt;br /&gt;
We have the following, and want to convert it to CPS&lt;br /&gt;
(assuming (all-pos/k) and (range/k) are correctly implemented,&lt;br /&gt;
and represent CPS versions of all-pos and range, respectively:&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 (define (test-all-pos)&lt;br /&gt;
  (if (all-pos (range -5 5))&lt;br /&gt;
      &#039;fail&lt;br /&gt;
      &#039;pass))&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
==&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 (define (test-all-pos/k k)&lt;br /&gt;
  (range/k -5 5&lt;br /&gt;
   (lambda (range-result)&lt;br /&gt;
    (all-pos/k range-result&lt;br /&gt;
     (lambda (allpos-result)&lt;br /&gt;
      (k (if allpos-result&lt;br /&gt;
            &#039;fail&lt;br /&gt;
            &#039;pass))))))&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&amp;lt;td width=&amp;quot;15%&amp;quot;&amp;gt;&lt;br /&gt;
Note that this assumes &#039;&#039;&#039;if&#039;&#039;&#039; is in CPS.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 (define (map f l)&lt;br /&gt;
  (if (empty? l)&lt;br /&gt;
     empty&lt;br /&gt;
     (cons (f (first l)) &lt;br /&gt;
              (map f (rest l)))))&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
==&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 (define (map/k f/k list k)&lt;br /&gt;
  (if (empty? list)&lt;br /&gt;
      (k empty)&lt;br /&gt;
      (f/k (first list)&lt;br /&gt;
           (lambda (f-result)&lt;br /&gt;
             (map/k f/k (rest list)&lt;br /&gt;
                    (lambda (r-result)&lt;br /&gt;
                      (k (cons f-result r-result))))))))&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td width=&amp;quot;15%&amp;quot;&amp;gt;&lt;br /&gt;
Note that this assumes &#039;&#039;&#039;empty, first, rest, and cons&#039;&#039;&#039; are all in CPS.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Variables==&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;th&amp;gt;Call-by-value&amp;lt;/th&amp;gt;&lt;br /&gt;
 &amp;lt;th&amp;gt;Call-by-reference&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td width=30%&amp;gt;&lt;br /&gt;
*evaluated argument is held in a new location,&lt;br /&gt;
**Therefore, changes to the content of that location in the store don&#039;t affect the actual parameter&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
*pass a reference to the actual argument, not the value&lt;br /&gt;
*updates to the reference within the called procedure will become visible to the calling context&lt;br /&gt;
*cheaper to use (no additional allocation) but introduce problems &lt;br /&gt;
*to implement:&lt;br /&gt;
**create a closure and give it the location of the actual argument&lt;br /&gt;
**uses l-value: env lookup without store lookup&lt;br /&gt;
**any mutations to the formal parameter are now changes to the same location as the actual parameter&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;th&amp;gt;Stateful&amp;lt;/th&amp;gt;&lt;br /&gt;
 &amp;lt;th&amp;gt;Stateless&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
*server maintains state information&lt;br /&gt;
*easier to program&lt;br /&gt;
**don&#039;t need setup and breakdown of state at each interaction&lt;br /&gt;
*ex: FTP&lt;br /&gt;
**interp of each command is relative to history of past commands&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
*does not retain record of prior communication&lt;br /&gt;
*Web application must completely restore state of the computation for each interaction&lt;br /&gt;
*server can handle higher loads&lt;br /&gt;
*server can ignore clients who don&#039;t appear to be active&lt;br /&gt;
*must transmit enough data to resume computation&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Web Programs==&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td width=&amp;quot;37%&amp;quot;&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Receiver&#039;&#039;&#039; - a procedure of one argument representing the pending computation&lt;br /&gt;
*any computation not mentioned in the receiver never gets performed because of the program&#039;s termination after each iteration&lt;br /&gt;
&#039;&#039;&#039;Lifting&#039;&#039;&#039; - make nested procedures into top-level procedures&lt;br /&gt;
* e.g. lambdas become defines&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Make a program Web-Ready&#039;&#039;&#039;&lt;br /&gt;
* 1. Generate receivers that capture pending computations&lt;br /&gt;
* 2. Pass values to receivers instead of returning them&lt;br /&gt;
&#039;&#039;&#039;Implications&#039;&#039;&#039;&lt;br /&gt;
* 1. Order of evaluation&lt;br /&gt;
* 2. Transformation is global&lt;br /&gt;
**all procedures in program must consume an extra receiver&lt;br /&gt;
* 3. Sequentializes the program&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Escaper Lambdas Example==&lt;br /&gt;
From assignment 7 (midterm 2 review), we want to describe the continuation of the&lt;br /&gt;
bolded expression (namely (* (- x 32) 5) ) below:&lt;br /&gt;
&lt;br /&gt;
 (define (fahrenheit-celcius-converter type x)&lt;br /&gt;
   (case type&lt;br /&gt;
     [&#039;Fahrenheit (/ &#039;&#039;&#039;(* (- x 32) 5)&#039;&#039;&#039; 9)]&lt;br /&gt;
     [&#039;Celcius    (+ 32 (/ (* x 9) 5))]))&lt;br /&gt;
 &lt;br /&gt;
   (+ (fahrenheit-celcius-converter &#039;Fahrenheit 32) &lt;br /&gt;
      (fahrenheit-celcius-converter &#039;Fahrenheit 212))&lt;br /&gt;
&lt;br /&gt;
We can use escaper lambdas to do this. The continuation of the bolded expression in&lt;br /&gt;
the first evaluation of the function is:&lt;br /&gt;
 (lambda^ (value) (+ (/ value 9) (fahrenheit-celcius-converter &#039;Fahrenheit 212)))&lt;br /&gt;
&lt;br /&gt;
And the second continuation is:     (lambda^ (value) (+ 0 (/ value 9)))&lt;br /&gt;
&lt;br /&gt;
= Post-Midterm 2 Material =&lt;br /&gt;
&lt;br /&gt;
==Lambda Calculus==&lt;br /&gt;
Consists of:&lt;br /&gt;
*1-parameter functions &lt;br /&gt;
*1-argument function applications&lt;br /&gt;
*identifiers&lt;br /&gt;
&lt;br /&gt;
Shrinking of a language is achieved by showing that a given component of the language may be replaced by a combination of other components making the component unnecessary. (ex. numbers are unnecessary as they can be represented by a function that represents zero and a successor function) By creating a simplified language it is easier to construct proofs showing that the language has desired properties. (ex. correctly implements tail-call optimization, never accesses an array out of bounds, ...)&lt;br /&gt;
&lt;br /&gt;
Representing zero, succession and sum using lambda calculus:&lt;br /&gt;
 zero_ = (lambda (f) (lambda (x) x)) ;; apply the function f zero times&lt;br /&gt;
 succ_ = (lambda (n) (lambda (f) (lambda (x) (f ((n f) x)))))   ;; apply the function f n times&lt;br /&gt;
 sum_ = (lambda (m)  (lambda (n)  ((n succ) m)))&lt;br /&gt;
&lt;br /&gt;
==Eliminating Recursion==&lt;br /&gt;
&lt;br /&gt;
While reducing the language closer to the lambda calculus, we needed a fixed-point function&lt;br /&gt;
to avoid the problem of having unbound errors in recursion definitions (eventually would get&lt;br /&gt;
to some expression of the form (f f))&lt;br /&gt;
&lt;br /&gt;
These are the lecture notes that show how we created the fixed-point function and used it in&lt;br /&gt;
implementing factorial (_mul is multiplication, _pred is predecessor (i.e. &amp;quot;decrement&amp;quot;):&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
 (define _fixmaker&lt;br /&gt;
   (lambda (fixmaker)&lt;br /&gt;
     (lambda (f)&lt;br /&gt;
       (f (lambda (x) (((fixmaker fixmaker) f) x))))))&lt;br /&gt;
 &lt;br /&gt;
 ;; Then, we&#039;ll give it itself:&lt;br /&gt;
 (define _fix&lt;br /&gt;
   ((lambda (f) (f f))&lt;br /&gt;
    _fixmaker))&lt;br /&gt;
 &lt;br /&gt;
 ;; Now, we can use fix to define recursive functions:&lt;br /&gt;
 (define _fac&lt;br /&gt;
   (_fix (lambda (fac)&lt;br /&gt;
           (lambda (n)&lt;br /&gt;
             (((_if (_zero? n))&lt;br /&gt;
               (lambda (IGNORE) _one))&lt;br /&gt;
              (lambda (IGNORE) ((_mul n) (fac (_pred n)))))))))&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
Here is the Y-combinator all of its lambadic glory:&lt;br /&gt;
&lt;br /&gt;
 (lambda (p)&lt;br /&gt;
   ((lambda (f)&lt;br /&gt;
      (f f))&lt;br /&gt;
    (lambda (f)&lt;br /&gt;
      (p (f f)))))&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Type Checking==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;th&amp;gt;Advantages&amp;lt;/th&amp;gt;&lt;br /&gt;
 &amp;lt;th&amp;gt;More Advantages&amp;lt;/th&amp;gt;&lt;br /&gt;
 &amp;lt;th&amp;gt;Disadvantages&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Safety&#039;&#039;&#039;:  A type checker can guarantee certain errors won&#039;t happen in the program before we run it.&amp;lt;br&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Readability&#039;&#039;&#039;:  Type declarations give information about the code to a programmer, and is never outdated in a program that compiles (unlike comments). This in itself is a form of &#039;&#039;&#039;Documentation.&#039;&#039;&#039;&amp;lt;br&amp;gt;&lt;br /&gt;
* Exploit types to make programs run faster, use less space, etc&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Efficiency&#039;&#039;&#039;:  Type declarations document static properties at compile time, so we don&#039;t have to check them each time code is visited during run time. &amp;lt;br&amp;gt;&lt;br /&gt;
*Reduce time spent debugging.&amp;lt;br&amp;gt;&lt;br /&gt;
*Catch errors in code that is not executed by the programmer (good if test suite is weak)&lt;br /&gt;
* Prohibits certain kinds of terrible coding styles&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;td width=&amp;quot;20%&amp;quot;&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Loses Expressiveness&#039;&#039;&#039;: some things will never produce errors but can&#039;t be type-checked will be rejected.&amp;lt;br&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;More Work at Start&#039;&#039;&#039;: upfront work is necessary before runtime testing.&lt;br /&gt;
&amp;lt;/td&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Type&#039;&#039;&#039; - any property that can be established without executing the program. Types record kind of value not the precise value&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;A type system&#039;&#039;&#039; is that part of a programming language&lt;br /&gt;
(definition and implementation) that concerns itself with&lt;br /&gt;
making sure that no operations are performed on&lt;br /&gt;
inappropriate (types of) arguments.&lt;br /&gt;
&lt;br /&gt;
A language is &#039;&#039;&#039;“Type Safe”&#039;&#039;&#039; (or just “Safe”) if it has a type&lt;br /&gt;
system that ensures that no operations can be performed&lt;br /&gt;
on inappropriate arguments without this being detected. &lt;br /&gt;
The property of type safety can be achieved by static&lt;br /&gt;
checking (=detecting before runtime), dynamic checking&lt;br /&gt;
(=detecting at runtime) or a combination of both!&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Type Judgements&#039;&#039;&#039; - Collection of rules. One type rule for every syntactic construct. At least one type rule applies to every sub-term. A type error occurs when we are unable to construct a type judgment tree&lt;br /&gt;
* Relation between type judments for functions and applications:&lt;br /&gt;
** Function declaration: assume arg has right type, guarantee that the body will have the promised type&lt;br /&gt;
** Function application: guarantee the argument has the right type for function, assume the result will have the type the function promises&lt;br /&gt;
&amp;lt;table border=&amp;quot;1&amp;quot; width=&amp;quot;100%&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Function&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Application&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;th&amp;gt;Recursion&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td align=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;u&amp;gt;Г[i &amp;lt;- τ1] |- b : τ2&amp;lt;/u&amp;gt;&amp;lt;br&amp;gt;Г |- fun {i : τ1} : τ2 b} : (τ1 -&amp;gt;τ2&amp;lt;/td&amp;gt;     &lt;br /&gt;
&amp;lt;td align=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;u&amp;gt;Г |- e1: τ1 -&amp;gt; τ2,       Г |- e2: τ1&amp;lt;/u&amp;gt;&amp;lt;br&amp;gt;  Г |- {e1 e2}: τ2   &amp;lt;/td&amp;gt;                   &lt;br /&gt;
&amp;lt;td align=&amp;quot;center&amp;quot;&amp;gt;&amp;lt;u&amp;gt;Г[i &amp;lt;- τi] |- b : τ,        Г[i &amp;lt;- τi] |- v : τi&amp;lt;/u&amp;gt;&amp;lt;br&amp;gt; Г |- {rec {i : τi v} b} : τ&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Type Procedures (^) (polymorphic function)&lt;br /&gt;
e.g. map : (for all) a, b. list(a) × (a -&amp;gt; b) -&amp;gt; list(b)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Soundness:&#039;&#039;&#039;&lt;br /&gt;
* A program &#039;&#039;&#039;P&#039;&#039;&#039; typechecks without error --&amp;gt; No possible runs of &#039;&#039;&#039;P&#039;&#039;&#039; have &amp;quot;forbidden errors&amp;quot;&lt;br /&gt;
* Typechecker says &#039;&#039;&#039;P&#039;&#039;&#039; has an error &amp;lt;-- Some possible runs of &#039;&#039;&#039;P&#039;&#039;&#039; have &amp;quot;forbidden errors&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Metavariables&#039;&#039;&#039; - Not program variables. Stand in for program text&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Typing Control&#039;&#039;&#039; &lt;br /&gt;
* Cannot write an infinitely long type&lt;br /&gt;
* Strongly Normalizing: No matter what program you write in a strongly normalized language, it will always terminate&lt;br /&gt;
* Typing Recursion: extend the environment for body to initiate recursion, extend again for v to sustain it&lt;br /&gt;
&lt;br /&gt;
* Datatype Variant Tags --  With static type checker, only need to store variant since type is guaranteed by type checker. Better space conception&lt;br /&gt;
&lt;br /&gt;
==Type Inferencing==&lt;br /&gt;
* Constraints are generated by traversing the abstract syntax tree and determining what types expressions are required to have by the operations that act with them.&lt;br /&gt;
&lt;br /&gt;
: -&amp;gt; Label expressions &amp;amp; add all constraints for the type of that particular expression&lt;br /&gt;
* The unification algorithm is applied on the generated constraints:  If the constraints unify a type can be determined for the expression otherwise type inferencing fails on the expression.&lt;br /&gt;
&lt;br /&gt;
==Unification Algorithm==&lt;br /&gt;
&lt;br /&gt;
 1. If X and Y are identical identifiers, do nothing.&lt;br /&gt;
 2. If X is an identifier, replace all occurrences of X by Y both on the stack and in the substitution, and&lt;br /&gt;
    add X |-&amp;gt; Y to the substitution.&lt;br /&gt;
 3. If Y is an identifier, replace all occurrences of Y by X both on the stack and in the substitution, and&lt;br /&gt;
    add Y |-&amp;gt; X to the substitution.&lt;br /&gt;
 4. If X is of the form C(X1,...,Xn) for some constructor C, and Y is of the form C(Y1,...,Yn) (i.e., it&lt;br /&gt;
    has the same constructor), then push Xi=Yi for all 1 &amp;lt;= i &amp;lt;= n onto the stack.&lt;br /&gt;
 5. Otherwise, X and Y do not unify. Report an error.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Occurs Check&#039;&#039;&#039; - Throw an error if the replacee is in the replacer (ie - recursion)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Principal Type&#039;&#039;&#039; - Imposes only enough constraints needed for type soundness, and no more&lt;br /&gt;
&lt;br /&gt;
==Garbage Collection==&lt;br /&gt;
Why manual?&lt;br /&gt;
 1. Automatic GC is a slow and expensive process, therefore let people handle it and only do as much as is necessary.&lt;br /&gt;
 2. Soundness over completeness -&amp;gt; Automatic GC cannot detect all correct instances of GC (can fail to reclaim, or reclaim too early)&lt;br /&gt;
&lt;br /&gt;
Why not manual?&lt;br /&gt;
 1. People make mistakes when de-allocating memory. &lt;br /&gt;
    (i.e. Reclaiming something that is in use, or failing to reclaim unused memory)&lt;br /&gt;
 2. Loss of structural simplicity &lt;br /&gt;
    (e.g. multiple versions of the same code that differ only in memory management)&lt;br /&gt;
 3. Loops often lose tail-calling behaviour&lt;br /&gt;
&lt;br /&gt;
Algorithms for GC:&lt;br /&gt;
 1. Pointer Counting -&amp;gt; Collect anything with 0 things referencing it (Cyclic Pointers!)&lt;br /&gt;
 2. Mark and sweep&lt;br /&gt;
    0) System is low on memory&lt;br /&gt;
    1) Mark root set as always live, and recursively Mark down any reachable children of root set as live.&lt;br /&gt;
    2) anything that can&#039;t be reached is trash and is Sweeped.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;GC should demonstrate:&#039;&#039;&#039; &lt;br /&gt;
* &#039;&#039;&#039;Utility:&#039;&#039;&#039;  - must reclaim enough garbage to actually help computation&lt;br /&gt;
* &#039;&#039;&#039;Soundness:&#039;&#039;&#039;  - never reclaim something that is still needed&lt;br /&gt;
* &#039;&#039;&#039;Efficiency:&#039;&#039;&#039;  - Run quickly&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Space Leakage:&#039;&#039;&#039;  - Collector not reclaiming space that we know is no longer needed (eg. global variables that are no longer needed)&lt;br /&gt;
&lt;br /&gt;
==Explicit Polymorphism==&lt;br /&gt;
 (define length&lt;br /&gt;
   &amp;lt; Λ (τ)&lt;br /&gt;
    (lambda (l : list (τ)) : number&lt;br /&gt;
      (cond&lt;br /&gt;
        [(Empty? &amp;lt;τ&amp;gt; l) 0]&lt;br /&gt;
        [(Cons? &amp;lt;τ&amp;gt; l) (add1 (length &amp;lt;τ&amp;gt; (Rest &amp;lt;τ&amp;gt;  l)))]))&amp;gt;)&lt;br /&gt;
* The expression (Rest &amp;lt;τ&amp;gt; l) first applies Rest to τ, resulting in an actual rest procedure that applies to lists of values of type τ&lt;br /&gt;
* This procedure consumes l as an argument and proceeds as it would in the type system free case&lt;br /&gt;
* Every type-parameterized procedure, such as Rest or length, is a generator of infinitely many procedures that each operate on specific types.&lt;br /&gt;
* Type Elaborator : The phase that performs type applications.&lt;br /&gt;
&lt;br /&gt;
==Implicit Polymorphism==&lt;br /&gt;
* Want to get new identifiers for the ones bound by the closures but not the ones in its lexical scope (lexical scope variables are shared between all applications of the closure). Only get fresh type varaibale for types introduced by let or letrec&lt;br /&gt;
&amp;lt;u&amp;gt;Г|- v : τ&#039;       Г[x &amp;lt;- CLOSE(τ&#039;, Г)]|- b : τ&amp;lt;/u&amp;gt;&amp;lt;br&amp;gt;                        &lt;br /&gt;
Г |- (let ([xv])b):τ&lt;br /&gt;
&lt;br /&gt;
* to get fresh type variables:&lt;br /&gt;
&amp;lt;u&amp;gt;Г|- e :  CLOSE(τ&#039;, Г&#039;)&amp;lt;/u&amp;gt;&amp;lt;br&amp;gt;                    &lt;br /&gt;
Г |- e : τ&lt;br /&gt;
* where τ is the same as τ&#039;, except the renaming applies only to type variables in τ&#039; that are not bound by Г&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Midterm_Exam_2_Appendix&amp;diff=121741</id>
		<title>Course:CPSC311/2011WT1/Midterm Exam 2 Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Midterm_Exam_2_Appendix&amp;diff=121741"/>
		<updated>2011-11-09T16:55:19Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Midterm 2 Appendix (Wiki, Student-Generated; credits at end) =&lt;br /&gt;
&lt;br /&gt;
= Midterm 1 Material (copy of old appendix) =&lt;br /&gt;
&lt;br /&gt;
== Definitions == &lt;br /&gt;
&#039;&#039;&#039;Abstract syntax&#039;&#039;&#039; - idealized syntax, designed for convenient use by the underlying interpreter/compiler; result of parsing a concrete syntax expression &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Concrete syntax&#039;&#039;&#039; - syntax written by the user; consumed by the parser &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Closure&#039;&#039;&#039; - a wrapper that contains an expression (function/ binding) and its environment; expression saved until it needs to be used &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;CPS&#039;&#039;&#039; stands for Continuation-Passing Style (a.k.a. Tail Call Elimination)&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Static vs. Dynamic Scoping ==&lt;br /&gt;
&#039;&#039;&#039;Static Scope:&#039;&#039;&#039; In a language with static scope, the scope of an identifier&#039;s binding is a syntactically delimited region.  A typical region would be the body of a function or other binding construct.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Dynamic Scope:&#039;&#039;&#039; In a language with dynamic scope, the scope of an identifier&#039;s binding is the entire remainder of the execution during which that binding is in effect. That is, in a language with dynamic scope, if a function g binds identifier n and then invokes f, then f can refer to n—and so can every other function invoked by g until it completes its execution—even though f has no locally visible binding for n.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;from PLAI, p. 36&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== Closures and First-Class vs. First-Order Functions ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-order Functions&#039;&#039;&#039; are not values in the language. They can only be defined in a designated portion of&lt;br /&gt;
the program, where they must be given names for use in the remainder of the program.  (For a language like C, the &amp;quot;designated portion&amp;quot; is anywhere in the top-level of a source file, but not, for example, within the body of another function.)  The functions&lt;br /&gt;
in F1WAE are of this nature, which explains the 1 in the name of the language.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Higher-order Functions&#039;&#039;&#039; can return other functions as values and take other functions as parameters.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-class Functions&#039;&#039;&#039; are values with all the rights of other values. In particular, they can be supplied as the&lt;br /&gt;
value of arguments to functions, returned by functions as answers, constructed as values by expressions, and stored in data structures.&lt;br /&gt;
&lt;br /&gt;
== Deferred Substitution ==&lt;br /&gt;
&#039;&#039;&#039;Deferred Substitution&#039;&#039;&#039; Initially, we have no substitutions to perform, so the repository&lt;br /&gt;
is empty. Every time we encounter a substitution (in the form of a with or application), we augment the&lt;br /&gt;
repository with one more entry, recording the identiﬁer’s name and the value (if eager) or expression (if&lt;br /&gt;
lazy) it should eventually be substituted with. We continue to evaluate without actually performing the&lt;br /&gt;
substitution. &lt;br /&gt;
&lt;br /&gt;
A side-effect of deferred substitution is dynamic scoping, which we were able to fix by using closures to store the environment from which the function was created in.&lt;br /&gt;
&lt;br /&gt;
== Eager vs. Lazy Evaluation ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lazy With&#039;&#039;&#039;- we reduce the named expression to a value only when we need to &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Lazy Functions&#039;&#039;&#039;- we do not reduce the argument to a value until necessary&amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Strictness Points&#039;&#039;&#039;- the points where the implementation of a lazy language forces an expression to reduce to a value (example: in a function application, need to evaluate exactly what function to invoke, can&#039;t just leave it as a function closure) &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Eager&#039;&#039;&#039;- the named expression is reduced to a value before being substituting it. &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Backus-Naur Form (BNF) ==&lt;br /&gt;
&lt;br /&gt;
A concise specification language for expressing (among other things) the concrete syntax of a programming language.  A BNF often strongly suggests a corresponding abstract syntax.&lt;br /&gt;
&lt;br /&gt;
Here&#039;s an example BNF for a language with arithmetic expressions, with expressions, first-class functions and applications, a conditional statement, and explicit language support for recursion.  This particular language allows an arbitrary number of parameters to a function in its definition or arguments in a function application.&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;CFWAE&amp;gt; ::= &amp;lt;num&amp;gt;&lt;br /&gt;
    | {+ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {- &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {* &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {/ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | &amp;lt;id&amp;gt;&lt;br /&gt;
    | {if0 &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {with {&amp;lt;id&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {fun {&amp;lt;id&amp;gt; ...} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {&amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; ...}&lt;br /&gt;
    | {rec {&amp;lt;symbol&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
&lt;br /&gt;
== Racket syntax, helpful procedures, and thoughts ==&lt;br /&gt;
&lt;br /&gt;
 (cond&lt;br /&gt;
      [(number? v) ...]&lt;br /&gt;
      [(boolean? v) ...]&lt;br /&gt;
      [(string? v) ...]&lt;br /&gt;
      [else false])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(match data&lt;br /&gt;
   [pattern  action]&lt;br /&gt;
      [(? number?) (foo)]                    ;; check if data is number &lt;br /&gt;
      [(list a b c) (foo a b c) ]            ;; back-reference of list elements&lt;br /&gt;
      [((and n (? number?))) (foo n)]        ;; check if data is number and back-reference it as n&lt;br /&gt;
      [else (foo)]&lt;br /&gt;
)&lt;br /&gt;
;; Keep in mind that in pattern matching&lt;br /&gt;
;; &#039;...&#039; or &#039;___&#039; means 0 or more &lt;br /&gt;
;; &#039;..n&#039; or &#039;__n&amp;quot; means n or more &lt;br /&gt;
;; &#039;_&#039; matches anything&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;begin:&#039;&#039;&#039; Evaluates all expressions passed as arguments to begin, in order, and the results are all ignored except for the last one.&lt;br /&gt;
 &amp;gt; (begin (+ 5 2) (printf &amp;quot;Hello World!\n&amp;quot;) (+ 3 1))&lt;br /&gt;
 Hello World!&lt;br /&gt;
 4&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Variable definition:&#039;&#039;&#039;&lt;br /&gt;
 (define x 3)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Function definition:&#039;&#039;&#039;&lt;br /&gt;
 (define (funcName arg)&lt;br /&gt;
     (+ 1 arg))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Environment:&#039;&#039;&#039; - repository of deferred substitutions&lt;br /&gt;
 (define-type Env&lt;br /&gt;
      [mtEnv]&lt;br /&gt;
      [anEnv (name symbol?)(value CFWAE-Value?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Expression Closures:&#039;&#039;&#039;&lt;br /&gt;
 (define-type CFWAE-Value&lt;br /&gt;
      [numV (num number?)]&lt;br /&gt;
      [exprV (body CFWAE?)(env Env?)] ;; used to hold an unevaluated expression and its environment (used in lazy interp)&lt;br /&gt;
      [closureV (param symbol?)(body CFWAE?)(env Env?)]) ;;used to close over the existing environment to ensure static scope&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;map&#039;&#039;&#039; - performs a procedure on all elements of the list in order&lt;br /&gt;
 map &amp;lt;procedure&amp;gt; &amp;lt;list&amp;gt;&lt;br /&gt;
 (define (parseList list)&lt;br /&gt;
 (map parse list))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Type-case&#039;&#039;&#039;&lt;br /&gt;
 (type-case WAE expr&lt;br /&gt;
             [num (n) expr]&lt;br /&gt;
             ...)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define-type&#039;&#039;&#039; (From assignment 1)&lt;br /&gt;
 (define-type WAE&lt;br /&gt;
  [num (n number?)]&lt;br /&gt;
  [binop (op procedure?) (lhs WAE?) (rhs WAE?)]&lt;br /&gt;
  [with (b Binding?) (body WAE?)]&lt;br /&gt;
  [with* (lob (listof Binding?)) (body WAE?)]&lt;br /&gt;
  [id (name symbol?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Recursion interp&#039;&#039;&#039; (From lecture)                                            &lt;br /&gt;
 (define (interp exp env)                                                      &lt;br /&gt;
  (type-case CFAE exp                                                         &lt;br /&gt;
      ...&lt;br /&gt;
     (id  (name) (lookup name env))&lt;br /&gt;
     (fun (arg-name body) (closureV arg-name body env))&lt;br /&gt;
     (if0 (tst thn els) &lt;br /&gt;
          (type-case CFAE-value (interp tst env) &lt;br /&gt;
             (numV (n) (if (zero? n) (interp thn env) (interp els env)))&lt;br /&gt;
             (else (error &amp;quot;non numeric if0 test value&amp;quot;))))&lt;br /&gt;
     (app (fun-exp arg-exp)&lt;br /&gt;
          (let ((the-fun (interp fun-exp env))&lt;br /&gt;
                (the-arg (interp arg-exp env)))&lt;br /&gt;
            (type-case CFAE-value the-fun&lt;br /&gt;
              (closureV (arg-name body closure-env)&lt;br /&gt;
                        (interp body (anEnv arg-name the-arg&lt;br /&gt;
                                            closure-env)))&lt;br /&gt;
              (else (error &amp;quot;You can only apply closures!&amp;quot;)))))&lt;br /&gt;
     (rec (bound-id named-exp body)&lt;br /&gt;
       (local ([define new-env (anEnv bound-id &lt;br /&gt;
                                      (numV 0)&lt;br /&gt;
                                      env)]&lt;br /&gt;
               [define named-value (interp named-exp new-env)])&lt;br /&gt;
         (begin&lt;br /&gt;
           (set-anEnv-val! new-env named-value)  ;; Patches (numV 0) out for the correct value&lt;br /&gt;
           (interp body new-env))))&lt;br /&gt;
       &lt;br /&gt;
     &lt;br /&gt;
     ))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Strict code&#039;&#039;&#039; (from lecture)&lt;br /&gt;
 (define (strict val)&lt;br /&gt;
  (if (expV? val)&lt;br /&gt;
      (strict (interp (expV-exp val) (expV-env val)))&lt;br /&gt;
      val))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Laziness with Caching&#039;&#039;&#039;&lt;br /&gt;
 (define-type CFAE/L-Value&lt;br /&gt;
   ...&lt;br /&gt;
  [exprV (env Env?) (body CFAE/L?) &lt;br /&gt;
         (cached-version (or/c boolean? CFAE/L-Value?))]&lt;br /&gt;
  ... )&lt;br /&gt;
Cached strictness (from lecture-code):&lt;br /&gt;
  [exprV (env body cached-version) &lt;br /&gt;
           (if (equal? cached-version false)&lt;br /&gt;
               (begin&lt;br /&gt;
                 (printf &amp;quot;First eval of ~a.~n&amp;quot; body)&lt;br /&gt;
                 (local ([define cv (strict (interp-env env body))])&lt;br /&gt;
                   (begin&lt;br /&gt;
                     (set-exprV-cached-version! val cv)&lt;br /&gt;
                     cv)))&lt;br /&gt;
               (begin&lt;br /&gt;
                 (printf &amp;quot;Using cached version of ~a.~n&amp;quot; body)&lt;br /&gt;
                 cached-version))]&lt;br /&gt;
In `interp`:&lt;br /&gt;
 [app (fun-expr arg-expr)&lt;br /&gt;
         (local ([define fun-value (val-to-type closureV?&lt;br /&gt;
                                                (interp-env env fun-expr))]&lt;br /&gt;
                 [define fun-param (closureV-param-name fun-value)]&lt;br /&gt;
                 [define fun-body  (closureV-body fun-value)]&lt;br /&gt;
                 [define fun-env   (closureV-env fun-value)]&lt;br /&gt;
                 [define value     (exprV env arg-expr false)])&lt;br /&gt;
           (interp-env (anEnv fun-param value fun-env) fun-body))]&lt;br /&gt;
&lt;br /&gt;
= Midterm 2 Material =&lt;br /&gt;
&lt;br /&gt;
==State==&lt;br /&gt;
*State is &amp;quot;threaded through&amp;quot; a program in the sense that the state that results from evaluating a given function is the state in which the next sequential function is to be evaluated in.&lt;br /&gt;
*State is inherently dynamic and so isn&#039;t supported by solely the environment, as it has static scope in our implementations.&lt;br /&gt;
*Closures do not remember state as function are to be applied using the state that exists where they are applied.     &lt;br /&gt;
&lt;br /&gt;
==Mutable Data Structures==&lt;br /&gt;
Need two repositories:&lt;br /&gt;
#Environment &lt;br /&gt;
#*protects static scope &lt;br /&gt;
#*maps identifier to location&lt;br /&gt;
#*can get back to a previous location&lt;br /&gt;
#*Identifier -&amp;gt; Location i.e. &#039;&#039;[aSub (name symbol?)(location number?)(env Env?)])&#039;&#039;&lt;br /&gt;
#Store&lt;br /&gt;
#*tracks dynamic changes&lt;br /&gt;
#*maps location to a value&lt;br /&gt;
#*changes are permanent&lt;br /&gt;
#*Location -&amp;gt; Value&lt;br /&gt;
&lt;br /&gt;
*Variables&lt;br /&gt;
**Identifier -&amp;gt; Location&lt;br /&gt;
**Location -&amp;gt; Value&lt;br /&gt;
&lt;br /&gt;
==Scope and Extent==&lt;br /&gt;
&lt;br /&gt;
;scope (or lexical scope)&lt;br /&gt;
: the portion of program text where an identifier is bound. Identifiers in the environment have a limited scope.&lt;br /&gt;
&lt;br /&gt;
;extent (or dynamic extent)&lt;br /&gt;
: the portion of program execution during which a value persists in the store. Values in the store have a potentially unlimited extent.&lt;br /&gt;
&lt;br /&gt;
;threading&lt;br /&gt;
: describes the flow of the store from one arm of a computation to the next, then back out with the returning value. We say that the store is &#039;&#039;threaded&#039;&#039; through program execution.&lt;br /&gt;
&lt;br /&gt;
Note how this differs from the flow of the environment: &lt;br /&gt;
:# The updated store from the first computation is used for the second computation, while the same environment is used for both computations&lt;br /&gt;
:# As we return, we pass the updated store, but not the environment (we are exiting our scope).&lt;br /&gt;
&lt;br /&gt;
==Meta vs Syntactic Interpreters==&lt;br /&gt;
&#039;&#039;&#039;Syntactic Interpreter&#039;&#039;&#039; - An interpreter that uses the interpreting language to represent only terms of the interpreted language, implementing all the corresponding behavior explicitly&lt;br /&gt;
*doesn&#039;t use Scheme&#039;s implementation of things (like numbers)&lt;br /&gt;
*doesn&#039;t matter how well the interpreting and interpreted languages correspond&lt;br /&gt;
&#039;&#039;&#039;Meta Interpreter&#039;&#039;&#039; - An interpreter that uses language features of the interpreting language to directly implement behaviour of the interpreted language&lt;br /&gt;
*easy to write when there is a strong match between interpreted and interpreting language&lt;br /&gt;
*uses Scheme&#039;s implementation of things (closures, procedure applications, numbers, etc)&lt;br /&gt;
&#039;&#039;&#039;Meta-Circular Interpreter&#039;&#039;&#039; - A meta interpreter in which the interpreting and interpreted language are the same&lt;br /&gt;
*Only uses Scheme implementation of things&lt;br /&gt;
&#039;&#039;from PLAI, p.107-109&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Continuations==&lt;br /&gt;
&#039;&#039;&#039;What are they?&#039;&#039;&#039;&lt;br /&gt;
*stack is represented procedurally&lt;br /&gt;
*remember only the result and what is left to do (passes the state along)&lt;br /&gt;
*roughly the same idea as tail recursion&lt;br /&gt;
&#039;&#039;&#039;Why?&lt;br /&gt;
*Saves on memory and computation (ex. tail recursion summation vs augmenting recursion summation)&lt;br /&gt;
*A way of recording state when the processing computer is not always available (ex. web-servers)&lt;br /&gt;
&#039;&#039;&#039;Why Not?&lt;br /&gt;
*get rid of machine optimizations by forcing the structure of stack into continuation&lt;br /&gt;
*memory wastage if language needlessly creates stack frames&lt;br /&gt;
*need access to the source of entire program otherwise CPS translator may fail&lt;br /&gt;
*security: what if someone outside knew how to read the stack procedure?&lt;br /&gt;
&#039;&#039;&#039;Conventions&lt;br /&gt;
*Continuation methods usually end in /k&lt;br /&gt;
&lt;br /&gt;
== Implementing Continuations ==&lt;br /&gt;
&lt;br /&gt;
Making Continuations Explicit&lt;br /&gt;
&lt;br /&gt;
 (define-type CFAE-Value &lt;br /&gt;
   [numV (n number?)] &lt;br /&gt;
   [closureV (p procedure?)])&lt;br /&gt;
&lt;br /&gt;
 ;; interp : CFAE Env receiver → doesn’t return &lt;br /&gt;
 (define (interp expr env k)&lt;br /&gt;
   (type-case CFAE expr &lt;br /&gt;
     [num (n) (k (numV n))] &lt;br /&gt;
     [add (l r) (interp l env&lt;br /&gt;
                       (lambda (lv) &lt;br /&gt;
                         (interp r env&lt;br /&gt;
                                 (lambda (rv)&lt;br /&gt;
                                   (k (num+ lv rv))))))]&lt;br /&gt;
    [if0 (test truth falsity) &lt;br /&gt;
         (interp test env&lt;br /&gt;
                 (lambda (tv)&lt;br /&gt;
                   (if (num-zero? tv)&lt;br /&gt;
                       (interp truth env k)&lt;br /&gt;
                       (interp falsity env k))))] &lt;br /&gt;
    [id (v) (k (lookup v env))]&lt;br /&gt;
    [fun (param body)&lt;br /&gt;
         (k (closureV (lambda (arg-val dyn-k)&lt;br /&gt;
                        (interp body &lt;br /&gt;
                                aSub param arg-val env) &lt;br /&gt;
                         dyn-k))))] &lt;br /&gt;
  [app (fun-expr arg-expr)&lt;br /&gt;
      (interp fun-expr env &lt;br /&gt;
              (lambda (fun-val)&lt;br /&gt;
                (interp arg-expr env &lt;br /&gt;
                        (lambda (arg-val)&lt;br /&gt;
                          ((closureV-p fun-val) arg-val k)))))]))&lt;br /&gt;
&lt;br /&gt;
Adding continuations as language constructs&lt;br /&gt;
&lt;br /&gt;
 (define-type KCFAE-Value &lt;br /&gt;
   [numV (n number?)]&lt;br /&gt;
   [closureV (p procedure?)] &lt;br /&gt;
   [contV (c procedure?)])&lt;br /&gt;
&lt;br /&gt;
 ;; interp : KCFAE Env receiver → doesn’t return &lt;br /&gt;
  (define (interp expr env k)&lt;br /&gt;
  (type-case KCFAE expr &lt;br /&gt;
   [num (n) (k (numV n))] &lt;br /&gt;
   [add (l r) &lt;br /&gt;
       (interp l env&lt;br /&gt;
               (lambda (lv) (interp r env&lt;br /&gt;
                                    (lambda (rv)&lt;br /&gt;
                                      (k (num+ lv rv))))))]&lt;br /&gt;
  [if0 (test truth falsity) &lt;br /&gt;
       (interp test env&lt;br /&gt;
               (lambda (tv)&lt;br /&gt;
                 (if (num-zero? tv)&lt;br /&gt;
                     (interp truth env k)&lt;br /&gt;
                     (interp falsity env k))))]&lt;br /&gt;
  [id (v) (k (lookup v env))]&lt;br /&gt;
  [fun (param body)&lt;br /&gt;
       (k (closureV (lambda (arg-val dyn-k)&lt;br /&gt;
                      (interp body &lt;br /&gt;
                              (aSub param arg-val env) &lt;br /&gt;
                              dyn-k))))] &lt;br /&gt;
  [app (fun-expr arg-expr)&lt;br /&gt;
       (interp fun-expr env &lt;br /&gt;
               (lambda (fun-val)&lt;br /&gt;
                 (interp arg-expr env &lt;br /&gt;
                         (lambda (arg-val)&lt;br /&gt;
                           (type-case KCFAE-Value fun-val&lt;br /&gt;
                             [closureV (c) (c arg-val k)]&lt;br /&gt;
                             [contV (c) (c arg-val)]&lt;br /&gt;
                             [else (error ”not an applicable value”)])))))]&lt;br /&gt;
  &lt;br /&gt;
  [bindcc (cont-var body)&lt;br /&gt;
          (interp body&lt;br /&gt;
                  (aSub cont-var&lt;br /&gt;
                        (contV (lambda (val)&lt;br /&gt;
                                 (k val)))&lt;br /&gt;
                        env)&lt;br /&gt;
                  k)]))&lt;br /&gt;
&lt;br /&gt;
==Variables==&lt;br /&gt;
&#039;&#039;&#039;Call-by-value&#039;&#039;&#039;&lt;br /&gt;
*evaluated argument is held in a &#039;&#039;&#039;new location&#039;&#039;&#039;&lt;br /&gt;
*changes to the content of that location in the store don&#039;t affect the actual parameter&lt;br /&gt;
&#039;&#039;&#039;Call-by-reference&#039;&#039;&#039;&lt;br /&gt;
*pass a reference to the actual argument, not the value&lt;br /&gt;
*updates to the reference within the called procedure will become visible to the calling context&lt;br /&gt;
*cheaper to use (no additional allocation) but introduce problems &lt;br /&gt;
*to implement:&lt;br /&gt;
**create a closure and give it the location of the actual argument &#039;&#039;i.e. [define arg-loc (env-lookup (id-name arg-expr) env)]&#039;&#039; where arg-expr is syntactically a variable.&lt;br /&gt;
***unlike in call-by-value, we do not interpret the arg-expr before dispatching the closure. This is to prevent reducing the arg-expr from a store-location to a value, thus defeating the purpose!&lt;br /&gt;
**uses l-value: env lookup without store lookup; NB. &#039;&#039;an l-value is the location of the value in the store. Termed so because it exists on the left-hand-side of an expression when bound to value.&#039;&#039;&lt;br /&gt;
**any mutations to the formal parameter are now changes to the same location as the actual parameter&lt;br /&gt;
&lt;br /&gt;
==Stateful vs Stateless==&lt;br /&gt;
&#039;&#039;&#039;Stateful&#039;&#039;&#039;&lt;br /&gt;
*server maintains state information&lt;br /&gt;
*easier to program&lt;br /&gt;
**don&#039;t need setup and breakdown of state at each interaction&lt;br /&gt;
*ex: FTP&lt;br /&gt;
**interp of each command is relative to history of past commands&lt;br /&gt;
&#039;&#039;&#039;Stateless&#039;&#039;&#039;&lt;br /&gt;
*does not retain record of prior communication&lt;br /&gt;
*Web application must completely restore state of the computation for each interaction&lt;br /&gt;
*server can handle higher loads&lt;br /&gt;
*server can ignore clients who don&#039;t appear to be active&lt;br /&gt;
*must transmit enough data to resume computation&lt;br /&gt;
&lt;br /&gt;
==Web Programs==&lt;br /&gt;
&#039;&#039;&#039;Receiver&#039;&#039;&#039; - a procedure of one argument representing the pending computation&lt;br /&gt;
*any computation not mentioned in the receiver never gets performed because of the program&#039;s termination after each iteration&lt;br /&gt;
&#039;&#039;&#039;Lifting&#039;&#039;&#039; - make nested procedures into top-level procedures&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Make a program Web-Ready&#039;&#039;&#039;&lt;br /&gt;
* 1. Generate receivers that capture pending computations&lt;br /&gt;
* 2. Pass values to receivers instead of returning them&lt;br /&gt;
&#039;&#039;&#039;Implications&#039;&#039;&#039;&lt;br /&gt;
* 1. Order of evaluation&lt;br /&gt;
* 2. Transformation is global&lt;br /&gt;
**all procedures in program must consume an extra receiver&lt;br /&gt;
* 3. Sequentializes the program&lt;br /&gt;
&lt;br /&gt;
==Map Example==&lt;br /&gt;
 (define (map f l)&lt;br /&gt;
   (if (empty? l)&lt;br /&gt;
      empty&lt;br /&gt;
      (cons (f (first l)) &lt;br /&gt;
               (map f (rest l)))))&lt;br /&gt;
 &lt;br /&gt;
 becomes:&lt;br /&gt;
 (define (map/k f/k list k)&lt;br /&gt;
   (if (empty? list)&lt;br /&gt;
       (k empty)&lt;br /&gt;
       (f/k (first list)&lt;br /&gt;
            (lambda (f-result)&lt;br /&gt;
              (map/k f/k (rest list)&lt;br /&gt;
                     (lambda (r-result)&lt;br /&gt;
                       (k (cons f-result r-result))))))))&lt;br /&gt;
&lt;br /&gt;
==The Y Combinator==&lt;br /&gt;
The generic representation of the Y combinator:&lt;br /&gt;
 (lambda (p)&lt;br /&gt;
     ((lambda (f)&lt;br /&gt;
        (f f))&lt;br /&gt;
       (lambda (f)&lt;br /&gt;
               (p (f f)))))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As applied to recursive factorial:&lt;br /&gt;
&lt;br /&gt;
 ((lambda (marf)&lt;br /&gt;
   (marf marf))&lt;br /&gt;
         (lambda (f)&lt;br /&gt;
             (lambda (n)&lt;br /&gt;
                (if (zero? n)&lt;br /&gt;
                           1&lt;br /&gt;
                           (* n ((f f) (- n 1)))))))&lt;br /&gt;
in CFAE:&lt;br /&gt;
  {with {marf {fun {warf}&lt;br /&gt;
                                    {{fun {f} {f f}}&lt;br /&gt;
                                     {fun {rfg}&lt;br /&gt;
                                          {warf {fun {ignore} {rfg rfg}}}}}}}&lt;br /&gt;
                         {with {fact {marf {fun {fact}&lt;br /&gt;
                                                {fun {n}&lt;br /&gt;
                                                     {if0 n 1 {* n {{fact 0} {- n 1}}}}}}}}&lt;br /&gt;
                               {fact 5}}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Continuation Passing Style rules==&lt;br /&gt;
&lt;br /&gt;
(1) Convert each function f to f/k and take an extra continuation&lt;br /&gt;
argument.&lt;br /&gt;
&lt;br /&gt;
(2) Find the &amp;quot;next step to take&amp;quot; (the &amp;quot;sequentialized&amp;quot; version of&lt;br /&gt;
the program, just like we did when we threaded the store!).&lt;br /&gt;
&lt;br /&gt;
(3) If that step is in tail position, great!  If it&#039;s a value,&lt;br /&gt;
apply the continuation to it.  If it&#039;s a function call, pass the&lt;br /&gt;
continuation to the function call.&lt;br /&gt;
&lt;br /&gt;
(4) If that step is not in tail position, cut it from its current&lt;br /&gt;
location, replace it with a placeholder value (it!), write it in&lt;br /&gt;
tail position (at the front), and give it a lambda that takes that&lt;br /&gt;
placeholder as a parameter and includes the whole rest of the&lt;br /&gt;
function as its body.  Then, repeat from (2) on the whole rest of&lt;br /&gt;
the function!&lt;br /&gt;
&lt;br /&gt;
Disadvantages to CPS&lt;br /&gt;
&lt;br /&gt;
(1) It requires access to the source of the entire problem. If a procedure is defined&lt;br /&gt;
in a library for which we don’t have access to the source, or is perhaps written in &lt;br /&gt;
a different language (as map often is), then the CPS translator will either fail to run&lt;br /&gt;
or will produce potentially erroneous output (i.e., code that does not properly restore the state of the computation).&lt;br /&gt;
&lt;br /&gt;
(2) By replacing the machine’s stack with an explicit representation in&lt;br /&gt;
the form of receivers, it inhibits optimizations built into compilers and microprocessor architectures.&lt;br /&gt;
&lt;br /&gt;
(3) As we will see in Section 20.4, executing a program in CPS also assumes that the run-time system will not needlessly create stack frames (since the stack is entirely represented by the receiver). Since many languages (such as C and Java) do anyway, the program consumes memory unnecessarily. In an extreme case, a Java or C program that would have executed without exhausting memory will run out of memory after conversion into CPS.&lt;br /&gt;
&lt;br /&gt;
== Recursion Support Code ==&lt;br /&gt;
&lt;br /&gt;
Figure 11.3: Meta-Circular Interpreter&lt;br /&gt;
&lt;br /&gt;
 (define (number-or-procedure? v) &lt;br /&gt;
  (or (number? v)&lt;br /&gt;
      (procedure? v)))&lt;br /&gt;
&lt;br /&gt;
 (define-type Env &lt;br /&gt;
  [mtSub]&lt;br /&gt;
  [aSub (name symbol?)&lt;br /&gt;
        (value number-or-procedure?) &lt;br /&gt;
        (env Env?)])&lt;br /&gt;
&lt;br /&gt;
 ;;lookup : symbol Env → number-or-procedure &lt;br /&gt;
&lt;br /&gt;
 (define (lookup name env)&lt;br /&gt;
  (type-case Env env&lt;br /&gt;
    [mtSub () (error ’lookup ”no binding for identifier”)] &lt;br /&gt;
    [aSub (bound-name bound-value rest-env)&lt;br /&gt;
        (if (symbol=? bound-name name) &lt;br /&gt;
            bound-value&lt;br /&gt;
            (lookup name rest-env))]))&lt;br /&gt;
&lt;br /&gt;
 ;; interp : FAE Env → number-or-procedure &lt;br /&gt;
  (define (interp expr env)&lt;br /&gt;
   (type-case FAE expr&lt;br /&gt;
     [num (n) n]&lt;br /&gt;
     [add (l r) (+ (interp l env) (interp r env))] &lt;br /&gt;
     [id (v) (lookup v env)]&lt;br /&gt;
     [fun (bound-id bound-body)&lt;br /&gt;
       (lambda (arg-val) &lt;br /&gt;
         (interp bound-body&lt;br /&gt;
                 (aSub bound-id arg-val env)))] &lt;br /&gt;
     [app (fun-expr arg-expr)&lt;br /&gt;
       (local ([define fun-val (interp fun-expr env)]&lt;br /&gt;
               [define arg-val (interp arg-expr env)])&lt;br /&gt;
         (fun-val arg-val))]))&lt;br /&gt;
&lt;br /&gt;
Figure 11.4: Recursion: Support Code with Procedural Representation of Environments&lt;br /&gt;
 &lt;br /&gt;
 (define-type RCFAE-Value &lt;br /&gt;
   [numV (n number?)] &lt;br /&gt;
   [closureV (param symbol?)&lt;br /&gt;
             (body RCFAE?) &lt;br /&gt;
             (env Env?)])&lt;br /&gt;
&lt;br /&gt;
 (define (Env? x) &lt;br /&gt;
   (procedure? x))&lt;br /&gt;
&lt;br /&gt;
 (define (mtSub) &lt;br /&gt;
   (lambda (name)&lt;br /&gt;
     (error ’lookup ”no binding for identifier”)))&lt;br /&gt;
&lt;br /&gt;
 (define (aSub bound-name bound-value env) &lt;br /&gt;
   (lambda (want-name)&lt;br /&gt;
     (cond&lt;br /&gt;
       [(symbol=? want-name bound-name)&lt;br /&gt;
        bound-value] &lt;br /&gt;
       [else (lookup want-name env)])))&lt;br /&gt;
&lt;br /&gt;
 (define (cyclically-bind-and-interp bound-name named-expr env) &lt;br /&gt;
   (local ([define rec-ext-env&lt;br /&gt;
             (lambda (want-name) &lt;br /&gt;
               (cond&lt;br /&gt;
                 [(symbol=? want-name bound-name) &lt;br /&gt;
                  (closureV (fun-param named-expr)&lt;br /&gt;
                            (fun-body named-expr)&lt;br /&gt;
                            rec-ext-env)]&lt;br /&gt;
                 [else (lookup want-name env)]))])&lt;br /&gt;
     rec-ext-env))&lt;br /&gt;
&lt;br /&gt;
 (define (lookup name env)&lt;br /&gt;
   (env name))&lt;br /&gt;
&lt;br /&gt;
=== Examples: (From 2010W1 MT#2) ===&lt;br /&gt;
 &lt;br /&gt;
Converting the following function into CPS:&lt;br /&gt;
&lt;br /&gt;
  (define (range i n)&lt;br /&gt;
    (if (&amp;gt; i n)&lt;br /&gt;
        empty&lt;br /&gt;
        (cons i (range (+ i 1) n))))&lt;br /&gt;
&lt;br /&gt;
 ;; 1) Convert function range to range/k&lt;br /&gt;
 &lt;br /&gt;
  (define (range/k i n k)&lt;br /&gt;
    (if (&amp;gt; i n)&lt;br /&gt;
        empty&lt;br /&gt;
        (cons i (range/k (+i 1) n &lt;br /&gt;
                  (lambda () &#039;todo)))))&lt;br /&gt;
 &lt;br /&gt;
  ;; Find the next step(s) to take: empty and (range (+i 1) n)&lt;br /&gt;
  ;; empty is a value in tail position, so we just apply the continuation to it.&lt;br /&gt;
 &lt;br /&gt;
  (define (range/k i n k)&lt;br /&gt;
    (if (&amp;gt; i n)&lt;br /&gt;
        (k empty)&lt;br /&gt;
        (cons i (range/k (+i 1) n &lt;br /&gt;
                 (lambda () &#039;todo)))))&lt;br /&gt;
 &lt;br /&gt;
  ;; (range ...) step is not in tail position.&lt;br /&gt;
  ;; Cut it from its current position, and replace it with a placeholder.&lt;br /&gt;
  ;; Write it in tail position, and give it an appropriate lambda.&lt;br /&gt;
 &lt;br /&gt;
  (define (range/k i n k)&lt;br /&gt;
    (if (&amp;gt; i n)&lt;br /&gt;
       (k empty)&lt;br /&gt;
       (range/k (+i 1) n&lt;br /&gt;
         (lambda (r-result)              ; Takes placeholder as parameter&lt;br /&gt;
           (cons i (r-result)))))))       ; includes rest of program as body&lt;br /&gt;
 &lt;br /&gt;
  ;; Now, we need to continue step 2) from the body of the lambda&lt;br /&gt;
  ;; The next step to take is (cons...)&lt;br /&gt;
  ;;  We can treat it like a value and apply the continuation to it.&lt;br /&gt;
 &lt;br /&gt;
  (define (range/k i n k)&lt;br /&gt;
   (if (&amp;gt; i n)&lt;br /&gt;
       (k empty)&lt;br /&gt;
       (range/k (+i 1) n&lt;br /&gt;
         (lambda (r-result)&lt;br /&gt;
           (k (cons i (r-result)))))))&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Midterm_Exam_2_Appendix&amp;diff=120914</id>
		<title>Course:CPSC311/2011WT1/Midterm Exam 2 Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Midterm_Exam_2_Appendix&amp;diff=120914"/>
		<updated>2011-11-04T21:00:17Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Do not remove:&#039;&#039;&#039; This reference sheet is the appendix for CPSC 311 2011W1 midterm exam #2.  Only the first 8 printed pages are guaranteed to be printed; so be compact!  Deadline for edits: Tue 8 Nov @ 11:59PM (generally ~24 hours before the exam).&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Midterm_Exam_1_Appendix&amp;diff=115973</id>
		<title>Course:CPSC311/2011WT1/Midterm Exam 1 Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Midterm_Exam_1_Appendix&amp;diff=115973"/>
		<updated>2011-10-05T21:14:25Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: Prepping for printing.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Definitions = &lt;br /&gt;
&#039;&#039;&#039;Abstract syntax&#039;&#039;&#039; - idealized syntax, designed for convenient use by the underlying interpreter/compiler; result of parsing a concrete syntax expression; carries more semantic structure than the concrete syntax (which is usually a simpler structure like a string of tokens or a tree of s-expressions)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Concrete syntax&#039;&#039;&#039; - syntax written by the user; consumed by the parser&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Closure&#039;&#039;&#039; - a wrapper that contains an expression (function/ binding) and its environment; expression saved until it needs to be used&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Binding instance&#039;&#039;&#039; - a binding instance of an identifier is the instance of the identifier that gives it its value. In WAE, the &amp;lt;id&amp;gt; position of a with is the only binding instance&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Bound instance&#039;&#039;&#039; - an identifier is bound if it is contained within the scope of a binding instance of its name&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Free instance&#039;&#039;&#039; - an identifier not contained in the scope of any binding instance of its name is said to be free&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Substitution&#039;&#039;&#039; - to substitue identifier i in e with expression v, replace all free instances of i in e with v&lt;br /&gt;
&lt;br /&gt;
= Static vs. Dynamic Scoping =&lt;br /&gt;
&#039;&#039;&#039;Static Scope:&#039;&#039;&#039; In a language with static scope, the scope of an identifier&#039;s binding is a syntactically delimited region.  A typical region would be the body of a function or other binding construct.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Dynamic Scope:&#039;&#039;&#039; In a language with dynamic scope, the scope of an identifier&#039;s binding is the entire remainder of the execution during which that binding is in effect. That is, in a language with dynamic scope, if a function g binds identifier n and then invokes f, then f can refer to n—and so can every other function invoked by g until it completes its execution—even though f has no locally visible binding for n.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;from PLAI, p. 36&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Closures and First-Class vs. First-Order Functions =&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-order Functions&#039;&#039;&#039; are not values in the language. They can only be defined in a designated portion of&lt;br /&gt;
the program, where they must be given names for use in the remainder of the program.  (For a language like C, the &amp;quot;designated portion&amp;quot; is anywhere in the top-level of a source file, but not, for example, within the body of another function.)  The functions&lt;br /&gt;
in F1WAE are of this nature, which explains the 1 in the name of the language.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Higher-order Functions&#039;&#039;&#039; can return other functions as values and take other functions as parameters.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-class Functions&#039;&#039;&#039; are values with all the rights of other values. In particular, they can be supplied as the&lt;br /&gt;
value of arguments to functions, returned by functions as answers, constructed as values by expressions, and stored in data structures.&lt;br /&gt;
&lt;br /&gt;
= Deferred Substitution =&lt;br /&gt;
&#039;&#039;&#039;Deferred Substitution&#039;&#039;&#039; means that substitutions are accumulated in a repository called the &#039;&#039;&#039;environment&#039;&#039;&#039;. Initially, we have no substitutions to perform, so the environment&lt;br /&gt;
is empty. Every time we encounter a substitution (in the form of a with or application), we augment the&lt;br /&gt;
environment with one more entry, recording the identifier’s name and the value (if eager) or expression (if&lt;br /&gt;
lazy) it should eventually be substituted with. We continue to evaluate without actually performing the&lt;br /&gt;
substitution. &lt;br /&gt;
&lt;br /&gt;
A side-effect of deferred substitution is dynamic scoping, which we were able to fix by using closures to store the environment from which the function was created in.&lt;br /&gt;
&lt;br /&gt;
= Eager vs. Lazy Evaluation =&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lazy With&#039;&#039;&#039;- we reduce the named expression to a value only when we need to&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lazy Functions&#039;&#039;&#039;- we do not reduce the argument to a value until necessary&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Strictness Points&#039;&#039;&#039;- the points where the implementation of a lazy language forces an expression to reduce to a value (example: in a function application, the function being applied needs to be evaluated so that its argument may be substituted and its body evaluated)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Eager&#039;&#039;&#039;- all expressions that need to be evaluated are evaluated as soon as they are encountered; expressions are never wrapped up and reduced to a value in the future. In particular, substitutions are evaluated when the substitution is made and not when the substituted identifier is evaluated.&lt;br /&gt;
&lt;br /&gt;
= Backus-Naur Form (BNF) =&lt;br /&gt;
&lt;br /&gt;
A concise specification language for expressing (among other things) the concrete syntax of a programming language.  A BNF often strongly suggests a corresponding abstract syntax.&lt;br /&gt;
&lt;br /&gt;
Here&#039;s an example BNF for a language with arithmetic expressions, with expressions, first-class functions and applications, a conditional statement, and explicit language support for recursion.  This particular language allows an arbitrary number of parameters to a function in its definition or arguments in a function application.&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;CFWAE&amp;gt; ::= &amp;lt;num&amp;gt;&lt;br /&gt;
    | {+ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {- &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {* &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {/ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | &amp;lt;id&amp;gt;&lt;br /&gt;
    | {if0 &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {with {&amp;lt;id&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {fun {&amp;lt;id&amp;gt; ...} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {&amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; ...}&lt;br /&gt;
    | {rec {&amp;lt;symbol&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
&lt;br /&gt;
= Objectives of Functions =&lt;br /&gt;
# Encapsulate and re-use code (defer evaluation of the function body)&lt;br /&gt;
# Parameterize behaviour over dynamic context (take parameters)&lt;br /&gt;
# Be opaque to the dynamic context, &amp;quot;close over&amp;quot; the static context (analogous to building a wall to block dynamic context, but having a window that allows the parameters to be passed in to &amp;quot;close over&amp;quot; the static context)&lt;br /&gt;
&lt;br /&gt;
= Racket syntax &amp;amp; helpful procedures =&lt;br /&gt;
&lt;br /&gt;
 (cond&lt;br /&gt;
   [Condition1 Result1]&lt;br /&gt;
   [Condition2 Result2]&lt;br /&gt;
   ....&lt;br /&gt;
   [ConditionN ResultN])&lt;br /&gt;
&lt;br /&gt;
 (cond&lt;br /&gt;
     [(number? v) ...]&lt;br /&gt;
     [(boolean? v) ...]&lt;br /&gt;
     [(string? v) ...]&lt;br /&gt;
     [else false])&lt;br /&gt;
&lt;br /&gt;
 (match data&lt;br /&gt;
   [pattern  action]&lt;br /&gt;
      [(? number?) (foo)]                    ;; check if data is number &lt;br /&gt;
      [(list a b c) (foo a b c) ]            ;; back-reference of list elements&lt;br /&gt;
      [((and n (? number?))) (foo n)]        ;; check if data is number and back-reference it as n&lt;br /&gt;
      [else (foo)]&lt;br /&gt;
 )&lt;br /&gt;
 ;; Keep in mind that in pattern matching&lt;br /&gt;
 ;; &#039;...&#039; or &#039;___&#039; means 0 or more &lt;br /&gt;
 ;; &#039;..n&#039; or &#039;__n&amp;quot; means n or more &lt;br /&gt;
 ;; &#039;_&#039; matches anything&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Variable definition:&lt;br /&gt;
 (define x 3)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Function definition:&lt;br /&gt;
 (define (funcName arg)&lt;br /&gt;
    (+ 1 arg))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Environment: - repository of deferred substitutions&lt;br /&gt;
 (define-type Env&lt;br /&gt;
     [mtEnv]&lt;br /&gt;
     [anEnv (name symbol?)(value CFWAE-Value?)])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Expression Closures:&lt;br /&gt;
 (define-type CFWAE-Value&lt;br /&gt;
     [numV (num number?)]&lt;br /&gt;
     [exprV (body CFWAE?)(env Env?)] ;; used to hold an unevaluated expression and its environment (used in lazy interp)&lt;br /&gt;
     [closureV (param symbol?)(body CFWAE?)(env Env?)]) ;;used to close over the existing environment to ensure static scope&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
map - performs a procedure on all elements of the list in order&lt;br /&gt;
 map &amp;lt;procedure&amp;gt; &amp;lt;list&amp;gt;&lt;br /&gt;
 (define (parseList list)&lt;br /&gt;
 (map parse list))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Type-case&lt;br /&gt;
 (type-case WAE expr&lt;br /&gt;
            [num (n) expr]&lt;br /&gt;
            ...)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Define-type (From assignment 1)&lt;br /&gt;
&lt;br /&gt;
 (define-type WAE&lt;br /&gt;
  [num (n number?)]&lt;br /&gt;
  [binop (op procedure?) (lhs WAE?) (rhs WAE?)]&lt;br /&gt;
  [with (b Binding?) (body WAE?)]&lt;br /&gt;
  [with* (lob (listof Binding?)) (body WAE?)]&lt;br /&gt;
  [id (name symbol?)])&lt;br /&gt;
&lt;br /&gt;
Substitution&lt;br /&gt;
&lt;br /&gt;
 (define (sub-all-free-instances-of sub-id expr repl)&lt;br /&gt;
  (type-case WAE expr&lt;br /&gt;
    [num (n) expr]&lt;br /&gt;
    [add (l r) (add (sub-all-free-instances-of sub-id l repl) &lt;br /&gt;
                    (sub-all-free-instances-of sub-id r repl))]&lt;br /&gt;
    [sub (l r) (sub (sub-all-free-instances-of sub-id l repl) &lt;br /&gt;
                    (sub-all-free-instances-of sub-id r repl))]&lt;br /&gt;
    [with (this-id named-e body)&lt;br /&gt;
          (if (symbol=? sub-id this-id)&lt;br /&gt;
              (with this-id&lt;br /&gt;
                    (sub-all-free-instances-of sub-id named-e repl)&lt;br /&gt;
                    body)&lt;br /&gt;
              (with this-id&lt;br /&gt;
                    (sub-all-free-instances-of sub-id named-e repl)&lt;br /&gt;
                    (sub-all-free-instances-of sub-id body repl)))  &lt;br /&gt;
          ]&lt;br /&gt;
    [id (name)&lt;br /&gt;
        (if (symbol=? name sub-id)&lt;br /&gt;
            repl&lt;br /&gt;
            expr)]&lt;br /&gt;
    ))&lt;br /&gt;
&lt;br /&gt;
Recursion interp (From lecture)&lt;br /&gt;
&lt;br /&gt;
 (define (interp exp env)                                                      &lt;br /&gt;
  (type-case CFAE exp                                                         &lt;br /&gt;
     ...&lt;br /&gt;
    (id  (name) (lookup name env))&lt;br /&gt;
    (fun (arg-name body) (closureV arg-name body env))&lt;br /&gt;
    (if0 (tst thn els) &lt;br /&gt;
         (type-case CFAE-value (interp tst env) &lt;br /&gt;
            (numV (n) (if (zero? n) (interp thn env) (interp els env)))&lt;br /&gt;
            (else (error &amp;quot;non numeric if0 test value&amp;quot;))))&lt;br /&gt;
    (app (fun-exp arg-exp)&lt;br /&gt;
         (let ((the-fun (interp fun-exp env))&lt;br /&gt;
               (the-arg (interp arg-exp env)))&lt;br /&gt;
           (type-case CFAE-value the-fun&lt;br /&gt;
             (closureV (arg-name body closure-env)&lt;br /&gt;
                       (interp body (anEnv arg-name the-arg&lt;br /&gt;
                                           closure-env)))&lt;br /&gt;
             (else (error &amp;quot;You can only apply closures!&amp;quot;)))))&lt;br /&gt;
    (rec (bound-id named-exp body)&lt;br /&gt;
      (local ([define new-env (anEnv bound-id &lt;br /&gt;
                                     (numV 0)&lt;br /&gt;
                                     env)]&lt;br /&gt;
              [define named-value (interp named-exp new-env)])&lt;br /&gt;
        (begin&lt;br /&gt;
          (set-anEnv-val! new-env named-value)  ;; Patches (numV 0) out for the correct value&lt;br /&gt;
          (interp body new-env))))&lt;br /&gt;
      &lt;br /&gt;
    &lt;br /&gt;
    ))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Examples of using functions&lt;br /&gt;
(run &#039;{{fun {x y z} {- {- x y} z}} 2 3 4})&lt;br /&gt;
&lt;br /&gt;
(run &#039;{if0 (+ 5 -5) 0 1})&lt;br /&gt;
&lt;br /&gt;
(run &#039;{with {double {fun {x} {/ x x}}} {double 10}})&lt;br /&gt;
&lt;br /&gt;
(pre-process(parse &#039;{{fun {x y z} {- {- x y} z}} 2 3 4}))&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Midterm_Exam_1_Appendix&amp;diff=115246</id>
		<title>Course:CPSC311/2011WT1/Midterm Exam 1 Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Midterm_Exam_1_Appendix&amp;diff=115246"/>
		<updated>2011-09-28T20:57:55Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Do not remove:&#039;&#039;&#039; This reference sheet is the appendix for CPSC 311 2011W1 midterm exam #1.  Only the first 4 printed pages are guaranteed to be printed; so be compact!  Deadline for edits: Tue 4 Oct at 9PM.&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Assignment_3_Gotchas&amp;diff=111829</id>
		<title>Course:CPSC311/2011WT1/Assignment 3 Gotchas</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Assignment_3_Gotchas&amp;diff=111829"/>
		<updated>2011-09-01T03:47:11Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;(Not clear yet that our Assignment #3 will be Haskell this year, but just in case..)&lt;br /&gt;
&lt;br /&gt;
* Installing [http://haskell.org/hugs/ Hugs98] on Windows 7 (and Windows Vista?), you need to run as administrator (right-click and choose &amp;quot;Run as Administrator&amp;quot;).&lt;br /&gt;
** The error I got was a failure to extract the file hugs98.chm --[[User:SteveWolfman|SteveWolfman]] 20:47, 31 August 2011 (PDT)&lt;br /&gt;
* If you run out of memory, you can increase Hug&#039;s memory by going to Options&amp;gt; Runtime&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Assignment_3_Gotchas&amp;diff=109466</id>
		<title>Course:CPSC311/2011WT1/Assignment 3 Gotchas</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Assignment_3_Gotchas&amp;diff=109466"/>
		<updated>2011-08-16T17:41:05Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: Created page with &amp;quot;(Initialized with last year&amp;#039;s content.)  If you run out of memory, you can increase Hug&amp;#039;s memory by going to Options&amp;gt; Runtime&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;(Initialized with last year&#039;s content.)&lt;br /&gt;
&lt;br /&gt;
If you run out of memory, you can increase Hug&#039;s memory by going to Options&amp;gt; Runtime&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Assignment_1_Gotchas&amp;diff=109465</id>
		<title>Course:CPSC311/2011WT1/Assignment 1 Gotchas</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Assignment_1_Gotchas&amp;diff=109465"/>
		<updated>2011-08-16T17:40:09Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: Created page with &amp;quot;(Initializing with last year&amp;#039;s content.)  == Assignment #1 Gotchas for Everyone ==  * Don&amp;#039;t kill yourself giving lovely errors on malformed input on this assignment.  It&amp;#039;s not re...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;(Initializing with last year&#039;s content.)&lt;br /&gt;
&lt;br /&gt;
== Assignment #1 Gotchas for Everyone ==&lt;br /&gt;
&lt;br /&gt;
* Don&#039;t kill yourself giving lovely errors on malformed input on this assignment.  It&#039;s not required.&lt;br /&gt;
* The IDE (DrRacket) does not automatically save your code when ran. Save manually (and often, as the debugger likes to crash).&lt;br /&gt;
&lt;br /&gt;
== Assignment #1 Gotchas for New Racketeers ==&lt;br /&gt;
&lt;br /&gt;
* Focus on the guide (http://docs.racket-lang.org/guide/) to learn new elements of Racket and the general docs (http://docs.racket-lang.org/) for reference&lt;br /&gt;
* Don&#039;t forget to put &amp;lt;code&amp;gt;#lang plai&amp;lt;/code&amp;gt; at the top of your file (or have it added automatically)&lt;br /&gt;
&lt;br /&gt;
== PLAI/Racket Gotchas for Experienced Schemers ==&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;()&amp;lt;/code&amp;gt; is &#039;&#039;&#039;&#039;&#039;not&#039;&#039;&#039;&#039;&#039; the empty list in Racket.  Use &amp;lt;code&amp;gt;&#039;()&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;empty&amp;lt;/code&amp;gt; instead.&lt;br /&gt;
&lt;br /&gt;
== Other Stuff ==&lt;br /&gt;
&lt;br /&gt;
* You can access the fields of a type with a &amp;lt;code&amp;gt;type-case&amp;lt;/code&amp;gt; expression, but for small accesses (like the &amp;lt;code&amp;gt;Binding&amp;lt;/code&amp;gt; type), try either writing your own reusable access method that uses &amp;lt;code&amp;gt;type-case&amp;lt;/code&amp;gt; or the built-in accessors based on the &#039;&#039;type constructor&#039;&#039; names, not the type&#039;s names, like: &amp;lt;code&amp;gt;binding-name&amp;lt;/code&amp;gt; (lowercase like the type constructor, not uppercase like the type) and &amp;lt;code&amp;gt;binop-lhs&amp;lt;/code&amp;gt; (though you&#039;ll probably want &amp;lt;code&amp;gt;type-case&amp;lt;/code&amp;gt; for that one).&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Glossary&amp;diff=109463</id>
		<title>Course:CPSC311/2011WT1/Glossary</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Glossary&amp;diff=109463"/>
		<updated>2011-08-16T17:38:31Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: Created page with &amp;quot;==Definitions==  (Initialized from last year&amp;#039;s content.)  ===Data-type===  (in terms of the Racket language)  Introduces a new data-type (an &amp;quot;algebraic&amp;quot; or &amp;quot;variant data type&amp;quot;). ...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Definitions==&lt;br /&gt;
&lt;br /&gt;
(Initialized from last year&#039;s content.)&lt;br /&gt;
&lt;br /&gt;
===Data-type=== &lt;br /&gt;
(in terms of the Racket language)&lt;br /&gt;
&lt;br /&gt;
Introduces a new data-type (an &amp;quot;algebraic&amp;quot; or &amp;quot;variant data type&amp;quot;).  Search the Racket docs for more info: http://docs.racket-lang.org/search/index.html?q=data-type.&lt;br /&gt;
&lt;br /&gt;
An explanation of how define-type works, from another course: [http://wiki.cs.grinnell.edu/mediawiki/index.php/CSC_302:_define-type CSC_302:_define-type]&lt;br /&gt;
&lt;br /&gt;
===WAE===&lt;br /&gt;
&lt;br /&gt;
One of the PLAI languages we&#039;ll work with: a &amp;quot;with arithmetic expression&amp;quot;.  In other words, an arithmetic expression that can use &amp;quot;with&amp;quot; statements in order to bind identifiers (names) to values.  We also use this as the name of the type used to represent our abstract syntax tree.  As defined in the PLAI textbook: [http://www.cs.brown.edu/~sk/Publications/Books/ProgLangs/2007-04-26/plai-2007-04-26.pdf#page=32 pg 32] &lt;br /&gt;
&lt;br /&gt;
===F1WAE===&lt;br /&gt;
&lt;br /&gt;
A language supporting only first-order functions (functions that must be called by their defined name and so cannot be passed as parameters, returned as results, or constructed as values).&lt;br /&gt;
&lt;br /&gt;
===CFWAE===&lt;br /&gt;
&lt;br /&gt;
A language extending FWAE (which has first-class functions implemented using closures) to support conditionals.&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Handy_Links&amp;diff=109462</id>
		<title>Course:CPSC311/2011WT1/Handy Links</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Handy_Links&amp;diff=109462"/>
		<updated>2011-08-16T17:37:33Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: Created page with &amp;quot;(Initialized with last year&amp;#039;s content.)  = Racket = * [http://planet.plt-scheme.org/display.ss?package=json.plt&amp;amp;owner=dherman Racket JSON] * [http://planet.plt-scheme.org/package...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;(Initialized with last year&#039;s content.)&lt;br /&gt;
&lt;br /&gt;
= Racket =&lt;br /&gt;
* [http://planet.plt-scheme.org/display.ss?package=json.plt&amp;amp;owner=dherman Racket JSON]&lt;br /&gt;
* [http://planet.plt-scheme.org/package-source/dherman/json.plt/3/0/planet-docs/json/index.html Racket JSON documentation]&lt;br /&gt;
* [http://docs.racket-lang.org/web-server/templates.html Racket web templates:]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Haskell =&lt;br /&gt;
* [http://learnyouahaskell.com/making-our-own-types-and-typeclasses Good Haskell tutorial]&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Midterm_Exam_2_Appendix&amp;diff=109461</id>
		<title>Course:CPSC311/2011WT1/Midterm Exam 2 Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Midterm_Exam_2_Appendix&amp;diff=109461"/>
		<updated>2011-08-16T17:36:32Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Do not remove:&#039;&#039;&#039; This reference sheet is the appendix for CPSC 311 2011W1 midterm exam #2.  Only the first 6 printed pages are guaranteed to be printed; so be compact!  Deadline for edits: TO BE ANNOUNCED (generally ~24 hours before the exam).&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Final_Exam_Appendix&amp;diff=109460</id>
		<title>Course:CPSC311/2011WT1/Final Exam Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Final_Exam_Appendix&amp;diff=109460"/>
		<updated>2011-08-16T17:36:21Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: Created page with &amp;quot;&amp;#039;&amp;#039;&amp;#039;Do not remove:&amp;#039;&amp;#039;&amp;#039; This reference sheet is the appendix for CPSC 311 2011W1 final exam.  Only the first 10 printed pages are guaranteed to be printed; so be compact!  Deadline ...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Do not remove:&#039;&#039;&#039; This reference sheet is the appendix for CPSC 311 2011W1 final exam.  Only the first 10 printed pages are guaranteed to be printed; so be compact!  Deadline for edits: TO BE ANNOUNCED (generally ~24 hours before the exam).&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Midterm_Exam_2_Appendix&amp;diff=109459</id>
		<title>Course:CPSC311/2011WT1/Midterm Exam 2 Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Midterm_Exam_2_Appendix&amp;diff=109459"/>
		<updated>2011-08-16T17:35:59Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: Created page with &amp;quot;&amp;#039;&amp;#039;&amp;#039;Do not remove:&amp;#039;&amp;#039;&amp;#039; This reference sheet is the appendix for CPSC 311 2011W1 midterm exam #1.  Only the first 6 printed pages are guaranteed to be printed; so be compact!  Deadl...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Do not remove:&#039;&#039;&#039; This reference sheet is the appendix for CPSC 311 2011W1 midterm exam #1.  Only the first 6 printed pages are guaranteed to be printed; so be compact!  Deadline for edits: TO BE ANNOUNCED (generally ~24 hours before the exam).&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Midterm_Exam_1_Appendix&amp;diff=109458</id>
		<title>Course:CPSC311/2011WT1/Midterm Exam 1 Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1/Midterm_Exam_1_Appendix&amp;diff=109458"/>
		<updated>2011-08-16T17:35:29Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: Created page with &amp;quot;&amp;#039;&amp;#039;&amp;#039;Do not remove:&amp;#039;&amp;#039;&amp;#039; This reference sheet is the appendix for CPSC 311 2011W1 midterm exam #1.  Only the first 4 printed pages are guaranteed to be printed; so be compact!  Deadl...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Do not remove:&#039;&#039;&#039; This reference sheet is the appendix for CPSC 311 2011W1 midterm exam #1.  Only the first 4 printed pages are guaranteed to be printed; so be compact!  Deadline for edits: TO BE ANNOUNCED (generally ~24 hours before the exam).&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1&amp;diff=109457</id>
		<title>Course:CPSC311/2011WT1</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2011WT1&amp;diff=109457"/>
		<updated>2011-08-16T17:33:36Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: Initialized page from 2010W1&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== CPSC 311 2011W1: Definition of Programming Languages ==&lt;br /&gt;
&lt;br /&gt;
Programming languages is a challenging course, littered with deep ideas linked in fascinating ways and occasionally shrouded in strange terminology.  Therefore, you have this wiki to help each other (and the teaching staff!) keep up with course issues. &lt;br /&gt;
The course wiki is available for anyone to contribute to the class&#039;s learning.  It&#039;s also a great way to earn some bonus points.&lt;br /&gt;
&lt;br /&gt;
Feel free to add useful possible content to this list.  Currently based off of last year.. but surely we can do better than last year! :P&lt;br /&gt;
&lt;br /&gt;
* Exam Appendices&lt;br /&gt;
** [[Course:CPSC311/2011WT1/Midterm Exam 1 Appendix | Midterm #1 Appendix]]: fill it in, we&#039;ll print it for the exam!  Maybe start from [[Course:CPSC311/2010WT1/Midterm Exam 1 Appendix]]?&lt;br /&gt;
** [[Course:CPSC311/2011WT1/Midterm Exam 2 Appendix | Midterm #2 Appendix]]: fill it in, we&#039;ll print it for the exam!  Maybe start from [[Course:CPSC311/2010WT1/Midterm Exam 2 Appendix]]?&lt;br /&gt;
** [[Course:CPSC311/2011WT1/Final Exam Appendix | Final Appendix]]: fill it in, we&#039;ll print it for the exam!  Maybe start from [[Course:CPSC311/2010WT1/Final Exam Appendix]]?&lt;br /&gt;
* A repository of people&#039;s [[Course:CPSC311/2011WT1/Lecture Notes | Lecture Notes]].  Post yours to pay it forward, in case you miss class and want to catch up!&lt;br /&gt;
* A collection of [[Course:CPSC311/2011WT1/Handy Links | Handy Links]] to help you find course-related info.&lt;br /&gt;
* Our very own [[Course:CPSC311/2011WT1/Glossary | Glossary]] of the many terms we&#039;ll run into.&lt;br /&gt;
* [[Course:CPSC311/2011WT1/Assignment 1 Gotchas | Assignment #1 Gotchas]], pesky things that might trip you up on rudimentary interpreters&lt;br /&gt;
* [[Course:CPSC311/2011WT1/Assignment 2 Gotchas | Assignment #2 Gotchas]], pesky things that might trip you up on extended interpreters&lt;br /&gt;
* [[Course:CPSC311/2011WT1/Assignment 3 Gotchas | Assignment #3 Gotchas]], pesky things that might trip you up as you get lazy&lt;br /&gt;
&lt;br /&gt;
== Handy List of All Our Pages ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;dpl&amp;gt;&lt;br /&gt;
titlematch={{PAGENAME}}/%&lt;br /&gt;
namespace={{NAMESPACE}}&lt;br /&gt;
replaceintitle=$CPSC311/2011WT1/$,&lt;br /&gt;
shownamespace=false&lt;br /&gt;
&amp;lt;/dpl&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:CPSC]]&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Final_Exam_Appendix&amp;diff=66120</id>
		<title>Course:CPSC311/2010WT1/Final Exam Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Final_Exam_Appendix&amp;diff=66120"/>
		<updated>2010-12-08T06:45:35Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Do not remove: This reference sheet is the appendix for the CPSC 311 2010W1 Final Exam. Only the first 10 printed pages are guaranteed to be printed; so be compact! Deadline for edits: 0900 Monday 20 December.&lt;br /&gt;
&lt;br /&gt;
= Final Appendix (Wiki, Student-Generated; credits at end in print version) =&lt;br /&gt;
&lt;br /&gt;
= Midterm 1 Material (copied from MT2 appendix) =&lt;br /&gt;
&lt;br /&gt;
== Definitions == &lt;br /&gt;
&#039;&#039;&#039;Abstract syntax&#039;&#039;&#039; - idealized syntax, designed for convenient use by the underlying interpreter/compiler; result of parsing a concrete syntax expression &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Concrete syntax&#039;&#039;&#039; - syntax written by the user; consumed by the parser &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Closure&#039;&#039;&#039; - a wrapper that contains an expression (function/ binding) and its environment; expression saved until it needs to be used &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;CPS&#039;&#039;&#039; stands for Continuation-Passing Style (a.k.a. Tail Call Elimination)&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Static vs. Dynamic Scoping ==&lt;br /&gt;
&#039;&#039;&#039;Static Scope:&#039;&#039;&#039; In a language with static scope, the scope of an identifier&#039;s binding is a syntactically delimited region.  A typical region would be the body of a function or other binding construct.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Dynamic Scope:&#039;&#039;&#039; In a language with dynamic scope, the scope of an identifier&#039;s binding is the entire remainder of the execution during which that binding is in effect. That is, in a language with dynamic scope, if a function g binds identifier n and then invokes f, then f can refer to n—and so can every other function invoked by g until it completes its execution—even though f has no locally visible binding for n.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;from PLAI, p. 36&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== Closures and First-Class vs. First-Order Functions ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-order Functions&#039;&#039;&#039; are not values in the language. They can only be defined in a designated portion of&lt;br /&gt;
the program, where they must be given names for use in the remainder of the program.  (For a language like C, the &amp;quot;designated portion&amp;quot; is anywhere in the top-level of a source file, but not, for example, within the body of another function.)  The functions&lt;br /&gt;
in F1WAE are of this nature, which explains the 1 in the name of the language.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Higher-order Functions&#039;&#039;&#039; can return other functions as values and take other functions as parameters.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-class Functions&#039;&#039;&#039; are values with all the rights of other values. In particular, they can be supplied as the&lt;br /&gt;
value of arguments to functions, returned by functions as answers, constructed as values by expressions, and stored in data structures.&lt;br /&gt;
&lt;br /&gt;
== Deferred Substitution ==&lt;br /&gt;
&#039;&#039;&#039;Deferred Substitution&#039;&#039;&#039; Initially, we have no substitutions to perform, so the repository&lt;br /&gt;
is empty. Every time we encounter a substitution (in the form of a with or application), we augment the&lt;br /&gt;
repository with one more entry, recording the identiﬁer’s name and the value (if eager) or expression (if&lt;br /&gt;
lazy) it should eventually be substituted with. We continue to evaluate without actually performing the&lt;br /&gt;
substitution. &lt;br /&gt;
&lt;br /&gt;
A side-effect of deferred substitution is dynamic scoping, which we were able to fix by using closures to store the environment from which the function was created in.&lt;br /&gt;
&lt;br /&gt;
== Eager vs. Lazy Evaluation ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lazy With&#039;&#039;&#039;- we reduce the named expression to a value only when we need to &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Lazy Functions&#039;&#039;&#039;- we do not reduce the argument to a value until necessary&amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Strictness Points&#039;&#039;&#039;- the points where the implementation of a lazy language forces an expression to reduce to a value (example: in a function application, need to evaluate exactly what function to invoke, can&#039;t just leave it as a function closure) &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Eager&#039;&#039;&#039;- the named expression is reduced to a value before being substituting it. &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Backus-Naur Form (BNF) ==&lt;br /&gt;
&lt;br /&gt;
A concise specification language for expressing (among other things) the concrete syntax of a programming language.  A BNF often strongly suggests a corresponding abstract syntax.&lt;br /&gt;
&lt;br /&gt;
Here&#039;s an example BNF for a language with arithmetic expressions, with expressions, first-class functions and applications, a conditional statement, and explicit language support for recursion.  This particular language allows an arbitrary number of parameters to a function in its definition or arguments in a function application.&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;CFWAE&amp;gt; ::= &amp;lt;num&amp;gt;&lt;br /&gt;
    | {+ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {- &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {* &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {/ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | &amp;lt;id&amp;gt;&lt;br /&gt;
    | {if0 &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {with {&amp;lt;id&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {fun {&amp;lt;id&amp;gt; ...} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {&amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; ...}&lt;br /&gt;
    | {rec {&amp;lt;symbol&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
&lt;br /&gt;
== Racket syntax, helpful procedures, and thoughts ==&lt;br /&gt;
&lt;br /&gt;
 (cond&lt;br /&gt;
      [(number? v) ...]&lt;br /&gt;
      [(boolean? v) ...]&lt;br /&gt;
      [(string? v) ...]&lt;br /&gt;
      [else false])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(match data&lt;br /&gt;
   [pattern  action]&lt;br /&gt;
      [(? number?) (foo)]                    ;; check if data is number &lt;br /&gt;
      [(list a b c) (foo a b c) ]            ;; back-reference of list elements&lt;br /&gt;
      [((and n (? number?))) (foo n)]        ;; check if data is number and back-reference it as n&lt;br /&gt;
      [else (foo)]&lt;br /&gt;
)&lt;br /&gt;
;; Keep in mind that in pattern matching&lt;br /&gt;
;; &#039;...&#039; or &#039;___&#039; means 0 or more &lt;br /&gt;
;; &#039;..n&#039; or &#039;__n&amp;quot; means n or more &lt;br /&gt;
;; &#039;_&#039; matches anything&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;begin:&#039;&#039;&#039; Evaluates all expressions passed as arguments to begin, in order, and the results are all ignored except for the last one.&lt;br /&gt;
 &amp;gt; (begin (+ 5 2) (printf &amp;quot;Hello World!\n&amp;quot;) (+ 3 1))&lt;br /&gt;
 Hello World!&lt;br /&gt;
 4&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Variable definition:&#039;&#039;&#039;&lt;br /&gt;
 (define x 3)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Function definition:&#039;&#039;&#039;&lt;br /&gt;
 (define (funcName arg)&lt;br /&gt;
     (+ 1 arg))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Environment:&#039;&#039;&#039; - repository of deferred substitutions&lt;br /&gt;
 (define-type Env&lt;br /&gt;
      [mtEnv]&lt;br /&gt;
      [anEnv (name symbol?)(value CFWAE-Value?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Expression Closures:&#039;&#039;&#039;&lt;br /&gt;
 (define-type CFWAE-Value&lt;br /&gt;
      [numV (num number?)]&lt;br /&gt;
      [exprV (body CFWAE?)(env Env?)] ;; used to hold an unevaluated expression and its environment (used in lazy interp)&lt;br /&gt;
      [closureV (param symbol?)(body CFWAE?)(env Env?)]) ;;used to close over the existing environment to ensure static scope&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;map&#039;&#039;&#039; - performs a procedure on all elements of the list in order&lt;br /&gt;
 map &amp;lt;procedure&amp;gt; &amp;lt;list&amp;gt;&lt;br /&gt;
 (define (parseList list)&lt;br /&gt;
 (map parse list))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Type-case&#039;&#039;&#039;&lt;br /&gt;
 (type-case WAE expr&lt;br /&gt;
             [num (n) expr]&lt;br /&gt;
             ...)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define-type&#039;&#039;&#039; (From assignment 1)&lt;br /&gt;
 (define-type WAE&lt;br /&gt;
  [num (n number?)]&lt;br /&gt;
  [binop (op procedure?) (lhs WAE?) (rhs WAE?)]&lt;br /&gt;
  [with (b Binding?) (body WAE?)]&lt;br /&gt;
  [with* (lob (listof Binding?)) (body WAE?)]&lt;br /&gt;
  [id (name symbol?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Recursion interp&#039;&#039;&#039; (From lecture)                                            &lt;br /&gt;
 (define (interp exp env)                                                      &lt;br /&gt;
  (type-case CFAE exp                                                         &lt;br /&gt;
      ...&lt;br /&gt;
     (id  (name) (lookup name env))&lt;br /&gt;
     (fun (arg-name body) (closureV arg-name body env))&lt;br /&gt;
     (if0 (tst thn els) &lt;br /&gt;
          (type-case CFAE-value (interp tst env) &lt;br /&gt;
             (numV (n) (if (zero? n) (interp thn env) (interp els env)))&lt;br /&gt;
             (else (error &amp;quot;non numeric if0 test value&amp;quot;))))&lt;br /&gt;
     (app (fun-exp arg-exp)&lt;br /&gt;
          (let ((the-fun (interp fun-exp env))&lt;br /&gt;
                (the-arg (interp arg-exp env)))&lt;br /&gt;
            (type-case CFAE-value the-fun&lt;br /&gt;
              (closureV (arg-name body closure-env)&lt;br /&gt;
                        (interp body (anEnv arg-name the-arg&lt;br /&gt;
                                            closure-env)))&lt;br /&gt;
              (else (error &amp;quot;You can only apply closures!&amp;quot;)))))&lt;br /&gt;
     (rec (bound-id named-exp body)&lt;br /&gt;
       (local ([define new-env (anEnv bound-id &lt;br /&gt;
                                      (numV 0)&lt;br /&gt;
                                      env)]&lt;br /&gt;
               [define named-value (interp named-exp new-env)])&lt;br /&gt;
         (begin&lt;br /&gt;
           (set-anEnv-val! new-env named-value)  ;; Patches (numV 0) out for the correct value&lt;br /&gt;
           (interp body new-env))))&lt;br /&gt;
       &lt;br /&gt;
     &lt;br /&gt;
     ))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Strict code&#039;&#039;&#039; (from lecture)&lt;br /&gt;
 (define (strict val)&lt;br /&gt;
  (if (expV? val)&lt;br /&gt;
      (strict (interp (expV-exp val) (expV-env val)))&lt;br /&gt;
      val))&lt;br /&gt;
&lt;br /&gt;
= Midterm 2 Material (copied from MT2 appendix) =&lt;br /&gt;
&lt;br /&gt;
==State==&lt;br /&gt;
*State is &amp;quot;threaded through&amp;quot; a program in the sense that the state that results from evaluating a given function is the state in which the next sequential function is to be evaluated in.&lt;br /&gt;
*State is inherently dynamic and so isn&#039;t supported by solely the environment, as it has static scope in our implementations.&lt;br /&gt;
*Closures do not remember state as function are to be applied using the state that exists where they are applied.     &lt;br /&gt;
&lt;br /&gt;
==Mutable Data Structures==&lt;br /&gt;
Need two repositories:&lt;br /&gt;
*Environment &lt;br /&gt;
**protects static scope &lt;br /&gt;
**maps identifier to location&lt;br /&gt;
**can get back to a previous location&lt;br /&gt;
**Identifier -&amp;gt; Value&lt;br /&gt;
*Store&lt;br /&gt;
**tracks dynamic changes&lt;br /&gt;
**maps location to a value&lt;br /&gt;
**changes are permanent&lt;br /&gt;
**Location -&amp;gt; Value&lt;br /&gt;
*Variables&lt;br /&gt;
**Identifier -&amp;gt; Location&lt;br /&gt;
**Location -&amp;gt; Value&lt;br /&gt;
&lt;br /&gt;
==Meta vs Syntactic Interpreters==&lt;br /&gt;
&#039;&#039;&#039;Syntactic Interpreter&#039;&#039;&#039; - An interpreter that uses the interpreting language to represent only terms of the interpreted language, implementing all the corresponding behavior explicitly&lt;br /&gt;
*doesn&#039;t use Scheme&#039;s implementation of things (like numbers)&lt;br /&gt;
*doesn&#039;t matter how well the interpreting and interpreted languages correspond&lt;br /&gt;
&#039;&#039;&#039;Meta Interpreter&#039;&#039;&#039; - An interpreter that uses language features of the interpreting language to directly implement behaviour of the interpreted language&lt;br /&gt;
*easy to write when there is a strong match between interpreted and interpreting language&lt;br /&gt;
*uses Scheme&#039;s implementation of things (closures, procedure applications, numbers, etc)&lt;br /&gt;
&#039;&#039;&#039;Meta-Circular Interpreter&#039;&#039;&#039; - A meta interpreter in which the interpreting and interpreted language are the same&lt;br /&gt;
*Only uses Scheme implementation of things&lt;br /&gt;
&#039;&#039;from PLAI, p.107-109&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Continuations==&lt;br /&gt;
&#039;&#039;&#039;What are they?&#039;&#039;&#039;&lt;br /&gt;
*stack is represented procedurally&lt;br /&gt;
*remember only the result and what is left to do (passes the state along)&lt;br /&gt;
*roughly the same idea as tail recursion&lt;br /&gt;
&#039;&#039;&#039;Why?&lt;br /&gt;
*Saves on memory and computation (ex. tail recursion summation vs augmenting recursion summation)&lt;br /&gt;
*A way of recording state when the processing computer is not always available (ex. web-servers)&lt;br /&gt;
&#039;&#039;&#039;Why Not?&lt;br /&gt;
*get rid of machine optimizations by forcing the structure of stack into continuation&lt;br /&gt;
*memory wastage if language needlessly creates stack frames&lt;br /&gt;
*need access to the source of entire program otherwise CPS translator may fail&lt;br /&gt;
*security: what if someone outside knew how to read the stack procedure?&lt;br /&gt;
&#039;&#039;&#039;Conventions&lt;br /&gt;
*Continuation methods usually end in /k&lt;br /&gt;
&lt;br /&gt;
==Variables==&lt;br /&gt;
&#039;&#039;&#039;Call-by-value&#039;&#039;&#039;&lt;br /&gt;
*evaluated argument is held in a new location&lt;br /&gt;
*changes to the content of that location in the store don&#039;t affect the actual parameter&lt;br /&gt;
&#039;&#039;&#039;Call-by-reference&#039;&#039;&#039;&lt;br /&gt;
*pass a reference to the actual argument, not the value&lt;br /&gt;
*updates to the reference within the called procedure will become visible to the calling context&lt;br /&gt;
*cheaper to use (no additional allocation) but introduce problems &lt;br /&gt;
*to implement:&lt;br /&gt;
**create a closure and give it the location of the actual argument&lt;br /&gt;
**uses l-value: env lookup without store lookup&lt;br /&gt;
**any mutations to the formal parameter are now changes to the same location as the actual parameter&lt;br /&gt;
&lt;br /&gt;
==Stateful vs Stateless==&lt;br /&gt;
&#039;&#039;&#039;Stateful&#039;&#039;&#039;&lt;br /&gt;
*server maintains state information&lt;br /&gt;
*easier to program&lt;br /&gt;
**don&#039;t need setup and breakdown of state at each interaction&lt;br /&gt;
*ex: FTP&lt;br /&gt;
**interp of each command is relative to history of past commands&lt;br /&gt;
&#039;&#039;&#039;Stateless&#039;&#039;&#039;&lt;br /&gt;
*does not retain record of prior communication&lt;br /&gt;
*Web application must completely restore state of the computation for each interaction&lt;br /&gt;
*server can handle higher loads&lt;br /&gt;
*server can ignore clients who don&#039;t appear to be active&lt;br /&gt;
*must transmit enough data to resume computation&lt;br /&gt;
&lt;br /&gt;
==Web Programs==&lt;br /&gt;
&#039;&#039;&#039;Receiver&#039;&#039;&#039; - a procedure of one argument representing the pending computation&lt;br /&gt;
*any computation not mentioned in the receiver never gets performed because of the program&#039;s termination after each iteration&lt;br /&gt;
&#039;&#039;&#039;Lifting&#039;&#039;&#039; - make nested procedures into top-level procedures&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Make a program Web-Ready&#039;&#039;&#039;&lt;br /&gt;
* 1. Generate receivers that capture pending computations&lt;br /&gt;
* 2. Pass values to receivers instead of returning them&lt;br /&gt;
&#039;&#039;&#039;Implications&#039;&#039;&#039;&lt;br /&gt;
* 1. Order of evaluation&lt;br /&gt;
* 2. Transformation is global&lt;br /&gt;
**all procedures in program must consume an extra receiver&lt;br /&gt;
* 3. Sequentializes the program&lt;br /&gt;
&lt;br /&gt;
==Map Example==&lt;br /&gt;
 (define (map f l)&lt;br /&gt;
   (if (empty? l)&lt;br /&gt;
      empty&lt;br /&gt;
      (cons (f (first l)) &lt;br /&gt;
               (map f (rest l)))))&lt;br /&gt;
 &lt;br /&gt;
 becomes:&lt;br /&gt;
 (define (map/k f/k list k)&lt;br /&gt;
   (if (empty? list)&lt;br /&gt;
       (k empty)&lt;br /&gt;
       (f/k (first list)&lt;br /&gt;
            (lambda (f-result)&lt;br /&gt;
              (map/k f/k (rest list)&lt;br /&gt;
                     (lambda (r-result)&lt;br /&gt;
                       (k (cons f-result r-result))))))))&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Final_Exam_Appendix&amp;diff=66119</id>
		<title>Course:CPSC311/2010WT1/Final Exam Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Final_Exam_Appendix&amp;diff=66119"/>
		<updated>2010-12-08T06:28:34Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Final Appendix (Wiki, Student-Generated; credits at end in print version) =&lt;br /&gt;
&lt;br /&gt;
= Midterm 1 Material (copied from MT2 appendix) =&lt;br /&gt;
&lt;br /&gt;
== Definitions == &lt;br /&gt;
&#039;&#039;&#039;Abstract syntax&#039;&#039;&#039; - idealized syntax, designed for convenient use by the underlying interpreter/compiler; result of parsing a concrete syntax expression &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Concrete syntax&#039;&#039;&#039; - syntax written by the user; consumed by the parser &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Closure&#039;&#039;&#039; - a wrapper that contains an expression (function/ binding) and its environment; expression saved until it needs to be used &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;CPS&#039;&#039;&#039; stands for Continuation-Passing Style (a.k.a. Tail Call Elimination)&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Static vs. Dynamic Scoping ==&lt;br /&gt;
&#039;&#039;&#039;Static Scope:&#039;&#039;&#039; In a language with static scope, the scope of an identifier&#039;s binding is a syntactically delimited region.  A typical region would be the body of a function or other binding construct.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Dynamic Scope:&#039;&#039;&#039; In a language with dynamic scope, the scope of an identifier&#039;s binding is the entire remainder of the execution during which that binding is in effect. That is, in a language with dynamic scope, if a function g binds identifier n and then invokes f, then f can refer to n—and so can every other function invoked by g until it completes its execution—even though f has no locally visible binding for n.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;from PLAI, p. 36&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== Closures and First-Class vs. First-Order Functions ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-order Functions&#039;&#039;&#039; are not values in the language. They can only be defined in a designated portion of&lt;br /&gt;
the program, where they must be given names for use in the remainder of the program.  (For a language like C, the &amp;quot;designated portion&amp;quot; is anywhere in the top-level of a source file, but not, for example, within the body of another function.)  The functions&lt;br /&gt;
in F1WAE are of this nature, which explains the 1 in the name of the language.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Higher-order Functions&#039;&#039;&#039; can return other functions as values and take other functions as parameters.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-class Functions&#039;&#039;&#039; are values with all the rights of other values. In particular, they can be supplied as the&lt;br /&gt;
value of arguments to functions, returned by functions as answers, constructed as values by expressions, and stored in data structures.&lt;br /&gt;
&lt;br /&gt;
== Deferred Substitution ==&lt;br /&gt;
&#039;&#039;&#039;Deferred Substitution&#039;&#039;&#039; Initially, we have no substitutions to perform, so the repository&lt;br /&gt;
is empty. Every time we encounter a substitution (in the form of a with or application), we augment the&lt;br /&gt;
repository with one more entry, recording the identiﬁer’s name and the value (if eager) or expression (if&lt;br /&gt;
lazy) it should eventually be substituted with. We continue to evaluate without actually performing the&lt;br /&gt;
substitution. &lt;br /&gt;
&lt;br /&gt;
A side-effect of deferred substitution is dynamic scoping, which we were able to fix by using closures to store the environment from which the function was created in.&lt;br /&gt;
&lt;br /&gt;
== Eager vs. Lazy Evaluation ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lazy With&#039;&#039;&#039;- we reduce the named expression to a value only when we need to &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Lazy Functions&#039;&#039;&#039;- we do not reduce the argument to a value until necessary&amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Strictness Points&#039;&#039;&#039;- the points where the implementation of a lazy language forces an expression to reduce to a value (example: in a function application, need to evaluate exactly what function to invoke, can&#039;t just leave it as a function closure) &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Eager&#039;&#039;&#039;- the named expression is reduced to a value before being substituting it. &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Backus-Naur Form (BNF) ==&lt;br /&gt;
&lt;br /&gt;
A concise specification language for expressing (among other things) the concrete syntax of a programming language.  A BNF often strongly suggests a corresponding abstract syntax.&lt;br /&gt;
&lt;br /&gt;
Here&#039;s an example BNF for a language with arithmetic expressions, with expressions, first-class functions and applications, a conditional statement, and explicit language support for recursion.  This particular language allows an arbitrary number of parameters to a function in its definition or arguments in a function application.&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;CFWAE&amp;gt; ::= &amp;lt;num&amp;gt;&lt;br /&gt;
    | {+ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {- &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {* &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {/ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | &amp;lt;id&amp;gt;&lt;br /&gt;
    | {if0 &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {with {&amp;lt;id&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {fun {&amp;lt;id&amp;gt; ...} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {&amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; ...}&lt;br /&gt;
    | {rec {&amp;lt;symbol&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
&lt;br /&gt;
== Racket syntax, helpful procedures, and thoughts ==&lt;br /&gt;
&lt;br /&gt;
 (cond&lt;br /&gt;
      [(number? v) ...]&lt;br /&gt;
      [(boolean? v) ...]&lt;br /&gt;
      [(string? v) ...]&lt;br /&gt;
      [else false])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(match data&lt;br /&gt;
   [pattern  action]&lt;br /&gt;
      [(? number?) (foo)]                    ;; check if data is number &lt;br /&gt;
      [(list a b c) (foo a b c) ]            ;; back-reference of list elements&lt;br /&gt;
      [((and n (? number?))) (foo n)]        ;; check if data is number and back-reference it as n&lt;br /&gt;
      [else (foo)]&lt;br /&gt;
)&lt;br /&gt;
;; Keep in mind that in pattern matching&lt;br /&gt;
;; &#039;...&#039; or &#039;___&#039; means 0 or more &lt;br /&gt;
;; &#039;..n&#039; or &#039;__n&amp;quot; means n or more &lt;br /&gt;
;; &#039;_&#039; matches anything&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;begin:&#039;&#039;&#039; Evaluates all expressions passed as arguments to begin, in order, and the results are all ignored except for the last one.&lt;br /&gt;
 &amp;gt; (begin (+ 5 2) (printf &amp;quot;Hello World!\n&amp;quot;) (+ 3 1))&lt;br /&gt;
 Hello World!&lt;br /&gt;
 4&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Variable definition:&#039;&#039;&#039;&lt;br /&gt;
 (define x 3)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Function definition:&#039;&#039;&#039;&lt;br /&gt;
 (define (funcName arg)&lt;br /&gt;
     (+ 1 arg))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Environment:&#039;&#039;&#039; - repository of deferred substitutions&lt;br /&gt;
 (define-type Env&lt;br /&gt;
      [mtEnv]&lt;br /&gt;
      [anEnv (name symbol?)(value CFWAE-Value?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Expression Closures:&#039;&#039;&#039;&lt;br /&gt;
 (define-type CFWAE-Value&lt;br /&gt;
      [numV (num number?)]&lt;br /&gt;
      [exprV (body CFWAE?)(env Env?)] ;; used to hold an unevaluated expression and its environment (used in lazy interp)&lt;br /&gt;
      [closureV (param symbol?)(body CFWAE?)(env Env?)]) ;;used to close over the existing environment to ensure static scope&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;map&#039;&#039;&#039; - performs a procedure on all elements of the list in order&lt;br /&gt;
 map &amp;lt;procedure&amp;gt; &amp;lt;list&amp;gt;&lt;br /&gt;
 (define (parseList list)&lt;br /&gt;
 (map parse list))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Type-case&#039;&#039;&#039;&lt;br /&gt;
 (type-case WAE expr&lt;br /&gt;
             [num (n) expr]&lt;br /&gt;
             ...)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define-type&#039;&#039;&#039; (From assignment 1)&lt;br /&gt;
 (define-type WAE&lt;br /&gt;
  [num (n number?)]&lt;br /&gt;
  [binop (op procedure?) (lhs WAE?) (rhs WAE?)]&lt;br /&gt;
  [with (b Binding?) (body WAE?)]&lt;br /&gt;
  [with* (lob (listof Binding?)) (body WAE?)]&lt;br /&gt;
  [id (name symbol?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Recursion interp&#039;&#039;&#039; (From lecture)                                            &lt;br /&gt;
 (define (interp exp env)                                                      &lt;br /&gt;
  (type-case CFAE exp                                                         &lt;br /&gt;
      ...&lt;br /&gt;
     (id  (name) (lookup name env))&lt;br /&gt;
     (fun (arg-name body) (closureV arg-name body env))&lt;br /&gt;
     (if0 (tst thn els) &lt;br /&gt;
          (type-case CFAE-value (interp tst env) &lt;br /&gt;
             (numV (n) (if (zero? n) (interp thn env) (interp els env)))&lt;br /&gt;
             (else (error &amp;quot;non numeric if0 test value&amp;quot;))))&lt;br /&gt;
     (app (fun-exp arg-exp)&lt;br /&gt;
          (let ((the-fun (interp fun-exp env))&lt;br /&gt;
                (the-arg (interp arg-exp env)))&lt;br /&gt;
            (type-case CFAE-value the-fun&lt;br /&gt;
              (closureV (arg-name body closure-env)&lt;br /&gt;
                        (interp body (anEnv arg-name the-arg&lt;br /&gt;
                                            closure-env)))&lt;br /&gt;
              (else (error &amp;quot;You can only apply closures!&amp;quot;)))))&lt;br /&gt;
     (rec (bound-id named-exp body)&lt;br /&gt;
       (local ([define new-env (anEnv bound-id &lt;br /&gt;
                                      (numV 0)&lt;br /&gt;
                                      env)]&lt;br /&gt;
               [define named-value (interp named-exp new-env)])&lt;br /&gt;
         (begin&lt;br /&gt;
           (set-anEnv-val! new-env named-value)  ;; Patches (numV 0) out for the correct value&lt;br /&gt;
           (interp body new-env))))&lt;br /&gt;
       &lt;br /&gt;
     &lt;br /&gt;
     ))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Strict code&#039;&#039;&#039; (from lecture)&lt;br /&gt;
 (define (strict val)&lt;br /&gt;
  (if (expV? val)&lt;br /&gt;
      (strict (interp (expV-exp val) (expV-env val)))&lt;br /&gt;
      val))&lt;br /&gt;
&lt;br /&gt;
= Midterm 2 Material (copied from MT2 appendix) =&lt;br /&gt;
&lt;br /&gt;
==State==&lt;br /&gt;
*State is &amp;quot;threaded through&amp;quot; a program in the sense that the state that results from evaluating a given function is the state in which the next sequential function is to be evaluated in.&lt;br /&gt;
*State is inherently dynamic and so isn&#039;t supported by solely the environment, as it has static scope in our implementations.&lt;br /&gt;
*Closures do not remember state as function are to be applied using the state that exists where they are applied.     &lt;br /&gt;
&lt;br /&gt;
==Mutable Data Structures==&lt;br /&gt;
Need two repositories:&lt;br /&gt;
*Environment &lt;br /&gt;
**protects static scope &lt;br /&gt;
**maps identifier to location&lt;br /&gt;
**can get back to a previous location&lt;br /&gt;
**Identifier -&amp;gt; Value&lt;br /&gt;
*Store&lt;br /&gt;
**tracks dynamic changes&lt;br /&gt;
**maps location to a value&lt;br /&gt;
**changes are permanent&lt;br /&gt;
**Location -&amp;gt; Value&lt;br /&gt;
*Variables&lt;br /&gt;
**Identifier -&amp;gt; Location&lt;br /&gt;
**Location -&amp;gt; Value&lt;br /&gt;
&lt;br /&gt;
==Meta vs Syntactic Interpreters==&lt;br /&gt;
&#039;&#039;&#039;Syntactic Interpreter&#039;&#039;&#039; - An interpreter that uses the interpreting language to represent only terms of the interpreted language, implementing all the corresponding behavior explicitly&lt;br /&gt;
*doesn&#039;t use Scheme&#039;s implementation of things (like numbers)&lt;br /&gt;
*doesn&#039;t matter how well the interpreting and interpreted languages correspond&lt;br /&gt;
&#039;&#039;&#039;Meta Interpreter&#039;&#039;&#039; - An interpreter that uses language features of the interpreting language to directly implement behaviour of the interpreted language&lt;br /&gt;
*easy to write when there is a strong match between interpreted and interpreting language&lt;br /&gt;
*uses Scheme&#039;s implementation of things (closures, procedure applications, numbers, etc)&lt;br /&gt;
&#039;&#039;&#039;Meta-Circular Interpreter&#039;&#039;&#039; - A meta interpreter in which the interpreting and interpreted language are the same&lt;br /&gt;
*Only uses Scheme implementation of things&lt;br /&gt;
&#039;&#039;from PLAI, p.107-109&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Continuations==&lt;br /&gt;
&#039;&#039;&#039;What are they?&#039;&#039;&#039;&lt;br /&gt;
*stack is represented procedurally&lt;br /&gt;
*remember only the result and what is left to do (passes the state along)&lt;br /&gt;
*roughly the same idea as tail recursion&lt;br /&gt;
&#039;&#039;&#039;Why?&lt;br /&gt;
*Saves on memory and computation (ex. tail recursion summation vs augmenting recursion summation)&lt;br /&gt;
*A way of recording state when the processing computer is not always available (ex. web-servers)&lt;br /&gt;
&#039;&#039;&#039;Why Not?&lt;br /&gt;
*get rid of machine optimizations by forcing the structure of stack into continuation&lt;br /&gt;
*memory wastage if language needlessly creates stack frames&lt;br /&gt;
*need access to the source of entire program otherwise CPS translator may fail&lt;br /&gt;
*security: what if someone outside knew how to read the stack procedure?&lt;br /&gt;
&#039;&#039;&#039;Conventions&lt;br /&gt;
*Continuation methods usually end in /k&lt;br /&gt;
&lt;br /&gt;
==Variables==&lt;br /&gt;
&#039;&#039;&#039;Call-by-value&#039;&#039;&#039;&lt;br /&gt;
*evaluated argument is held in a new location&lt;br /&gt;
*changes to the content of that location in the store don&#039;t affect the actual parameter&lt;br /&gt;
&#039;&#039;&#039;Call-by-reference&#039;&#039;&#039;&lt;br /&gt;
*pass a reference to the actual argument, not the value&lt;br /&gt;
*updates to the reference within the called procedure will become visible to the calling context&lt;br /&gt;
*cheaper to use (no additional allocation) but introduce problems &lt;br /&gt;
*to implement:&lt;br /&gt;
**create a closure and give it the location of the actual argument&lt;br /&gt;
**uses l-value: env lookup without store lookup&lt;br /&gt;
**any mutations to the formal parameter are now changes to the same location as the actual parameter&lt;br /&gt;
&lt;br /&gt;
==Stateful vs Stateless==&lt;br /&gt;
&#039;&#039;&#039;Stateful&#039;&#039;&#039;&lt;br /&gt;
*server maintains state information&lt;br /&gt;
*easier to program&lt;br /&gt;
**don&#039;t need setup and breakdown of state at each interaction&lt;br /&gt;
*ex: FTP&lt;br /&gt;
**interp of each command is relative to history of past commands&lt;br /&gt;
&#039;&#039;&#039;Stateless&#039;&#039;&#039;&lt;br /&gt;
*does not retain record of prior communication&lt;br /&gt;
*Web application must completely restore state of the computation for each interaction&lt;br /&gt;
*server can handle higher loads&lt;br /&gt;
*server can ignore clients who don&#039;t appear to be active&lt;br /&gt;
*must transmit enough data to resume computation&lt;br /&gt;
&lt;br /&gt;
==Web Programs==&lt;br /&gt;
&#039;&#039;&#039;Receiver&#039;&#039;&#039; - a procedure of one argument representing the pending computation&lt;br /&gt;
*any computation not mentioned in the receiver never gets performed because of the program&#039;s termination after each iteration&lt;br /&gt;
&#039;&#039;&#039;Lifting&#039;&#039;&#039; - make nested procedures into top-level procedures&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Make a program Web-Ready&#039;&#039;&#039;&lt;br /&gt;
* 1. Generate receivers that capture pending computations&lt;br /&gt;
* 2. Pass values to receivers instead of returning them&lt;br /&gt;
&#039;&#039;&#039;Implications&#039;&#039;&#039;&lt;br /&gt;
* 1. Order of evaluation&lt;br /&gt;
* 2. Transformation is global&lt;br /&gt;
**all procedures in program must consume an extra receiver&lt;br /&gt;
* 3. Sequentializes the program&lt;br /&gt;
&lt;br /&gt;
==Map Example==&lt;br /&gt;
 (define (map f l)&lt;br /&gt;
   (if (empty? l)&lt;br /&gt;
      empty&lt;br /&gt;
      (cons (f (first l)) &lt;br /&gt;
               (map f (rest l)))))&lt;br /&gt;
 &lt;br /&gt;
 becomes:&lt;br /&gt;
 (define (map/k f/k list k)&lt;br /&gt;
   (if (empty? list)&lt;br /&gt;
       (k empty)&lt;br /&gt;
       (f/k (first list)&lt;br /&gt;
            (lambda (f-result)&lt;br /&gt;
              (map/k f/k (rest list)&lt;br /&gt;
                     (lambda (r-result)&lt;br /&gt;
                       (k (cons f-result r-result))))))))&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Final_Exam_Appendix&amp;diff=66118</id>
		<title>Course:CPSC311/2010WT1/Final Exam Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Final_Exam_Appendix&amp;diff=66118"/>
		<updated>2010-12-08T06:27:16Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Final Appendix (Wiki, Student-Generated; credits at end) =&lt;br /&gt;
&lt;br /&gt;
= Midterm 1 Material (copied from MT2 appendix) =&lt;br /&gt;
&lt;br /&gt;
== Definitions == &lt;br /&gt;
&#039;&#039;&#039;Abstract syntax&#039;&#039;&#039; - idealized syntax, designed for convenient use by the underlying interpreter/compiler; result of parsing a concrete syntax expression &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Concrete syntax&#039;&#039;&#039; - syntax written by the user; consumed by the parser &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Closure&#039;&#039;&#039; - a wrapper that contains an expression (function/ binding) and its environment; expression saved until it needs to be used &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;CPS&#039;&#039;&#039; stands for Continuation-Passing Style (a.k.a. Tail Call Elimination)&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Static vs. Dynamic Scoping ==&lt;br /&gt;
&#039;&#039;&#039;Static Scope:&#039;&#039;&#039; In a language with static scope, the scope of an identifier&#039;s binding is a syntactically delimited region.  A typical region would be the body of a function or other binding construct.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Dynamic Scope:&#039;&#039;&#039; In a language with dynamic scope, the scope of an identifier&#039;s binding is the entire remainder of the execution during which that binding is in effect. That is, in a language with dynamic scope, if a function g binds identifier n and then invokes f, then f can refer to n—and so can every other function invoked by g until it completes its execution—even though f has no locally visible binding for n.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;from PLAI, p. 36&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== Closures and First-Class vs. First-Order Functions ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-order Functions&#039;&#039;&#039; are not values in the language. They can only be defined in a designated portion of&lt;br /&gt;
the program, where they must be given names for use in the remainder of the program.  (For a language like C, the &amp;quot;designated portion&amp;quot; is anywhere in the top-level of a source file, but not, for example, within the body of another function.)  The functions&lt;br /&gt;
in F1WAE are of this nature, which explains the 1 in the name of the language.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Higher-order Functions&#039;&#039;&#039; can return other functions as values and take other functions as parameters.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-class Functions&#039;&#039;&#039; are values with all the rights of other values. In particular, they can be supplied as the&lt;br /&gt;
value of arguments to functions, returned by functions as answers, constructed as values by expressions, and stored in data structures.&lt;br /&gt;
&lt;br /&gt;
== Deferred Substitution ==&lt;br /&gt;
&#039;&#039;&#039;Deferred Substitution&#039;&#039;&#039; Initially, we have no substitutions to perform, so the repository&lt;br /&gt;
is empty. Every time we encounter a substitution (in the form of a with or application), we augment the&lt;br /&gt;
repository with one more entry, recording the identiﬁer’s name and the value (if eager) or expression (if&lt;br /&gt;
lazy) it should eventually be substituted with. We continue to evaluate without actually performing the&lt;br /&gt;
substitution. &lt;br /&gt;
&lt;br /&gt;
A side-effect of deferred substitution is dynamic scoping, which we were able to fix by using closures to store the environment from which the function was created in.&lt;br /&gt;
&lt;br /&gt;
== Eager vs. Lazy Evaluation ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lazy With&#039;&#039;&#039;- we reduce the named expression to a value only when we need to &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Lazy Functions&#039;&#039;&#039;- we do not reduce the argument to a value until necessary&amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Strictness Points&#039;&#039;&#039;- the points where the implementation of a lazy language forces an expression to reduce to a value (example: in a function application, need to evaluate exactly what function to invoke, can&#039;t just leave it as a function closure) &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Eager&#039;&#039;&#039;- the named expression is reduced to a value before being substituting it. &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Backus-Naur Form (BNF) ==&lt;br /&gt;
&lt;br /&gt;
A concise specification language for expressing (among other things) the concrete syntax of a programming language.  A BNF often strongly suggests a corresponding abstract syntax.&lt;br /&gt;
&lt;br /&gt;
Here&#039;s an example BNF for a language with arithmetic expressions, with expressions, first-class functions and applications, a conditional statement, and explicit language support for recursion.  This particular language allows an arbitrary number of parameters to a function in its definition or arguments in a function application.&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;CFWAE&amp;gt; ::= &amp;lt;num&amp;gt;&lt;br /&gt;
    | {+ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {- &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {* &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {/ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | &amp;lt;id&amp;gt;&lt;br /&gt;
    | {if0 &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {with {&amp;lt;id&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {fun {&amp;lt;id&amp;gt; ...} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {&amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; ...}&lt;br /&gt;
    | {rec {&amp;lt;symbol&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
&lt;br /&gt;
== Racket syntax, helpful procedures, and thoughts ==&lt;br /&gt;
&lt;br /&gt;
 (cond&lt;br /&gt;
      [(number? v) ...]&lt;br /&gt;
      [(boolean? v) ...]&lt;br /&gt;
      [(string? v) ...]&lt;br /&gt;
      [else false])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(match data&lt;br /&gt;
   [pattern  action]&lt;br /&gt;
      [(? number?) (foo)]                    ;; check if data is number &lt;br /&gt;
      [(list a b c) (foo a b c) ]            ;; back-reference of list elements&lt;br /&gt;
      [((and n (? number?))) (foo n)]        ;; check if data is number and back-reference it as n&lt;br /&gt;
      [else (foo)]&lt;br /&gt;
)&lt;br /&gt;
;; Keep in mind that in pattern matching&lt;br /&gt;
;; &#039;...&#039; or &#039;___&#039; means 0 or more &lt;br /&gt;
;; &#039;..n&#039; or &#039;__n&amp;quot; means n or more &lt;br /&gt;
;; &#039;_&#039; matches anything&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;begin:&#039;&#039;&#039; Evaluates all expressions passed as arguments to begin, in order, and the results are all ignored except for the last one.&lt;br /&gt;
 &amp;gt; (begin (+ 5 2) (printf &amp;quot;Hello World!\n&amp;quot;) (+ 3 1))&lt;br /&gt;
 Hello World!&lt;br /&gt;
 4&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Variable definition:&#039;&#039;&#039;&lt;br /&gt;
 (define x 3)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Function definition:&#039;&#039;&#039;&lt;br /&gt;
 (define (funcName arg)&lt;br /&gt;
     (+ 1 arg))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Environment:&#039;&#039;&#039; - repository of deferred substitutions&lt;br /&gt;
 (define-type Env&lt;br /&gt;
      [mtEnv]&lt;br /&gt;
      [anEnv (name symbol?)(value CFWAE-Value?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Expression Closures:&#039;&#039;&#039;&lt;br /&gt;
 (define-type CFWAE-Value&lt;br /&gt;
      [numV (num number?)]&lt;br /&gt;
      [exprV (body CFWAE?)(env Env?)] ;; used to hold an unevaluated expression and its environment (used in lazy interp)&lt;br /&gt;
      [closureV (param symbol?)(body CFWAE?)(env Env?)]) ;;used to close over the existing environment to ensure static scope&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;map&#039;&#039;&#039; - performs a procedure on all elements of the list in order&lt;br /&gt;
 map &amp;lt;procedure&amp;gt; &amp;lt;list&amp;gt;&lt;br /&gt;
 (define (parseList list)&lt;br /&gt;
 (map parse list))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Type-case&#039;&#039;&#039;&lt;br /&gt;
 (type-case WAE expr&lt;br /&gt;
             [num (n) expr]&lt;br /&gt;
             ...)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define-type&#039;&#039;&#039; (From assignment 1)&lt;br /&gt;
 (define-type WAE&lt;br /&gt;
  [num (n number?)]&lt;br /&gt;
  [binop (op procedure?) (lhs WAE?) (rhs WAE?)]&lt;br /&gt;
  [with (b Binding?) (body WAE?)]&lt;br /&gt;
  [with* (lob (listof Binding?)) (body WAE?)]&lt;br /&gt;
  [id (name symbol?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Recursion interp&#039;&#039;&#039; (From lecture)                                            &lt;br /&gt;
 (define (interp exp env)                                                      &lt;br /&gt;
  (type-case CFAE exp                                                         &lt;br /&gt;
      ...&lt;br /&gt;
     (id  (name) (lookup name env))&lt;br /&gt;
     (fun (arg-name body) (closureV arg-name body env))&lt;br /&gt;
     (if0 (tst thn els) &lt;br /&gt;
          (type-case CFAE-value (interp tst env) &lt;br /&gt;
             (numV (n) (if (zero? n) (interp thn env) (interp els env)))&lt;br /&gt;
             (else (error &amp;quot;non numeric if0 test value&amp;quot;))))&lt;br /&gt;
     (app (fun-exp arg-exp)&lt;br /&gt;
          (let ((the-fun (interp fun-exp env))&lt;br /&gt;
                (the-arg (interp arg-exp env)))&lt;br /&gt;
            (type-case CFAE-value the-fun&lt;br /&gt;
              (closureV (arg-name body closure-env)&lt;br /&gt;
                        (interp body (anEnv arg-name the-arg&lt;br /&gt;
                                            closure-env)))&lt;br /&gt;
              (else (error &amp;quot;You can only apply closures!&amp;quot;)))))&lt;br /&gt;
     (rec (bound-id named-exp body)&lt;br /&gt;
       (local ([define new-env (anEnv bound-id &lt;br /&gt;
                                      (numV 0)&lt;br /&gt;
                                      env)]&lt;br /&gt;
               [define named-value (interp named-exp new-env)])&lt;br /&gt;
         (begin&lt;br /&gt;
           (set-anEnv-val! new-env named-value)  ;; Patches (numV 0) out for the correct value&lt;br /&gt;
           (interp body new-env))))&lt;br /&gt;
       &lt;br /&gt;
     &lt;br /&gt;
     ))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Strict code&#039;&#039;&#039; (from lecture)&lt;br /&gt;
 (define (strict val)&lt;br /&gt;
  (if (expV? val)&lt;br /&gt;
      (strict (interp (expV-exp val) (expV-env val)))&lt;br /&gt;
      val))&lt;br /&gt;
&lt;br /&gt;
= Midterm 2 Material (copied from MT2 appendix) =&lt;br /&gt;
&lt;br /&gt;
==State==&lt;br /&gt;
*State is &amp;quot;threaded through&amp;quot; a program in the sense that the state that results from evaluating a given function is the state in which the next sequential function is to be evaluated in.&lt;br /&gt;
*State is inherently dynamic and so isn&#039;t supported by solely the environment, as it has static scope in our implementations.&lt;br /&gt;
*Closures do not remember state as function are to be applied using the state that exists where they are applied.     &lt;br /&gt;
&lt;br /&gt;
==Mutable Data Structures==&lt;br /&gt;
Need two repositories:&lt;br /&gt;
*Environment &lt;br /&gt;
**protects static scope &lt;br /&gt;
**maps identifier to location&lt;br /&gt;
**can get back to a previous location&lt;br /&gt;
**Identifier -&amp;gt; Value&lt;br /&gt;
*Store&lt;br /&gt;
**tracks dynamic changes&lt;br /&gt;
**maps location to a value&lt;br /&gt;
**changes are permanent&lt;br /&gt;
**Location -&amp;gt; Value&lt;br /&gt;
*Variables&lt;br /&gt;
**Identifier -&amp;gt; Location&lt;br /&gt;
**Location -&amp;gt; Value&lt;br /&gt;
&lt;br /&gt;
==Meta vs Syntactic Interpreters==&lt;br /&gt;
&#039;&#039;&#039;Syntactic Interpreter&#039;&#039;&#039; - An interpreter that uses the interpreting language to represent only terms of the interpreted language, implementing all the corresponding behavior explicitly&lt;br /&gt;
*doesn&#039;t use Scheme&#039;s implementation of things (like numbers)&lt;br /&gt;
*doesn&#039;t matter how well the interpreting and interpreted languages correspond&lt;br /&gt;
&#039;&#039;&#039;Meta Interpreter&#039;&#039;&#039; - An interpreter that uses language features of the interpreting language to directly implement behaviour of the interpreted language&lt;br /&gt;
*easy to write when there is a strong match between interpreted and interpreting language&lt;br /&gt;
*uses Scheme&#039;s implementation of things (closures, procedure applications, numbers, etc)&lt;br /&gt;
&#039;&#039;&#039;Meta-Circular Interpreter&#039;&#039;&#039; - A meta interpreter in which the interpreting and interpreted language are the same&lt;br /&gt;
*Only uses Scheme implementation of things&lt;br /&gt;
&#039;&#039;from PLAI, p.107-109&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Continuations==&lt;br /&gt;
&#039;&#039;&#039;What are they?&#039;&#039;&#039;&lt;br /&gt;
*stack is represented procedurally&lt;br /&gt;
*remember only the result and what is left to do (passes the state along)&lt;br /&gt;
*roughly the same idea as tail recursion&lt;br /&gt;
&#039;&#039;&#039;Why?&lt;br /&gt;
*Saves on memory and computation (ex. tail recursion summation vs augmenting recursion summation)&lt;br /&gt;
*A way of recording state when the processing computer is not always available (ex. web-servers)&lt;br /&gt;
&#039;&#039;&#039;Why Not?&lt;br /&gt;
*get rid of machine optimizations by forcing the structure of stack into continuation&lt;br /&gt;
*memory wastage if language needlessly creates stack frames&lt;br /&gt;
*need access to the source of entire program otherwise CPS translator may fail&lt;br /&gt;
*security: what if someone outside knew how to read the stack procedure?&lt;br /&gt;
&#039;&#039;&#039;Conventions&lt;br /&gt;
*Continuation methods usually end in /k&lt;br /&gt;
&lt;br /&gt;
==Variables==&lt;br /&gt;
&#039;&#039;&#039;Call-by-value&#039;&#039;&#039;&lt;br /&gt;
*evaluated argument is held in a new location&lt;br /&gt;
*changes to the content of that location in the store don&#039;t affect the actual parameter&lt;br /&gt;
&#039;&#039;&#039;Call-by-reference&#039;&#039;&#039;&lt;br /&gt;
*pass a reference to the actual argument, not the value&lt;br /&gt;
*updates to the reference within the called procedure will become visible to the calling context&lt;br /&gt;
*cheaper to use (no additional allocation) but introduce problems &lt;br /&gt;
*to implement:&lt;br /&gt;
**create a closure and give it the location of the actual argument&lt;br /&gt;
**uses l-value: env lookup without store lookup&lt;br /&gt;
**any mutations to the formal parameter are now changes to the same location as the actual parameter&lt;br /&gt;
&lt;br /&gt;
==Stateful vs Stateless==&lt;br /&gt;
&#039;&#039;&#039;Stateful&#039;&#039;&#039;&lt;br /&gt;
*server maintains state information&lt;br /&gt;
*easier to program&lt;br /&gt;
**don&#039;t need setup and breakdown of state at each interaction&lt;br /&gt;
*ex: FTP&lt;br /&gt;
**interp of each command is relative to history of past commands&lt;br /&gt;
&#039;&#039;&#039;Stateless&#039;&#039;&#039;&lt;br /&gt;
*does not retain record of prior communication&lt;br /&gt;
*Web application must completely restore state of the computation for each interaction&lt;br /&gt;
*server can handle higher loads&lt;br /&gt;
*server can ignore clients who don&#039;t appear to be active&lt;br /&gt;
*must transmit enough data to resume computation&lt;br /&gt;
&lt;br /&gt;
==Web Programs==&lt;br /&gt;
&#039;&#039;&#039;Receiver&#039;&#039;&#039; - a procedure of one argument representing the pending computation&lt;br /&gt;
*any computation not mentioned in the receiver never gets performed because of the program&#039;s termination after each iteration&lt;br /&gt;
&#039;&#039;&#039;Lifting&#039;&#039;&#039; - make nested procedures into top-level procedures&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Make a program Web-Ready&#039;&#039;&#039;&lt;br /&gt;
* 1. Generate receivers that capture pending computations&lt;br /&gt;
* 2. Pass values to receivers instead of returning them&lt;br /&gt;
&#039;&#039;&#039;Implications&#039;&#039;&#039;&lt;br /&gt;
* 1. Order of evaluation&lt;br /&gt;
* 2. Transformation is global&lt;br /&gt;
**all procedures in program must consume an extra receiver&lt;br /&gt;
* 3. Sequentializes the program&lt;br /&gt;
&lt;br /&gt;
==Map Example==&lt;br /&gt;
 (define (map f l)&lt;br /&gt;
   (if (empty? l)&lt;br /&gt;
      empty&lt;br /&gt;
      (cons (f (first l)) &lt;br /&gt;
               (map f (rest l)))))&lt;br /&gt;
 &lt;br /&gt;
 becomes:&lt;br /&gt;
 (define (map/k f/k list k)&lt;br /&gt;
   (if (empty? list)&lt;br /&gt;
       (k empty)&lt;br /&gt;
       (f/k (first list)&lt;br /&gt;
            (lambda (f-result)&lt;br /&gt;
              (map/k f/k (rest list)&lt;br /&gt;
                     (lambda (r-result)&lt;br /&gt;
                       (k (cons f-result r-result))))))))&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Final_Exam_Appendix&amp;diff=66117</id>
		<title>Course:CPSC311/2010WT1/Final Exam Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Final_Exam_Appendix&amp;diff=66117"/>
		<updated>2010-12-08T06:26:28Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: Created page with &amp;#039;= Final Appendix (Wiki, Student-Generated; credits at end) =  = Midterm 1 and 2 Material (copy of MT2 appendix) =  == Definitions ==  &amp;#039;&amp;#039;&amp;#039;Abstract syntax&amp;#039;&amp;#039;&amp;#039; - idealized syntax, de…&amp;#039;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Final Appendix (Wiki, Student-Generated; credits at end) =&lt;br /&gt;
&lt;br /&gt;
= Midterm 1 and 2 Material (copy of MT2 appendix) =&lt;br /&gt;
&lt;br /&gt;
== Definitions == &lt;br /&gt;
&#039;&#039;&#039;Abstract syntax&#039;&#039;&#039; - idealized syntax, designed for convenient use by the underlying interpreter/compiler; result of parsing a concrete syntax expression &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Concrete syntax&#039;&#039;&#039; - syntax written by the user; consumed by the parser &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Closure&#039;&#039;&#039; - a wrapper that contains an expression (function/ binding) and its environment; expression saved until it needs to be used &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;CPS&#039;&#039;&#039; stands for Continuation-Passing Style (a.k.a. Tail Call Elimination)&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Static vs. Dynamic Scoping ==&lt;br /&gt;
&#039;&#039;&#039;Static Scope:&#039;&#039;&#039; In a language with static scope, the scope of an identifier&#039;s binding is a syntactically delimited region.  A typical region would be the body of a function or other binding construct.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Dynamic Scope:&#039;&#039;&#039; In a language with dynamic scope, the scope of an identifier&#039;s binding is the entire remainder of the execution during which that binding is in effect. That is, in a language with dynamic scope, if a function g binds identifier n and then invokes f, then f can refer to n—and so can every other function invoked by g until it completes its execution—even though f has no locally visible binding for n.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;from PLAI, p. 36&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== Closures and First-Class vs. First-Order Functions ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-order Functions&#039;&#039;&#039; are not values in the language. They can only be defined in a designated portion of&lt;br /&gt;
the program, where they must be given names for use in the remainder of the program.  (For a language like C, the &amp;quot;designated portion&amp;quot; is anywhere in the top-level of a source file, but not, for example, within the body of another function.)  The functions&lt;br /&gt;
in F1WAE are of this nature, which explains the 1 in the name of the language.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Higher-order Functions&#039;&#039;&#039; can return other functions as values and take other functions as parameters.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-class Functions&#039;&#039;&#039; are values with all the rights of other values. In particular, they can be supplied as the&lt;br /&gt;
value of arguments to functions, returned by functions as answers, constructed as values by expressions, and stored in data structures.&lt;br /&gt;
&lt;br /&gt;
== Deferred Substitution ==&lt;br /&gt;
&#039;&#039;&#039;Deferred Substitution&#039;&#039;&#039; Initially, we have no substitutions to perform, so the repository&lt;br /&gt;
is empty. Every time we encounter a substitution (in the form of a with or application), we augment the&lt;br /&gt;
repository with one more entry, recording the identiﬁer’s name and the value (if eager) or expression (if&lt;br /&gt;
lazy) it should eventually be substituted with. We continue to evaluate without actually performing the&lt;br /&gt;
substitution. &lt;br /&gt;
&lt;br /&gt;
A side-effect of deferred substitution is dynamic scoping, which we were able to fix by using closures to store the environment from which the function was created in.&lt;br /&gt;
&lt;br /&gt;
== Eager vs. Lazy Evaluation ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lazy With&#039;&#039;&#039;- we reduce the named expression to a value only when we need to &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Lazy Functions&#039;&#039;&#039;- we do not reduce the argument to a value until necessary&amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Strictness Points&#039;&#039;&#039;- the points where the implementation of a lazy language forces an expression to reduce to a value (example: in a function application, need to evaluate exactly what function to invoke, can&#039;t just leave it as a function closure) &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Eager&#039;&#039;&#039;- the named expression is reduced to a value before being substituting it. &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Backus-Naur Form (BNF) ==&lt;br /&gt;
&lt;br /&gt;
A concise specification language for expressing (among other things) the concrete syntax of a programming language.  A BNF often strongly suggests a corresponding abstract syntax.&lt;br /&gt;
&lt;br /&gt;
Here&#039;s an example BNF for a language with arithmetic expressions, with expressions, first-class functions and applications, a conditional statement, and explicit language support for recursion.  This particular language allows an arbitrary number of parameters to a function in its definition or arguments in a function application.&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;CFWAE&amp;gt; ::= &amp;lt;num&amp;gt;&lt;br /&gt;
    | {+ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {- &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {* &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {/ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | &amp;lt;id&amp;gt;&lt;br /&gt;
    | {if0 &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {with {&amp;lt;id&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {fun {&amp;lt;id&amp;gt; ...} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {&amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; ...}&lt;br /&gt;
    | {rec {&amp;lt;symbol&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
&lt;br /&gt;
== Racket syntax, helpful procedures, and thoughts ==&lt;br /&gt;
&lt;br /&gt;
 (cond&lt;br /&gt;
      [(number? v) ...]&lt;br /&gt;
      [(boolean? v) ...]&lt;br /&gt;
      [(string? v) ...]&lt;br /&gt;
      [else false])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(match data&lt;br /&gt;
   [pattern  action]&lt;br /&gt;
      [(? number?) (foo)]                    ;; check if data is number &lt;br /&gt;
      [(list a b c) (foo a b c) ]            ;; back-reference of list elements&lt;br /&gt;
      [((and n (? number?))) (foo n)]        ;; check if data is number and back-reference it as n&lt;br /&gt;
      [else (foo)]&lt;br /&gt;
)&lt;br /&gt;
;; Keep in mind that in pattern matching&lt;br /&gt;
;; &#039;...&#039; or &#039;___&#039; means 0 or more &lt;br /&gt;
;; &#039;..n&#039; or &#039;__n&amp;quot; means n or more &lt;br /&gt;
;; &#039;_&#039; matches anything&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;begin:&#039;&#039;&#039; Evaluates all expressions passed as arguments to begin, in order, and the results are all ignored except for the last one.&lt;br /&gt;
 &amp;gt; (begin (+ 5 2) (printf &amp;quot;Hello World!\n&amp;quot;) (+ 3 1))&lt;br /&gt;
 Hello World!&lt;br /&gt;
 4&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Variable definition:&#039;&#039;&#039;&lt;br /&gt;
 (define x 3)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Function definition:&#039;&#039;&#039;&lt;br /&gt;
 (define (funcName arg)&lt;br /&gt;
     (+ 1 arg))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Environment:&#039;&#039;&#039; - repository of deferred substitutions&lt;br /&gt;
 (define-type Env&lt;br /&gt;
      [mtEnv]&lt;br /&gt;
      [anEnv (name symbol?)(value CFWAE-Value?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Expression Closures:&#039;&#039;&#039;&lt;br /&gt;
 (define-type CFWAE-Value&lt;br /&gt;
      [numV (num number?)]&lt;br /&gt;
      [exprV (body CFWAE?)(env Env?)] ;; used to hold an unevaluated expression and its environment (used in lazy interp)&lt;br /&gt;
      [closureV (param symbol?)(body CFWAE?)(env Env?)]) ;;used to close over the existing environment to ensure static scope&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;map&#039;&#039;&#039; - performs a procedure on all elements of the list in order&lt;br /&gt;
 map &amp;lt;procedure&amp;gt; &amp;lt;list&amp;gt;&lt;br /&gt;
 (define (parseList list)&lt;br /&gt;
 (map parse list))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Type-case&#039;&#039;&#039;&lt;br /&gt;
 (type-case WAE expr&lt;br /&gt;
             [num (n) expr]&lt;br /&gt;
             ...)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define-type&#039;&#039;&#039; (From assignment 1)&lt;br /&gt;
 (define-type WAE&lt;br /&gt;
  [num (n number?)]&lt;br /&gt;
  [binop (op procedure?) (lhs WAE?) (rhs WAE?)]&lt;br /&gt;
  [with (b Binding?) (body WAE?)]&lt;br /&gt;
  [with* (lob (listof Binding?)) (body WAE?)]&lt;br /&gt;
  [id (name symbol?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Recursion interp&#039;&#039;&#039; (From lecture)                                            &lt;br /&gt;
 (define (interp exp env)                                                      &lt;br /&gt;
  (type-case CFAE exp                                                         &lt;br /&gt;
      ...&lt;br /&gt;
     (id  (name) (lookup name env))&lt;br /&gt;
     (fun (arg-name body) (closureV arg-name body env))&lt;br /&gt;
     (if0 (tst thn els) &lt;br /&gt;
          (type-case CFAE-value (interp tst env) &lt;br /&gt;
             (numV (n) (if (zero? n) (interp thn env) (interp els env)))&lt;br /&gt;
             (else (error &amp;quot;non numeric if0 test value&amp;quot;))))&lt;br /&gt;
     (app (fun-exp arg-exp)&lt;br /&gt;
          (let ((the-fun (interp fun-exp env))&lt;br /&gt;
                (the-arg (interp arg-exp env)))&lt;br /&gt;
            (type-case CFAE-value the-fun&lt;br /&gt;
              (closureV (arg-name body closure-env)&lt;br /&gt;
                        (interp body (anEnv arg-name the-arg&lt;br /&gt;
                                            closure-env)))&lt;br /&gt;
              (else (error &amp;quot;You can only apply closures!&amp;quot;)))))&lt;br /&gt;
     (rec (bound-id named-exp body)&lt;br /&gt;
       (local ([define new-env (anEnv bound-id &lt;br /&gt;
                                      (numV 0)&lt;br /&gt;
                                      env)]&lt;br /&gt;
               [define named-value (interp named-exp new-env)])&lt;br /&gt;
         (begin&lt;br /&gt;
           (set-anEnv-val! new-env named-value)  ;; Patches (numV 0) out for the correct value&lt;br /&gt;
           (interp body new-env))))&lt;br /&gt;
       &lt;br /&gt;
     &lt;br /&gt;
     ))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Strict code&#039;&#039;&#039; (from lecture)&lt;br /&gt;
 (define (strict val)&lt;br /&gt;
  (if (expV? val)&lt;br /&gt;
      (strict (interp (expV-exp val) (expV-env val)))&lt;br /&gt;
      val))&lt;br /&gt;
&lt;br /&gt;
= Midterm 2 Material =&lt;br /&gt;
&lt;br /&gt;
==State==&lt;br /&gt;
*State is &amp;quot;threaded through&amp;quot; a program in the sense that the state that results from evaluating a given function is the state in which the next sequential function is to be evaluated in.&lt;br /&gt;
*State is inherently dynamic and so isn&#039;t supported by solely the environment, as it has static scope in our implementations.&lt;br /&gt;
*Closures do not remember state as function are to be applied using the state that exists where they are applied.     &lt;br /&gt;
&lt;br /&gt;
==Mutable Data Structures==&lt;br /&gt;
Need two repositories:&lt;br /&gt;
*Environment &lt;br /&gt;
**protects static scope &lt;br /&gt;
**maps identifier to location&lt;br /&gt;
**can get back to a previous location&lt;br /&gt;
**Identifier -&amp;gt; Value&lt;br /&gt;
*Store&lt;br /&gt;
**tracks dynamic changes&lt;br /&gt;
**maps location to a value&lt;br /&gt;
**changes are permanent&lt;br /&gt;
**Location -&amp;gt; Value&lt;br /&gt;
*Variables&lt;br /&gt;
**Identifier -&amp;gt; Location&lt;br /&gt;
**Location -&amp;gt; Value&lt;br /&gt;
&lt;br /&gt;
==Meta vs Syntactic Interpreters==&lt;br /&gt;
&#039;&#039;&#039;Syntactic Interpreter&#039;&#039;&#039; - An interpreter that uses the interpreting language to represent only terms of the interpreted language, implementing all the corresponding behavior explicitly&lt;br /&gt;
*doesn&#039;t use Scheme&#039;s implementation of things (like numbers)&lt;br /&gt;
*doesn&#039;t matter how well the interpreting and interpreted languages correspond&lt;br /&gt;
&#039;&#039;&#039;Meta Interpreter&#039;&#039;&#039; - An interpreter that uses language features of the interpreting language to directly implement behaviour of the interpreted language&lt;br /&gt;
*easy to write when there is a strong match between interpreted and interpreting language&lt;br /&gt;
*uses Scheme&#039;s implementation of things (closures, procedure applications, numbers, etc)&lt;br /&gt;
&#039;&#039;&#039;Meta-Circular Interpreter&#039;&#039;&#039; - A meta interpreter in which the interpreting and interpreted language are the same&lt;br /&gt;
*Only uses Scheme implementation of things&lt;br /&gt;
&#039;&#039;from PLAI, p.107-109&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Continuations==&lt;br /&gt;
&#039;&#039;&#039;What are they?&#039;&#039;&#039;&lt;br /&gt;
*stack is represented procedurally&lt;br /&gt;
*remember only the result and what is left to do (passes the state along)&lt;br /&gt;
*roughly the same idea as tail recursion&lt;br /&gt;
&#039;&#039;&#039;Why?&lt;br /&gt;
*Saves on memory and computation (ex. tail recursion summation vs augmenting recursion summation)&lt;br /&gt;
*A way of recording state when the processing computer is not always available (ex. web-servers)&lt;br /&gt;
&#039;&#039;&#039;Why Not?&lt;br /&gt;
*get rid of machine optimizations by forcing the structure of stack into continuation&lt;br /&gt;
*memory wastage if language needlessly creates stack frames&lt;br /&gt;
*need access to the source of entire program otherwise CPS translator may fail&lt;br /&gt;
*security: what if someone outside knew how to read the stack procedure?&lt;br /&gt;
&#039;&#039;&#039;Conventions&lt;br /&gt;
*Continuation methods usually end in /k&lt;br /&gt;
&lt;br /&gt;
==Variables==&lt;br /&gt;
&#039;&#039;&#039;Call-by-value&#039;&#039;&#039;&lt;br /&gt;
*evaluated argument is held in a new location&lt;br /&gt;
*changes to the content of that location in the store don&#039;t affect the actual parameter&lt;br /&gt;
&#039;&#039;&#039;Call-by-reference&#039;&#039;&#039;&lt;br /&gt;
*pass a reference to the actual argument, not the value&lt;br /&gt;
*updates to the reference within the called procedure will become visible to the calling context&lt;br /&gt;
*cheaper to use (no additional allocation) but introduce problems &lt;br /&gt;
*to implement:&lt;br /&gt;
**create a closure and give it the location of the actual argument&lt;br /&gt;
**uses l-value: env lookup without store lookup&lt;br /&gt;
**any mutations to the formal parameter are now changes to the same location as the actual parameter&lt;br /&gt;
&lt;br /&gt;
==Stateful vs Stateless==&lt;br /&gt;
&#039;&#039;&#039;Stateful&#039;&#039;&#039;&lt;br /&gt;
*server maintains state information&lt;br /&gt;
*easier to program&lt;br /&gt;
**don&#039;t need setup and breakdown of state at each interaction&lt;br /&gt;
*ex: FTP&lt;br /&gt;
**interp of each command is relative to history of past commands&lt;br /&gt;
&#039;&#039;&#039;Stateless&#039;&#039;&#039;&lt;br /&gt;
*does not retain record of prior communication&lt;br /&gt;
*Web application must completely restore state of the computation for each interaction&lt;br /&gt;
*server can handle higher loads&lt;br /&gt;
*server can ignore clients who don&#039;t appear to be active&lt;br /&gt;
*must transmit enough data to resume computation&lt;br /&gt;
&lt;br /&gt;
==Web Programs==&lt;br /&gt;
&#039;&#039;&#039;Receiver&#039;&#039;&#039; - a procedure of one argument representing the pending computation&lt;br /&gt;
*any computation not mentioned in the receiver never gets performed because of the program&#039;s termination after each iteration&lt;br /&gt;
&#039;&#039;&#039;Lifting&#039;&#039;&#039; - make nested procedures into top-level procedures&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Make a program Web-Ready&#039;&#039;&#039;&lt;br /&gt;
* 1. Generate receivers that capture pending computations&lt;br /&gt;
* 2. Pass values to receivers instead of returning them&lt;br /&gt;
&#039;&#039;&#039;Implications&#039;&#039;&#039;&lt;br /&gt;
* 1. Order of evaluation&lt;br /&gt;
* 2. Transformation is global&lt;br /&gt;
**all procedures in program must consume an extra receiver&lt;br /&gt;
* 3. Sequentializes the program&lt;br /&gt;
&lt;br /&gt;
==Map Example==&lt;br /&gt;
 (define (map f l)&lt;br /&gt;
   (if (empty? l)&lt;br /&gt;
      empty&lt;br /&gt;
      (cons (f (first l)) &lt;br /&gt;
               (map f (rest l)))))&lt;br /&gt;
 &lt;br /&gt;
 becomes:&lt;br /&gt;
 (define (map/k f/k list k)&lt;br /&gt;
   (if (empty? list)&lt;br /&gt;
       (k empty)&lt;br /&gt;
       (f/k (first list)&lt;br /&gt;
            (lambda (f-result)&lt;br /&gt;
              (map/k f/k (rest list)&lt;br /&gt;
                     (lambda (r-result)&lt;br /&gt;
                       (k (cons f-result r-result))))))))&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1&amp;diff=66116</id>
		<title>Course:CPSC311/2010WT1</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1&amp;diff=66116"/>
		<updated>2010-12-08T06:24:59Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== CPSC 311 2010W1: Definition of Programming Languages ==&lt;br /&gt;
&lt;br /&gt;
Programming languages is a challenging course, littered with deep ideas linked in fascinating ways and occasionally shrouded in strange terminology.  Therefore, you have this wiki to help each other (and the teaching staff!) keep up with course issues. &lt;br /&gt;
The course wiki is available for anyone to contribute to the class&#039;s learning.  It&#039;s also a great way to earn some bonus points.&lt;br /&gt;
&lt;br /&gt;
Feel free to add useful possible content to this list:&lt;br /&gt;
&lt;br /&gt;
* Exam Appendices&lt;br /&gt;
** [[Course:CPSC311/2010WT1/Midterm Exam 1 Appendix | Midterm #1 Appendix]]: fill it in, we&#039;ll print it for the exam!&lt;br /&gt;
** [[Course:CPSC311/2010WT1/Midterm Exam 2 Appendix | Midterm #2 Appendix]]: fill it in, we&#039;ll print it for the exam!&lt;br /&gt;
** [[Course:CPSC311/2010WT1/Final Exam Appendix | Final Appendix]]: fill it in, we&#039;ll print it for the exam!&lt;br /&gt;
* A repository of people&#039;s [[Course:CPSC311/2010WT1/Lecture Notes | Lecture Notes]].  Post yours in case you miss class and want to catch up!&lt;br /&gt;
* A collection of [[Course:CPSC311/2010WT1/Handy Links | Handy Links]] to help you find course-related info.&lt;br /&gt;
* Our very own [[Course:CPSC311/2010WT1/Glossary | Glossary]] of the many terms we&#039;ll run into.&lt;br /&gt;
* [[Course:CPSC311/2010WT1/Assignment 1 Gotchas | Assignment #1 Gotchas]], pesky things that might trip you up on rudimentary interpreters&lt;br /&gt;
* [[Course:CPSC311/2010WT1/Assignment 2 Gotchas | Assignment #2 Gotchas]], pesky things that might trip you up on extended interpreters&lt;br /&gt;
* [[Course:CPSC311/2010WT1/Assignment 3 Gotchas | Assignment #3 Gotchas]], pesky things that might trip you up as you get lazy&lt;br /&gt;
&lt;br /&gt;
== Handy List of All Our Pages ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;dpl&amp;gt;&lt;br /&gt;
titlematch={{PAGENAME}}/%&lt;br /&gt;
namespace={{NAMESPACE}}&lt;br /&gt;
replaceintitle=$CPSC311/2010WT1/$,&lt;br /&gt;
shownamespace=false&lt;br /&gt;
&amp;lt;/dpl&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:CPSC]]&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Midterm_Exam_2_Appendix&amp;diff=59239</id>
		<title>Course:CPSC311/2010WT1/Midterm Exam 2 Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Midterm_Exam_2_Appendix&amp;diff=59239"/>
		<updated>2010-11-02T22:52:36Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Midterm 2 Appendix (Wiki, Student-Generated; credits at end) =&lt;br /&gt;
&lt;br /&gt;
= Midterm 1 Material (copy of old appendix) =&lt;br /&gt;
&lt;br /&gt;
== Definitions == &lt;br /&gt;
&#039;&#039;&#039;Abstract syntax&#039;&#039;&#039; - idealized syntax, designed for convenient use by the underlying interpreter/compiler; result of parsing a concrete syntax expression &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Concrete syntax&#039;&#039;&#039; - syntax written by the user; consumed by the parser &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Closure&#039;&#039;&#039; - a wrapper that contains an expression (function/ binding) and its environment; expression saved until it needs to be used &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;CPS&#039;&#039;&#039; stands for Continuation-Passing Style (a.k.a. Tail Call Elimination)&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Static vs. Dynamic Scoping ==&lt;br /&gt;
&#039;&#039;&#039;Static Scope:&#039;&#039;&#039; In a language with static scope, the scope of an identifier&#039;s binding is a syntactically delimited region.  A typical region would be the body of a function or other binding construct.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Dynamic Scope:&#039;&#039;&#039; In a language with dynamic scope, the scope of an identifier&#039;s binding is the entire remainder of the execution during which that binding is in effect. That is, in a language with dynamic scope, if a function g binds identifier n and then invokes f, then f can refer to n—and so can every other function invoked by g until it completes its execution—even though f has no locally visible binding for n.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;from PLAI, p. 36&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== Closures and First-Class vs. First-Order Functions ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-order Functions&#039;&#039;&#039; are not values in the language. They can only be defined in a designated portion of&lt;br /&gt;
the program, where they must be given names for use in the remainder of the program.  (For a language like C, the &amp;quot;designated portion&amp;quot; is anywhere in the top-level of a source file, but not, for example, within the body of another function.)  The functions&lt;br /&gt;
in F1WAE are of this nature, which explains the 1 in the name of the language.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Higher-order Functions&#039;&#039;&#039; can return other functions as values and take other functions as parameters.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-class Functions&#039;&#039;&#039; are values with all the rights of other values. In particular, they can be supplied as the&lt;br /&gt;
value of arguments to functions, returned by functions as answers, constructed as values by expressions, and stored in data structures.&lt;br /&gt;
&lt;br /&gt;
== Deferred Substitution ==&lt;br /&gt;
&#039;&#039;&#039;Deferred Substitution&#039;&#039;&#039; Initially, we have no substitutions to perform, so the repository&lt;br /&gt;
is empty. Every time we encounter a substitution (in the form of a with or application), we augment the&lt;br /&gt;
repository with one more entry, recording the identiﬁer’s name and the value (if eager) or expression (if&lt;br /&gt;
lazy) it should eventually be substituted with. We continue to evaluate without actually performing the&lt;br /&gt;
substitution. &lt;br /&gt;
&lt;br /&gt;
A side-effect of deferred substitution is dynamic scoping, which we were able to fix by using closures to store the environment from which the function was created in.&lt;br /&gt;
&lt;br /&gt;
== Eager vs. Lazy Evaluation ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lazy With&#039;&#039;&#039;- we reduce the named expression to a value only when we need to &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Lazy Functions&#039;&#039;&#039;- we do not reduce the argument to a value until necessary&amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Strictness Points&#039;&#039;&#039;- the points where the implementation of a lazy language forces an expression to reduce to a value (example: in a function application, need to evaluate exactly what function to invoke, can&#039;t just leave it as a function closure) &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Eager&#039;&#039;&#039;- the named expression is reduced to a value before being substituting it. &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Backus-Naur Form (BNF) ==&lt;br /&gt;
&lt;br /&gt;
A concise specification language for expressing (among other things) the concrete syntax of a programming language.  A BNF often strongly suggests a corresponding abstract syntax.&lt;br /&gt;
&lt;br /&gt;
Here&#039;s an example BNF for a language with arithmetic expressions, with expressions, first-class functions and applications, a conditional statement, and explicit language support for recursion.  This particular language allows an arbitrary number of parameters to a function in its definition or arguments in a function application.&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;CFWAE&amp;gt; ::= &amp;lt;num&amp;gt;&lt;br /&gt;
    | {+ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {- &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {* &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {/ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | &amp;lt;id&amp;gt;&lt;br /&gt;
    | {if0 &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {with {&amp;lt;id&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {fun {&amp;lt;id&amp;gt; ...} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {&amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; ...}&lt;br /&gt;
    | {rec {&amp;lt;symbol&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
&lt;br /&gt;
== Racket syntax, helpful procedures, and thoughts ==&lt;br /&gt;
&lt;br /&gt;
 (cond&lt;br /&gt;
      [(number? v) ...]&lt;br /&gt;
      [(boolean? v) ...]&lt;br /&gt;
      [(string? v) ...]&lt;br /&gt;
      [else false])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(match data&lt;br /&gt;
   [pattern  action]&lt;br /&gt;
      [(? number?) (foo)]                    ;; check if data is number &lt;br /&gt;
      [(list a b c) (foo a b c) ]            ;; back-reference of list elements&lt;br /&gt;
      [((and n (? number?))) (foo n)]        ;; check if data is number and back-reference it as n&lt;br /&gt;
      [else (foo)]&lt;br /&gt;
)&lt;br /&gt;
;; Keep in mind that in pattern matching&lt;br /&gt;
;; &#039;...&#039; or &#039;___&#039; means 0 or more &lt;br /&gt;
;; &#039;..n&#039; or &#039;__n&amp;quot; means n or more &lt;br /&gt;
;; &#039;_&#039; matches anything&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;begin:&#039;&#039;&#039; Evaluates all expressions passed as arguments to begin, in order, and the results are all ignored except for the last one.&lt;br /&gt;
 &amp;gt; (begin (+ 5 2) (printf &amp;quot;Hello World!\n&amp;quot;) (+ 3 1))&lt;br /&gt;
 Hello World!&lt;br /&gt;
 4&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Variable definition:&#039;&#039;&#039;&lt;br /&gt;
 (define x 3)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Function definition:&#039;&#039;&#039;&lt;br /&gt;
 (define (funcName arg)&lt;br /&gt;
     (+ 1 arg))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Environment:&#039;&#039;&#039; - repository of deferred substitutions&lt;br /&gt;
 (define-type Env&lt;br /&gt;
      [mtEnv]&lt;br /&gt;
      [anEnv (name symbol?)(value CFWAE-Value?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Expression Closures:&#039;&#039;&#039;&lt;br /&gt;
 (define-type CFWAE-Value&lt;br /&gt;
      [numV (num number?)]&lt;br /&gt;
      [exprV (body CFWAE?)(env Env?)] ;; used to hold an unevaluated expression and its environment (used in lazy interp)&lt;br /&gt;
      [closureV (param symbol?)(body CFWAE?)(env Env?)]) ;;used to close over the existing environment to ensure static scope&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;map&#039;&#039;&#039; - performs a procedure on all elements of the list in order&lt;br /&gt;
 map &amp;lt;procedure&amp;gt; &amp;lt;list&amp;gt;&lt;br /&gt;
 (define (parseList list)&lt;br /&gt;
 (map parse list))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Type-case&#039;&#039;&#039;&lt;br /&gt;
 (type-case WAE expr&lt;br /&gt;
             [num (n) expr]&lt;br /&gt;
             ...)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define-type&#039;&#039;&#039; (From assignment 1)&lt;br /&gt;
 (define-type WAE&lt;br /&gt;
  [num (n number?)]&lt;br /&gt;
  [binop (op procedure?) (lhs WAE?) (rhs WAE?)]&lt;br /&gt;
  [with (b Binding?) (body WAE?)]&lt;br /&gt;
  [with* (lob (listof Binding?)) (body WAE?)]&lt;br /&gt;
  [id (name symbol?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Recursion interp&#039;&#039;&#039; (From lecture)                                            &lt;br /&gt;
 (define (interp exp env)                                                      &lt;br /&gt;
  (type-case CFAE exp                                                         &lt;br /&gt;
      ...&lt;br /&gt;
     (id  (name) (lookup name env))&lt;br /&gt;
     (fun (arg-name body) (closureV arg-name body env))&lt;br /&gt;
     (if0 (tst thn els) &lt;br /&gt;
          (type-case CFAE-value (interp tst env) &lt;br /&gt;
             (numV (n) (if (zero? n) (interp thn env) (interp els env)))&lt;br /&gt;
             (else (error &amp;quot;non numeric if0 test value&amp;quot;))))&lt;br /&gt;
     (app (fun-exp arg-exp)&lt;br /&gt;
          (let ((the-fun (interp fun-exp env))&lt;br /&gt;
                (the-arg (interp arg-exp env)))&lt;br /&gt;
            (type-case CFAE-value the-fun&lt;br /&gt;
              (closureV (arg-name body closure-env)&lt;br /&gt;
                        (interp body (anEnv arg-name the-arg&lt;br /&gt;
                                            closure-env)))&lt;br /&gt;
              (else (error &amp;quot;You can only apply closures!&amp;quot;)))))&lt;br /&gt;
     (rec (bound-id named-exp body)&lt;br /&gt;
       (local ([define new-env (anEnv bound-id &lt;br /&gt;
                                      (numV 0)&lt;br /&gt;
                                      env)]&lt;br /&gt;
               [define named-value (interp named-exp new-env)])&lt;br /&gt;
         (begin&lt;br /&gt;
           (set-anEnv-val! new-env named-value)  ;; Patches (numV 0) out for the correct value&lt;br /&gt;
           (interp body new-env))))&lt;br /&gt;
       &lt;br /&gt;
     &lt;br /&gt;
     ))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Strict code&#039;&#039;&#039; (from lecture)&lt;br /&gt;
 (define (strict val)&lt;br /&gt;
  (if (expV? val)&lt;br /&gt;
      (strict (interp (expV-exp val) (expV-env val)))&lt;br /&gt;
      val))&lt;br /&gt;
= Midterm 2 Material =&lt;br /&gt;
&lt;br /&gt;
==State==&lt;br /&gt;
*State is &amp;quot;threaded through&amp;quot; a program in the sense that the state that results from evaluating a given function is the state in which the next sequential function is to be evaluated in.&lt;br /&gt;
*State is inherently dynamic and so isn&#039;t supported by solely the environment, as it has static scope in our implementations.&lt;br /&gt;
*Closures do not remember state as function are to be applied using the state that exists where they are applied.     &lt;br /&gt;
&lt;br /&gt;
==Mutable Data Structures==&lt;br /&gt;
Need two repositories:&lt;br /&gt;
*Environment &lt;br /&gt;
**protects static scope &lt;br /&gt;
**maps identifier to location&lt;br /&gt;
**can get back to a previous location&lt;br /&gt;
**Identifier -&amp;gt; Value&lt;br /&gt;
*Store&lt;br /&gt;
**tracks dynamic changes&lt;br /&gt;
**maps location to a value&lt;br /&gt;
**changes are permanent&lt;br /&gt;
**Location -&amp;gt; Value&lt;br /&gt;
*Variables&lt;br /&gt;
**Identifier -&amp;gt; Location&lt;br /&gt;
**Location -&amp;gt; Value&lt;br /&gt;
&lt;br /&gt;
==Meta vs Syntactic Interpreters==&lt;br /&gt;
&#039;&#039;&#039;Syntactic Interpreter&#039;&#039;&#039; - An interpreter that uses the interpreting language to represent only terms of the interpreted language, implementing all the corresponding behavior explicitly&lt;br /&gt;
*doesn&#039;t use Scheme&#039;s implementation of things (like numbers)&lt;br /&gt;
*doesn&#039;t matter how well the interpreting and interpreted languages correspond&lt;br /&gt;
&#039;&#039;&#039;Meta Interpreter&#039;&#039;&#039; - An interpreter that uses language features of the interpreting language to directly implement behaviour of the interpreted language&lt;br /&gt;
*easy to write when there is a strong match between interpreted and interpreting language&lt;br /&gt;
*uses Scheme&#039;s implementation of things (closures, procedure applications, numbers, etc)&lt;br /&gt;
&#039;&#039;&#039;Meta-Circular Interpreter&#039;&#039;&#039; - A meta interpreter in which the interpreting and interpreted language are the same&lt;br /&gt;
*Only uses Scheme implementation of things&lt;br /&gt;
&#039;&#039;from PLAI, p.107-109&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Continuations==&lt;br /&gt;
&#039;&#039;&#039;What are they?&#039;&#039;&#039;&lt;br /&gt;
*stack is represented procedurally&lt;br /&gt;
*remember only the result and what is left to do (passes the state along)&lt;br /&gt;
*roughly the same idea as tail recursion&lt;br /&gt;
&#039;&#039;&#039;Why?&lt;br /&gt;
*Saves on memory and computation (ex. tail recursion summation vs augmenting recursion summation)&lt;br /&gt;
*A way of recording state when the processing computer is not always available (ex. web-servers)&lt;br /&gt;
&#039;&#039;&#039;Why Not?&lt;br /&gt;
*get rid of machine optimizations by forcing the structure of stack into continuation&lt;br /&gt;
*memory wastage if language needlessly creates stack frames&lt;br /&gt;
*need access to the source of entire program otherwise CPS translator may fail&lt;br /&gt;
*security: what if someone outside knew how to read the stack procedure?&lt;br /&gt;
&#039;&#039;&#039;Conventions&lt;br /&gt;
*Continuation methods usually end in /k&lt;br /&gt;
&lt;br /&gt;
==Variables==&lt;br /&gt;
&#039;&#039;&#039;Call-by-value&#039;&#039;&#039;&lt;br /&gt;
*evaluated argument is held in a new location&lt;br /&gt;
*changes to the content of that location in the store don&#039;t affect the actual parameter&lt;br /&gt;
&#039;&#039;&#039;Call-by-reference&#039;&#039;&#039;&lt;br /&gt;
*pass a reference to the actual argument, not the value&lt;br /&gt;
*updates to the reference within the called procedure will become visible to the calling context&lt;br /&gt;
*cheaper to use (no additional allocation) but introduce problems &lt;br /&gt;
*to implement:&lt;br /&gt;
**create a closure and give it the location of the actual argument&lt;br /&gt;
**uses l-value: env lookup without store lookup&lt;br /&gt;
**any mutations to the formal parameter are now changes to the same location as the actual parameter&lt;br /&gt;
&lt;br /&gt;
==Stateful vs Stateless==&lt;br /&gt;
&#039;&#039;&#039;Stateful&#039;&#039;&#039;&lt;br /&gt;
*server maintains state information&lt;br /&gt;
*easier to program&lt;br /&gt;
**don&#039;t need setup and breakdown of state at each interaction&lt;br /&gt;
*ex: FTP&lt;br /&gt;
**interp of each command is relative to history of past commands&lt;br /&gt;
&#039;&#039;&#039;Stateless&#039;&#039;&#039;&lt;br /&gt;
*does not retain record of prior communication&lt;br /&gt;
*Web application must completely restore state of the computation for each interaction&lt;br /&gt;
*server can handle higher loads&lt;br /&gt;
*server can ignore clients who don&#039;t appear to be active&lt;br /&gt;
*must transmit enough data to resume computation&lt;br /&gt;
&lt;br /&gt;
==Web Programs==&lt;br /&gt;
&#039;&#039;&#039;Receiver&#039;&#039;&#039; - a procedure of one argument representing the pending computation&lt;br /&gt;
*any computation not mentioned in the receiver never gets performed because of the program&#039;s termination after each iteration&lt;br /&gt;
&#039;&#039;&#039;Lifting&#039;&#039;&#039; - make nested procedures into top-level procedures&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Make a program Web-Ready&#039;&#039;&#039;&lt;br /&gt;
* 1. Generate receivers that capture pending computations&lt;br /&gt;
* 2. Pass values to receivers instead of returning them&lt;br /&gt;
&#039;&#039;&#039;Implications&#039;&#039;&#039;&lt;br /&gt;
* 1. Order of evaluation&lt;br /&gt;
* 2. Transformation is global&lt;br /&gt;
**all procedures in program must consume an extra receiver&lt;br /&gt;
* 3. Sequentializes the program&lt;br /&gt;
&lt;br /&gt;
==Map Example==&lt;br /&gt;
 (define (map f l)&lt;br /&gt;
   (if (empty? l)&lt;br /&gt;
      empty&lt;br /&gt;
      (cons (f (first l)) &lt;br /&gt;
               (map f (rest l)))))&lt;br /&gt;
 &lt;br /&gt;
 becomes:&lt;br /&gt;
 (define (map/k f/k list k)&lt;br /&gt;
   (if (empty? list)&lt;br /&gt;
       (k empty)&lt;br /&gt;
       (f/k (first list)&lt;br /&gt;
            (lambda (f-result)&lt;br /&gt;
              (map/k f/k (rest list)&lt;br /&gt;
                     (lambda (r-result)&lt;br /&gt;
                       (k (cons f-result r-result))))))))&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Midterm_Exam_2_Appendix&amp;diff=59236</id>
		<title>Course:CPSC311/2010WT1/Midterm Exam 2 Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Midterm_Exam_2_Appendix&amp;diff=59236"/>
		<updated>2010-11-02T22:51:38Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Midterm 1 Material (copy of old appendix) =&lt;br /&gt;
&lt;br /&gt;
== Definitions == &lt;br /&gt;
&#039;&#039;&#039;Abstract syntax&#039;&#039;&#039; - idealized syntax, designed for convenient use by the underlying interpreter/compiler; result of parsing a concrete syntax expression &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Concrete syntax&#039;&#039;&#039; - syntax written by the user; consumed by the parser &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Closure&#039;&#039;&#039; - a wrapper that contains an expression (function/ binding) and its environment; expression saved until it needs to be used &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;CPS&#039;&#039;&#039; stands for Continuation-Passing Style (a.k.a. Tail Call Elimination)&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Static vs. Dynamic Scoping ==&lt;br /&gt;
&#039;&#039;&#039;Static Scope:&#039;&#039;&#039; In a language with static scope, the scope of an identifier&#039;s binding is a syntactically delimited region.  A typical region would be the body of a function or other binding construct.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Dynamic Scope:&#039;&#039;&#039; In a language with dynamic scope, the scope of an identifier&#039;s binding is the entire remainder of the execution during which that binding is in effect. That is, in a language with dynamic scope, if a function g binds identifier n and then invokes f, then f can refer to n—and so can every other function invoked by g until it completes its execution—even though f has no locally visible binding for n.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;from PLAI, p. 36&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== Closures and First-Class vs. First-Order Functions ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-order Functions&#039;&#039;&#039; are not values in the language. They can only be defined in a designated portion of&lt;br /&gt;
the program, where they must be given names for use in the remainder of the program.  (For a language like C, the &amp;quot;designated portion&amp;quot; is anywhere in the top-level of a source file, but not, for example, within the body of another function.)  The functions&lt;br /&gt;
in F1WAE are of this nature, which explains the 1 in the name of the language.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Higher-order Functions&#039;&#039;&#039; can return other functions as values and take other functions as parameters.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-class Functions&#039;&#039;&#039; are values with all the rights of other values. In particular, they can be supplied as the&lt;br /&gt;
value of arguments to functions, returned by functions as answers, constructed as values by expressions, and stored in data structures.&lt;br /&gt;
&lt;br /&gt;
== Deferred Substitution ==&lt;br /&gt;
&#039;&#039;&#039;Deferred Substitution&#039;&#039;&#039; Initially, we have no substitutions to perform, so the repository&lt;br /&gt;
is empty. Every time we encounter a substitution (in the form of a with or application), we augment the&lt;br /&gt;
repository with one more entry, recording the identiﬁer’s name and the value (if eager) or expression (if&lt;br /&gt;
lazy) it should eventually be substituted with. We continue to evaluate without actually performing the&lt;br /&gt;
substitution. &lt;br /&gt;
&lt;br /&gt;
A side-effect of deferred substitution is dynamic scoping, which we were able to fix by using closures to store the environment from which the function was created in.&lt;br /&gt;
&lt;br /&gt;
== Eager vs. Lazy Evaluation ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lazy With&#039;&#039;&#039;- we reduce the named expression to a value only when we need to &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Lazy Functions&#039;&#039;&#039;- we do not reduce the argument to a value until necessary&amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Strictness Points&#039;&#039;&#039;- the points where the implementation of a lazy language forces an expression to reduce to a value (example: in a function application, need to evaluate exactly what function to invoke, can&#039;t just leave it as a function closure) &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Eager&#039;&#039;&#039;- the named expression is reduced to a value before being substituting it. &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Backus-Naur Form (BNF) ==&lt;br /&gt;
&lt;br /&gt;
A concise specification language for expressing (among other things) the concrete syntax of a programming language.  A BNF often strongly suggests a corresponding abstract syntax.&lt;br /&gt;
&lt;br /&gt;
Here&#039;s an example BNF for a language with arithmetic expressions, with expressions, first-class functions and applications, a conditional statement, and explicit language support for recursion.  This particular language allows an arbitrary number of parameters to a function in its definition or arguments in a function application.&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;CFWAE&amp;gt; ::= &amp;lt;num&amp;gt;&lt;br /&gt;
    | {+ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {- &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {* &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {/ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | &amp;lt;id&amp;gt;&lt;br /&gt;
    | {if0 &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {with {&amp;lt;id&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {fun {&amp;lt;id&amp;gt; ...} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {&amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; ...}&lt;br /&gt;
    | {rec {&amp;lt;symbol&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
&lt;br /&gt;
== Racket syntax, helpful procedures, and thoughts ==&lt;br /&gt;
&lt;br /&gt;
 (cond&lt;br /&gt;
      [(number? v) ...]&lt;br /&gt;
      [(boolean? v) ...]&lt;br /&gt;
      [(string? v) ...]&lt;br /&gt;
      [else false])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(match data&lt;br /&gt;
   [pattern  action]&lt;br /&gt;
      [(? number?) (foo)]                    ;; check if data is number &lt;br /&gt;
      [(list a b c) (foo a b c) ]            ;; back-reference of list elements&lt;br /&gt;
      [((and n (? number?))) (foo n)]        ;; check if data is number and back-reference it as n&lt;br /&gt;
      [else (foo)]&lt;br /&gt;
)&lt;br /&gt;
;; Keep in mind that in pattern matching&lt;br /&gt;
;; &#039;...&#039; or &#039;___&#039; means 0 or more &lt;br /&gt;
;; &#039;..n&#039; or &#039;__n&amp;quot; means n or more &lt;br /&gt;
;; &#039;_&#039; matches anything&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;begin:&#039;&#039;&#039; Evaluates all expressions passed as arguments to begin, in order, and the results are all ignored except for the last one.&lt;br /&gt;
 &amp;gt; (begin (+ 5 2) (printf &amp;quot;Hello World!\n&amp;quot;) (+ 3 1))&lt;br /&gt;
 Hello World!&lt;br /&gt;
 4&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Variable definition:&#039;&#039;&#039;&lt;br /&gt;
 (define x 3)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Function definition:&#039;&#039;&#039;&lt;br /&gt;
 (define (funcName arg)&lt;br /&gt;
     (+ 1 arg))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Environment:&#039;&#039;&#039; - repository of deferred substitutions&lt;br /&gt;
 (define-type Env&lt;br /&gt;
      [mtEnv]&lt;br /&gt;
      [anEnv (name symbol?)(value CFWAE-Value?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Expression Closures:&#039;&#039;&#039;&lt;br /&gt;
 (define-type CFWAE-Value&lt;br /&gt;
      [numV (num number?)]&lt;br /&gt;
      [exprV (body CFWAE?)(env Env?)] ;; used to hold an unevaluated expression and its environment (used in lazy interp)&lt;br /&gt;
      [closureV (param symbol?)(body CFWAE?)(env Env?)]) ;;used to close over the existing environment to ensure static scope&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;map&#039;&#039;&#039; - performs a procedure on all elements of the list in order&lt;br /&gt;
 map &amp;lt;procedure&amp;gt; &amp;lt;list&amp;gt;&lt;br /&gt;
 (define (parseList list)&lt;br /&gt;
 (map parse list))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Type-case&#039;&#039;&#039;&lt;br /&gt;
 (type-case WAE expr&lt;br /&gt;
             [num (n) expr]&lt;br /&gt;
             ...)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define-type&#039;&#039;&#039; (From assignment 1)&lt;br /&gt;
 (define-type WAE&lt;br /&gt;
  [num (n number?)]&lt;br /&gt;
  [binop (op procedure?) (lhs WAE?) (rhs WAE?)]&lt;br /&gt;
  [with (b Binding?) (body WAE?)]&lt;br /&gt;
  [with* (lob (listof Binding?)) (body WAE?)]&lt;br /&gt;
  [id (name symbol?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Recursion interp&#039;&#039;&#039; (From lecture)                                            &lt;br /&gt;
 (define (interp exp env)                                                      &lt;br /&gt;
  (type-case CFAE exp                                                         &lt;br /&gt;
      ...&lt;br /&gt;
     (id  (name) (lookup name env))&lt;br /&gt;
     (fun (arg-name body) (closureV arg-name body env))&lt;br /&gt;
     (if0 (tst thn els) &lt;br /&gt;
          (type-case CFAE-value (interp tst env) &lt;br /&gt;
             (numV (n) (if (zero? n) (interp thn env) (interp els env)))&lt;br /&gt;
             (else (error &amp;quot;non numeric if0 test value&amp;quot;))))&lt;br /&gt;
     (app (fun-exp arg-exp)&lt;br /&gt;
          (let ((the-fun (interp fun-exp env))&lt;br /&gt;
                (the-arg (interp arg-exp env)))&lt;br /&gt;
            (type-case CFAE-value the-fun&lt;br /&gt;
              (closureV (arg-name body closure-env)&lt;br /&gt;
                        (interp body (anEnv arg-name the-arg&lt;br /&gt;
                                            closure-env)))&lt;br /&gt;
              (else (error &amp;quot;You can only apply closures!&amp;quot;)))))&lt;br /&gt;
     (rec (bound-id named-exp body)&lt;br /&gt;
       (local ([define new-env (anEnv bound-id &lt;br /&gt;
                                      (numV 0)&lt;br /&gt;
                                      env)]&lt;br /&gt;
               [define named-value (interp named-exp new-env)])&lt;br /&gt;
         (begin&lt;br /&gt;
           (set-anEnv-val! new-env named-value)  ;; Patches (numV 0) out for the correct value&lt;br /&gt;
           (interp body new-env))))&lt;br /&gt;
       &lt;br /&gt;
     &lt;br /&gt;
     ))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Strict code&#039;&#039;&#039; (from lecture)&lt;br /&gt;
 (define (strict val)&lt;br /&gt;
  (if (expV? val)&lt;br /&gt;
      (strict (interp (expV-exp val) (expV-env val)))&lt;br /&gt;
      val))&lt;br /&gt;
= Midterm 2 Material =&lt;br /&gt;
&lt;br /&gt;
==State==&lt;br /&gt;
*State is &amp;quot;threaded through&amp;quot; a program in the sense that the state that results from evaluating a given function is the state in which the next sequential function is to be evaluated in.&lt;br /&gt;
*State is inherently dynamic and so isn&#039;t supported by solely the environment, as it has static scope in our implementations.&lt;br /&gt;
*Closures do not remember state as function are to be applied using the state that exists where they are applied.     &lt;br /&gt;
&lt;br /&gt;
==Mutable Data Structures==&lt;br /&gt;
Need two repositories:&lt;br /&gt;
*Environment &lt;br /&gt;
**protects static scope &lt;br /&gt;
**maps identifier to location&lt;br /&gt;
**can get back to a previous location&lt;br /&gt;
**Identifier -&amp;gt; Value&lt;br /&gt;
*Store&lt;br /&gt;
**tracks dynamic changes&lt;br /&gt;
**maps location to a value&lt;br /&gt;
**changes are permanent&lt;br /&gt;
**Location -&amp;gt; Value&lt;br /&gt;
*Variables&lt;br /&gt;
**Identifier -&amp;gt; Location&lt;br /&gt;
**Location -&amp;gt; Value&lt;br /&gt;
&lt;br /&gt;
==Meta vs Syntactic Interpreters==&lt;br /&gt;
&#039;&#039;&#039;Syntactic Interpreter&#039;&#039;&#039; - An interpreter that uses the interpreting language to represent only terms of the interpreted language, implementing all the corresponding behavior explicitly&lt;br /&gt;
*doesn&#039;t use Scheme&#039;s implementation of things (like numbers)&lt;br /&gt;
*doesn&#039;t matter how well the interpreting and interpreted languages correspond&lt;br /&gt;
&#039;&#039;&#039;Meta Interpreter&#039;&#039;&#039; - An interpreter that uses language features of the interpreting language to directly implement behaviour of the interpreted language&lt;br /&gt;
*easy to write when there is a strong match between interpreted and interpreting language&lt;br /&gt;
*uses Scheme&#039;s implementation of things (closures, procedure applications, numbers, etc)&lt;br /&gt;
&#039;&#039;&#039;Meta-Circular Interpreter&#039;&#039;&#039; - A meta interpreter in which the interpreting and interpreted language are the same&lt;br /&gt;
*Only uses Scheme implementation of things&lt;br /&gt;
&#039;&#039;from PLAI, p.107-109&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Continuations==&lt;br /&gt;
&#039;&#039;&#039;What are they?&#039;&#039;&#039;&lt;br /&gt;
*stack is represented procedurally&lt;br /&gt;
*remember only the result and what is left to do (passes the state along)&lt;br /&gt;
*roughly the same idea as tail recursion&lt;br /&gt;
&#039;&#039;&#039;Why?&lt;br /&gt;
*Saves on memory and computation (ex. tail recursion summation vs augmenting recursion summation)&lt;br /&gt;
*A way of recording state when the processing computer is not always available (ex. web-servers)&lt;br /&gt;
&#039;&#039;&#039;Why Not?&lt;br /&gt;
*get rid of machine optimizations by forcing the structure of stack into continuation&lt;br /&gt;
*memory wastage if language needlessly creates stack frames&lt;br /&gt;
*need access to the source of entire program otherwise CPS translator may fail&lt;br /&gt;
*security: what if someone outside knew how to read the stack procedure?&lt;br /&gt;
&#039;&#039;&#039;Conventions&lt;br /&gt;
*Continuation methods usually end in /k&lt;br /&gt;
&lt;br /&gt;
==Variables==&lt;br /&gt;
&#039;&#039;&#039;Call-by-value&#039;&#039;&#039;&lt;br /&gt;
*evaluated argument is held in a new location&lt;br /&gt;
*changes to the content of that location in the store don&#039;t affect the actual parameter&lt;br /&gt;
&#039;&#039;&#039;Call-by-reference&#039;&#039;&#039;&lt;br /&gt;
*pass a reference to the actual argument, not the value&lt;br /&gt;
*updates to the reference within the called procedure will become visible to the calling context&lt;br /&gt;
*cheaper to use (no additional allocation) but introduce problems &lt;br /&gt;
*to implement:&lt;br /&gt;
**create a closure and give it the location of the actual argument&lt;br /&gt;
**uses l-value: env lookup without store lookup&lt;br /&gt;
**any mutations to the formal parameter are now changes to the same location as the actual parameter&lt;br /&gt;
&lt;br /&gt;
==Stateful vs Stateless==&lt;br /&gt;
&#039;&#039;&#039;Stateful&#039;&#039;&#039;&lt;br /&gt;
*server maintains state information&lt;br /&gt;
*easier to program&lt;br /&gt;
**don&#039;t need setup and breakdown of state at each interaction&lt;br /&gt;
*ex: FTP&lt;br /&gt;
**interp of each command is relative to history of past commands&lt;br /&gt;
&#039;&#039;&#039;Stateless&#039;&#039;&#039;&lt;br /&gt;
*does not retain record of prior communication&lt;br /&gt;
*Web application must completely restore state of the computation for each interaction&lt;br /&gt;
*server can handle higher loads&lt;br /&gt;
*server can ignore clients who don&#039;t appear to be active&lt;br /&gt;
*must transmit enough data to resume computation&lt;br /&gt;
&lt;br /&gt;
==Web Programs==&lt;br /&gt;
&#039;&#039;&#039;Receiver&#039;&#039;&#039; - a procedure of one argument representing the pending computation&lt;br /&gt;
*any computation not mentioned in the receiver never gets performed because of the program&#039;s termination after each iteration&lt;br /&gt;
&#039;&#039;&#039;Lifting&#039;&#039;&#039; - make nested procedures into top-level procedures&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Make a program Web-Ready&#039;&#039;&#039;&lt;br /&gt;
* 1. Generate receivers that capture pending computations&lt;br /&gt;
* 2. Pass values to receivers instead of returning them&lt;br /&gt;
&#039;&#039;&#039;Implications&#039;&#039;&#039;&lt;br /&gt;
* 1. Order of evaluation&lt;br /&gt;
* 2. Transformation is global&lt;br /&gt;
**all procedures in program must consume an extra receiver&lt;br /&gt;
* 3. Sequentializes the program&lt;br /&gt;
&lt;br /&gt;
==Map Example==&lt;br /&gt;
 (define (map f l)&lt;br /&gt;
   (if (empty? l)&lt;br /&gt;
      empty&lt;br /&gt;
      (cons (f (first l)) &lt;br /&gt;
               (map f (rest l)))))&lt;br /&gt;
 &lt;br /&gt;
 becomes:&lt;br /&gt;
 (define (map/k f/k list k)&lt;br /&gt;
   (if (empty? list)&lt;br /&gt;
       (k empty)&lt;br /&gt;
       (f/k (first list)&lt;br /&gt;
            (lambda (f-result)&lt;br /&gt;
              (map/k f/k (rest list)&lt;br /&gt;
                     (lambda (r-result)&lt;br /&gt;
                       (k (cons f-result r-result))))))))&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Midterm_Exam_2_Appendix&amp;diff=59235</id>
		<title>Course:CPSC311/2010WT1/Midterm Exam 2 Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Midterm_Exam_2_Appendix&amp;diff=59235"/>
		<updated>2010-11-02T22:51:26Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Midterm 2 Appendix (Wiki, Student-Generated) =&lt;br /&gt;
&lt;br /&gt;
= Midterm 1 Material (copy of old appendix) =&lt;br /&gt;
&lt;br /&gt;
== Definitions == &lt;br /&gt;
&#039;&#039;&#039;Abstract syntax&#039;&#039;&#039; - idealized syntax, designed for convenient use by the underlying interpreter/compiler; result of parsing a concrete syntax expression &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Concrete syntax&#039;&#039;&#039; - syntax written by the user; consumed by the parser &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Closure&#039;&#039;&#039; - a wrapper that contains an expression (function/ binding) and its environment; expression saved until it needs to be used &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;CPS&#039;&#039;&#039; stands for Continuation-Passing Style (a.k.a. Tail Call Elimination)&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Static vs. Dynamic Scoping ==&lt;br /&gt;
&#039;&#039;&#039;Static Scope:&#039;&#039;&#039; In a language with static scope, the scope of an identifier&#039;s binding is a syntactically delimited region.  A typical region would be the body of a function or other binding construct.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Dynamic Scope:&#039;&#039;&#039; In a language with dynamic scope, the scope of an identifier&#039;s binding is the entire remainder of the execution during which that binding is in effect. That is, in a language with dynamic scope, if a function g binds identifier n and then invokes f, then f can refer to n—and so can every other function invoked by g until it completes its execution—even though f has no locally visible binding for n.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;from PLAI, p. 36&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== Closures and First-Class vs. First-Order Functions ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-order Functions&#039;&#039;&#039; are not values in the language. They can only be defined in a designated portion of&lt;br /&gt;
the program, where they must be given names for use in the remainder of the program.  (For a language like C, the &amp;quot;designated portion&amp;quot; is anywhere in the top-level of a source file, but not, for example, within the body of another function.)  The functions&lt;br /&gt;
in F1WAE are of this nature, which explains the 1 in the name of the language.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Higher-order Functions&#039;&#039;&#039; can return other functions as values and take other functions as parameters.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-class Functions&#039;&#039;&#039; are values with all the rights of other values. In particular, they can be supplied as the&lt;br /&gt;
value of arguments to functions, returned by functions as answers, constructed as values by expressions, and stored in data structures.&lt;br /&gt;
&lt;br /&gt;
== Deferred Substitution ==&lt;br /&gt;
&#039;&#039;&#039;Deferred Substitution&#039;&#039;&#039; Initially, we have no substitutions to perform, so the repository&lt;br /&gt;
is empty. Every time we encounter a substitution (in the form of a with or application), we augment the&lt;br /&gt;
repository with one more entry, recording the identiﬁer’s name and the value (if eager) or expression (if&lt;br /&gt;
lazy) it should eventually be substituted with. We continue to evaluate without actually performing the&lt;br /&gt;
substitution. &lt;br /&gt;
&lt;br /&gt;
A side-effect of deferred substitution is dynamic scoping, which we were able to fix by using closures to store the environment from which the function was created in.&lt;br /&gt;
&lt;br /&gt;
== Eager vs. Lazy Evaluation ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lazy With&#039;&#039;&#039;- we reduce the named expression to a value only when we need to &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Lazy Functions&#039;&#039;&#039;- we do not reduce the argument to a value until necessary&amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Strictness Points&#039;&#039;&#039;- the points where the implementation of a lazy language forces an expression to reduce to a value (example: in a function application, need to evaluate exactly what function to invoke, can&#039;t just leave it as a function closure) &amp;lt;br /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Eager&#039;&#039;&#039;- the named expression is reduced to a value before being substituting it. &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Backus-Naur Form (BNF) ==&lt;br /&gt;
&lt;br /&gt;
A concise specification language for expressing (among other things) the concrete syntax of a programming language.  A BNF often strongly suggests a corresponding abstract syntax.&lt;br /&gt;
&lt;br /&gt;
Here&#039;s an example BNF for a language with arithmetic expressions, with expressions, first-class functions and applications, a conditional statement, and explicit language support for recursion.  This particular language allows an arbitrary number of parameters to a function in its definition or arguments in a function application.&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;CFWAE&amp;gt; ::= &amp;lt;num&amp;gt;&lt;br /&gt;
    | {+ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {- &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {* &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {/ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | &amp;lt;id&amp;gt;&lt;br /&gt;
    | {if0 &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {with {&amp;lt;id&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {fun {&amp;lt;id&amp;gt; ...} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {&amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; ...}&lt;br /&gt;
    | {rec {&amp;lt;symbol&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
&lt;br /&gt;
== Racket syntax, helpful procedures, and thoughts ==&lt;br /&gt;
&lt;br /&gt;
 (cond&lt;br /&gt;
      [(number? v) ...]&lt;br /&gt;
      [(boolean? v) ...]&lt;br /&gt;
      [(string? v) ...]&lt;br /&gt;
      [else false])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(match data&lt;br /&gt;
   [pattern  action]&lt;br /&gt;
      [(? number?) (foo)]                    ;; check if data is number &lt;br /&gt;
      [(list a b c) (foo a b c) ]            ;; back-reference of list elements&lt;br /&gt;
      [((and n (? number?))) (foo n)]        ;; check if data is number and back-reference it as n&lt;br /&gt;
      [else (foo)]&lt;br /&gt;
)&lt;br /&gt;
;; Keep in mind that in pattern matching&lt;br /&gt;
;; &#039;...&#039; or &#039;___&#039; means 0 or more &lt;br /&gt;
;; &#039;..n&#039; or &#039;__n&amp;quot; means n or more &lt;br /&gt;
;; &#039;_&#039; matches anything&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;begin:&#039;&#039;&#039; Evaluates all expressions passed as arguments to begin, in order, and the results are all ignored except for the last one.&lt;br /&gt;
 &amp;gt; (begin (+ 5 2) (printf &amp;quot;Hello World!\n&amp;quot;) (+ 3 1))&lt;br /&gt;
 Hello World!&lt;br /&gt;
 4&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Variable definition:&#039;&#039;&#039;&lt;br /&gt;
 (define x 3)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Function definition:&#039;&#039;&#039;&lt;br /&gt;
 (define (funcName arg)&lt;br /&gt;
     (+ 1 arg))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Environment:&#039;&#039;&#039; - repository of deferred substitutions&lt;br /&gt;
 (define-type Env&lt;br /&gt;
      [mtEnv]&lt;br /&gt;
      [anEnv (name symbol?)(value CFWAE-Value?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Expression Closures:&#039;&#039;&#039;&lt;br /&gt;
 (define-type CFWAE-Value&lt;br /&gt;
      [numV (num number?)]&lt;br /&gt;
      [exprV (body CFWAE?)(env Env?)] ;; used to hold an unevaluated expression and its environment (used in lazy interp)&lt;br /&gt;
      [closureV (param symbol?)(body CFWAE?)(env Env?)]) ;;used to close over the existing environment to ensure static scope&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;map&#039;&#039;&#039; - performs a procedure on all elements of the list in order&lt;br /&gt;
 map &amp;lt;procedure&amp;gt; &amp;lt;list&amp;gt;&lt;br /&gt;
 (define (parseList list)&lt;br /&gt;
 (map parse list))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Type-case&#039;&#039;&#039;&lt;br /&gt;
 (type-case WAE expr&lt;br /&gt;
             [num (n) expr]&lt;br /&gt;
             ...)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define-type&#039;&#039;&#039; (From assignment 1)&lt;br /&gt;
 (define-type WAE&lt;br /&gt;
  [num (n number?)]&lt;br /&gt;
  [binop (op procedure?) (lhs WAE?) (rhs WAE?)]&lt;br /&gt;
  [with (b Binding?) (body WAE?)]&lt;br /&gt;
  [with* (lob (listof Binding?)) (body WAE?)]&lt;br /&gt;
  [id (name symbol?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Recursion interp&#039;&#039;&#039; (From lecture)                                            &lt;br /&gt;
 (define (interp exp env)                                                      &lt;br /&gt;
  (type-case CFAE exp                                                         &lt;br /&gt;
      ...&lt;br /&gt;
     (id  (name) (lookup name env))&lt;br /&gt;
     (fun (arg-name body) (closureV arg-name body env))&lt;br /&gt;
     (if0 (tst thn els) &lt;br /&gt;
          (type-case CFAE-value (interp tst env) &lt;br /&gt;
             (numV (n) (if (zero? n) (interp thn env) (interp els env)))&lt;br /&gt;
             (else (error &amp;quot;non numeric if0 test value&amp;quot;))))&lt;br /&gt;
     (app (fun-exp arg-exp)&lt;br /&gt;
          (let ((the-fun (interp fun-exp env))&lt;br /&gt;
                (the-arg (interp arg-exp env)))&lt;br /&gt;
            (type-case CFAE-value the-fun&lt;br /&gt;
              (closureV (arg-name body closure-env)&lt;br /&gt;
                        (interp body (anEnv arg-name the-arg&lt;br /&gt;
                                            closure-env)))&lt;br /&gt;
              (else (error &amp;quot;You can only apply closures!&amp;quot;)))))&lt;br /&gt;
     (rec (bound-id named-exp body)&lt;br /&gt;
       (local ([define new-env (anEnv bound-id &lt;br /&gt;
                                      (numV 0)&lt;br /&gt;
                                      env)]&lt;br /&gt;
               [define named-value (interp named-exp new-env)])&lt;br /&gt;
         (begin&lt;br /&gt;
           (set-anEnv-val! new-env named-value)  ;; Patches (numV 0) out for the correct value&lt;br /&gt;
           (interp body new-env))))&lt;br /&gt;
       &lt;br /&gt;
     &lt;br /&gt;
     ))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Strict code&#039;&#039;&#039; (from lecture)&lt;br /&gt;
 (define (strict val)&lt;br /&gt;
  (if (expV? val)&lt;br /&gt;
      (strict (interp (expV-exp val) (expV-env val)))&lt;br /&gt;
      val))&lt;br /&gt;
= Midterm 2 Material =&lt;br /&gt;
&lt;br /&gt;
==State==&lt;br /&gt;
*State is &amp;quot;threaded through&amp;quot; a program in the sense that the state that results from evaluating a given function is the state in which the next sequential function is to be evaluated in.&lt;br /&gt;
*State is inherently dynamic and so isn&#039;t supported by solely the environment, as it has static scope in our implementations.&lt;br /&gt;
*Closures do not remember state as function are to be applied using the state that exists where they are applied.     &lt;br /&gt;
&lt;br /&gt;
==Mutable Data Structures==&lt;br /&gt;
Need two repositories:&lt;br /&gt;
*Environment &lt;br /&gt;
**protects static scope &lt;br /&gt;
**maps identifier to location&lt;br /&gt;
**can get back to a previous location&lt;br /&gt;
**Identifier -&amp;gt; Value&lt;br /&gt;
*Store&lt;br /&gt;
**tracks dynamic changes&lt;br /&gt;
**maps location to a value&lt;br /&gt;
**changes are permanent&lt;br /&gt;
**Location -&amp;gt; Value&lt;br /&gt;
*Variables&lt;br /&gt;
**Identifier -&amp;gt; Location&lt;br /&gt;
**Location -&amp;gt; Value&lt;br /&gt;
&lt;br /&gt;
==Meta vs Syntactic Interpreters==&lt;br /&gt;
&#039;&#039;&#039;Syntactic Interpreter&#039;&#039;&#039; - An interpreter that uses the interpreting language to represent only terms of the interpreted language, implementing all the corresponding behavior explicitly&lt;br /&gt;
*doesn&#039;t use Scheme&#039;s implementation of things (like numbers)&lt;br /&gt;
*doesn&#039;t matter how well the interpreting and interpreted languages correspond&lt;br /&gt;
&#039;&#039;&#039;Meta Interpreter&#039;&#039;&#039; - An interpreter that uses language features of the interpreting language to directly implement behaviour of the interpreted language&lt;br /&gt;
*easy to write when there is a strong match between interpreted and interpreting language&lt;br /&gt;
*uses Scheme&#039;s implementation of things (closures, procedure applications, numbers, etc)&lt;br /&gt;
&#039;&#039;&#039;Meta-Circular Interpreter&#039;&#039;&#039; - A meta interpreter in which the interpreting and interpreted language are the same&lt;br /&gt;
*Only uses Scheme implementation of things&lt;br /&gt;
&#039;&#039;from PLAI, p.107-109&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Continuations==&lt;br /&gt;
&#039;&#039;&#039;What are they?&#039;&#039;&#039;&lt;br /&gt;
*stack is represented procedurally&lt;br /&gt;
*remember only the result and what is left to do (passes the state along)&lt;br /&gt;
*roughly the same idea as tail recursion&lt;br /&gt;
&#039;&#039;&#039;Why?&lt;br /&gt;
*Saves on memory and computation (ex. tail recursion summation vs augmenting recursion summation)&lt;br /&gt;
*A way of recording state when the processing computer is not always available (ex. web-servers)&lt;br /&gt;
&#039;&#039;&#039;Why Not?&lt;br /&gt;
*get rid of machine optimizations by forcing the structure of stack into continuation&lt;br /&gt;
*memory wastage if language needlessly creates stack frames&lt;br /&gt;
*need access to the source of entire program otherwise CPS translator may fail&lt;br /&gt;
*security: what if someone outside knew how to read the stack procedure?&lt;br /&gt;
&#039;&#039;&#039;Conventions&lt;br /&gt;
*Continuation methods usually end in /k&lt;br /&gt;
&lt;br /&gt;
==Variables==&lt;br /&gt;
&#039;&#039;&#039;Call-by-value&#039;&#039;&#039;&lt;br /&gt;
*evaluated argument is held in a new location&lt;br /&gt;
*changes to the content of that location in the store don&#039;t affect the actual parameter&lt;br /&gt;
&#039;&#039;&#039;Call-by-reference&#039;&#039;&#039;&lt;br /&gt;
*pass a reference to the actual argument, not the value&lt;br /&gt;
*updates to the reference within the called procedure will become visible to the calling context&lt;br /&gt;
*cheaper to use (no additional allocation) but introduce problems &lt;br /&gt;
*to implement:&lt;br /&gt;
**create a closure and give it the location of the actual argument&lt;br /&gt;
**uses l-value: env lookup without store lookup&lt;br /&gt;
**any mutations to the formal parameter are now changes to the same location as the actual parameter&lt;br /&gt;
&lt;br /&gt;
==Stateful vs Stateless==&lt;br /&gt;
&#039;&#039;&#039;Stateful&#039;&#039;&#039;&lt;br /&gt;
*server maintains state information&lt;br /&gt;
*easier to program&lt;br /&gt;
**don&#039;t need setup and breakdown of state at each interaction&lt;br /&gt;
*ex: FTP&lt;br /&gt;
**interp of each command is relative to history of past commands&lt;br /&gt;
&#039;&#039;&#039;Stateless&#039;&#039;&#039;&lt;br /&gt;
*does not retain record of prior communication&lt;br /&gt;
*Web application must completely restore state of the computation for each interaction&lt;br /&gt;
*server can handle higher loads&lt;br /&gt;
*server can ignore clients who don&#039;t appear to be active&lt;br /&gt;
*must transmit enough data to resume computation&lt;br /&gt;
&lt;br /&gt;
==Web Programs==&lt;br /&gt;
&#039;&#039;&#039;Receiver&#039;&#039;&#039; - a procedure of one argument representing the pending computation&lt;br /&gt;
*any computation not mentioned in the receiver never gets performed because of the program&#039;s termination after each iteration&lt;br /&gt;
&#039;&#039;&#039;Lifting&#039;&#039;&#039; - make nested procedures into top-level procedures&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Make a program Web-Ready&#039;&#039;&#039;&lt;br /&gt;
* 1. Generate receivers that capture pending computations&lt;br /&gt;
* 2. Pass values to receivers instead of returning them&lt;br /&gt;
&#039;&#039;&#039;Implications&#039;&#039;&#039;&lt;br /&gt;
* 1. Order of evaluation&lt;br /&gt;
* 2. Transformation is global&lt;br /&gt;
**all procedures in program must consume an extra receiver&lt;br /&gt;
* 3. Sequentializes the program&lt;br /&gt;
&lt;br /&gt;
==Map Example==&lt;br /&gt;
 (define (map f l)&lt;br /&gt;
   (if (empty? l)&lt;br /&gt;
      empty&lt;br /&gt;
      (cons (f (first l)) &lt;br /&gt;
               (map f (rest l)))))&lt;br /&gt;
 &lt;br /&gt;
 becomes:&lt;br /&gt;
 (define (map/k f/k list k)&lt;br /&gt;
   (if (empty? list)&lt;br /&gt;
       (k empty)&lt;br /&gt;
       (f/k (first list)&lt;br /&gt;
            (lambda (f-result)&lt;br /&gt;
              (map/k f/k (rest list)&lt;br /&gt;
                     (lambda (r-result)&lt;br /&gt;
                       (k (cons f-result r-result))))))))&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Midterm_Exam_2_Appendix&amp;diff=57996</id>
		<title>Course:CPSC311/2010WT1/Midterm Exam 2 Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Midterm_Exam_2_Appendix&amp;diff=57996"/>
		<updated>2010-10-27T20:06:01Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Do not remove:&#039;&#039;&#039; This reference sheet is the appendix for CPSC 311 2010W1 midterm exam #2.  Only the first 6 printed pages are guaranteed to be printed; so be compact!  Deadline for edits: 2000 Monday 1 November.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Midterm 1 Material (copy of old appendix) =&lt;br /&gt;
&lt;br /&gt;
== Definitions == &lt;br /&gt;
&#039;&#039;&#039;Abstract syntax&#039;&#039;&#039; - idealized syntax, designed for convenient use by the underlying interpreter/compiler; result of parsing a concrete syntax expression&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Concrete syntax&#039;&#039;&#039; - syntax written by the user; consumed by the parser&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Closure&#039;&#039;&#039; - a wrapper that contains an expression (function/ binding) and its environment; expression saved until it needs to be used&lt;br /&gt;
&lt;br /&gt;
== Static vs. Dynamic Scoping ==&lt;br /&gt;
&#039;&#039;&#039;Static Scope:&#039;&#039;&#039; In a language with static scope, the scope of an identifier&#039;s binding is a syntactically delimited region.  A typical region would be the body of a function or other binding construct.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Dynamic Scope:&#039;&#039;&#039; In a language with dynamic scope, the scope of an identifier&#039;s binding is the entire remainder of the execution during which that binding is in effect. That is, in a language with dynamic scope, if a function g binds identifier n and then invokes f, then f can refer to n—and so can every other function invoked by g until it completes its execution—even though f has no locally visible binding for n.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;from PLAI, p. 36&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== Closures and First-Class vs. First-Order Functions ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-order Functions&#039;&#039;&#039; are not values in the language. They can only be defined in a designated portion of&lt;br /&gt;
the program, where they must be given names for use in the remainder of the program.  (For a language like C, the &amp;quot;designated portion&amp;quot; is anywhere in the top-level of a source file, but not, for example, within the body of another function.)  The functions&lt;br /&gt;
in F1WAE are of this nature, which explains the 1 in the name of the language.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Higher-order Functions&#039;&#039;&#039; can return other functions as values and take other functions as parameters.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-class Functions&#039;&#039;&#039; are values with all the rights of other values. In particular, they can be supplied as the&lt;br /&gt;
value of arguments to functions, returned by functions as answers, constructed as values by expressions, and stored in data structures.&lt;br /&gt;
&lt;br /&gt;
== Deferred Substitution ==&lt;br /&gt;
&#039;&#039;&#039;Deferred Substitution&#039;&#039;&#039; Initially, we have no substitutions to perform, so the repository&lt;br /&gt;
is empty. Every time we encounter a substitution (in the form of a with or application), we augment the&lt;br /&gt;
repository with one more entry, recording the identiﬁer’s name and the value (if eager) or expression (if&lt;br /&gt;
lazy) it should eventually be substituted with. We continue to evaluate without actually performing the&lt;br /&gt;
substitution. &lt;br /&gt;
&lt;br /&gt;
A side-effect of deferred substitution is dynamic scoping, which we were able to fix by using closures to store the environment from which the function was created in.&lt;br /&gt;
&lt;br /&gt;
== Eager vs. Lazy Evaluation ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lazy With&#039;&#039;&#039;- we reduce the named expression to a value only when we need to&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lazy Functions&#039;&#039;&#039;- we do not reduce the argument to a value until necessary&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Strictness Points&#039;&#039;&#039;- the points where the implementation of a lazy language forces an expression to reduce to a value (example: in a function application, need to evaluate exactly what function to invoke, can&#039;t just leave it as a function closure)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Eager&#039;&#039;&#039;- the named expression is reduced to a value before being substituting it.&lt;br /&gt;
&lt;br /&gt;
== Backus-Naur Form (BNF) ==&lt;br /&gt;
&lt;br /&gt;
A concise specification language for expressing (among other things) the concrete syntax of a programming language.  A BNF often strongly suggests a corresponding abstract syntax.&lt;br /&gt;
&lt;br /&gt;
Here&#039;s an example BNF for a language with arithmetic expressions, with expressions, first-class functions and applications, a conditional statement, and explicit language support for recursion.  This particular language allows an arbitrary number of parameters to a function in its definition or arguments in a function application.&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;CFWAE&amp;gt; ::= &amp;lt;num&amp;gt;&lt;br /&gt;
    | {+ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {- &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {* &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {/ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | &amp;lt;id&amp;gt;&lt;br /&gt;
    | {if0 &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {with {&amp;lt;id&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {fun {&amp;lt;id&amp;gt; ...} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {&amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; ...}&lt;br /&gt;
    | {rec {&amp;lt;symbol&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
&lt;br /&gt;
== Racket syntax, helpful procedures, and thoughts ==&lt;br /&gt;
&lt;br /&gt;
 (cond&lt;br /&gt;
    [Condition1 Result1]&lt;br /&gt;
    [Condition2 Result2]&lt;br /&gt;
    ....&lt;br /&gt;
    [ConditionN ResultN])&lt;br /&gt;
&lt;br /&gt;
 (cond&lt;br /&gt;
      [(number? v) ...]&lt;br /&gt;
      [(boolean? v) ...]&lt;br /&gt;
      [(string? v) ...]&lt;br /&gt;
      [else false])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(match data&lt;br /&gt;
   [pattern  action]&lt;br /&gt;
      [(? number?) (foo)]                    ;; check if data is number &lt;br /&gt;
      [(list a b c) (foo a b c) ]            ;; back-reference of list elements&lt;br /&gt;
      [((and n (? number?))) (foo n)]        ;; check if data is number and back-reference it as n&lt;br /&gt;
      [else (foo)]&lt;br /&gt;
)&lt;br /&gt;
;; Keep in mind that in pattern matching&lt;br /&gt;
;; &#039;...&#039; or &#039;___&#039; means 0 or more &lt;br /&gt;
;; &#039;..n&#039; or &#039;__n&amp;quot; means n or more &lt;br /&gt;
;; &#039;_&#039; matches anything&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Variable definition:&#039;&#039;&#039;&lt;br /&gt;
 (define x 3)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Function definition:&#039;&#039;&#039;&lt;br /&gt;
 (define (funcName arg)&lt;br /&gt;
     (+ 1 arg))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Environment:&#039;&#039;&#039; - repository of deferred substitutions&lt;br /&gt;
 (define-type Env&lt;br /&gt;
      [mtEnv]&lt;br /&gt;
      [anEnv (name symbol?)(value CFWAE-Value?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Expression Closures:&#039;&#039;&#039;&lt;br /&gt;
 (define-type CFWAE-Value&lt;br /&gt;
      [numV (num number?)]&lt;br /&gt;
      [exprV (body CFWAE?)(env Env?)] ;; used to hold an unevaluated expression and its environment (used in lazy interp)&lt;br /&gt;
      [closureV (param symbol?)(body CFWAE?)(env Env?)]) ;;used to close over the existing environment to ensure static scope&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;map&#039;&#039;&#039; - performs a procedure on all elements of the list in order&lt;br /&gt;
 map &amp;lt;procedure&amp;gt; &amp;lt;list&amp;gt;&lt;br /&gt;
 (define (parseList list)&lt;br /&gt;
 (map parse list))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Type-case&#039;&#039;&#039;&lt;br /&gt;
 (type-case WAE expr&lt;br /&gt;
             [num (n) expr]&lt;br /&gt;
             ...)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define-type&#039;&#039;&#039; (From assignment 1)&lt;br /&gt;
 (define-type WAE&lt;br /&gt;
  [num (n number?)]&lt;br /&gt;
  [binop (op procedure?) (lhs WAE?) (rhs WAE?)]&lt;br /&gt;
  [with (b Binding?) (body WAE?)]&lt;br /&gt;
  [with* (lob (listof Binding?)) (body WAE?)]&lt;br /&gt;
  [id (name symbol?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Recursion interp&#039;&#039;&#039; (From lecture)                                            &lt;br /&gt;
 (define (interp exp env)                                                      &lt;br /&gt;
  (type-case CFAE exp                                                         &lt;br /&gt;
      ...&lt;br /&gt;
     (id  (name) (lookup name env))&lt;br /&gt;
     (fun (arg-name body) (closureV arg-name body env))&lt;br /&gt;
     (if0 (tst thn els) &lt;br /&gt;
          (type-case CFAE-value (interp tst env) &lt;br /&gt;
             (numV (n) (if (zero? n) (interp thn env) (interp els env)))&lt;br /&gt;
             (else (error &amp;quot;non numeric if0 test value&amp;quot;))))&lt;br /&gt;
     (app (fun-exp arg-exp)&lt;br /&gt;
          (let ((the-fun (interp fun-exp env))&lt;br /&gt;
                (the-arg (interp arg-exp env)))&lt;br /&gt;
            (type-case CFAE-value the-fun&lt;br /&gt;
              (closureV (arg-name body closure-env)&lt;br /&gt;
                        (interp body (anEnv arg-name the-arg&lt;br /&gt;
                                            closure-env)))&lt;br /&gt;
              (else (error &amp;quot;You can only apply closures!&amp;quot;)))))&lt;br /&gt;
     (rec (bound-id named-exp body)&lt;br /&gt;
       (local ([define new-env (anEnv bound-id &lt;br /&gt;
                                      (numV 0)&lt;br /&gt;
                                      env)]&lt;br /&gt;
               [define named-value (interp named-exp new-env)])&lt;br /&gt;
         (begin&lt;br /&gt;
           (set-anEnv-val! new-env named-value)  ;; Patches (numV 0) out for the correct value&lt;br /&gt;
           (interp body new-env))))&lt;br /&gt;
       &lt;br /&gt;
     &lt;br /&gt;
     ))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Strict code&#039;&#039;&#039; (from lecture)&lt;br /&gt;
 (define (strict val)&lt;br /&gt;
  (if (expV? val)&lt;br /&gt;
      (strict (interp (expV-exp val) (expV-env val)))&lt;br /&gt;
      val))&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Midterm_Exam_2_Appendix&amp;diff=57995</id>
		<title>Course:CPSC311/2010WT1/Midterm Exam 2 Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Midterm_Exam_2_Appendix&amp;diff=57995"/>
		<updated>2010-10-27T20:04:27Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: Created page with &amp;#039;&amp;#039;&amp;#039;&amp;#039;Do not remove:&amp;#039;&amp;#039;&amp;#039; This reference sheet is the appendix for CPSC 311 2010W1 midterm exam #2.  Only the first 6 printed pages are guaranteed to be printed; so be compact!  Deadl…&amp;#039;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Do not remove:&#039;&#039;&#039; This reference sheet is the appendix for CPSC 311 2010W1 midterm exam #2.  Only the first 6 printed pages are guaranteed to be printed; so be compact!  Deadline for edits: 2000 Monday 1 November.&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1&amp;diff=57993</id>
		<title>Course:CPSC311/2010WT1</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1&amp;diff=57993"/>
		<updated>2010-10-27T20:03:44Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: /* CPSC 311 2010W1: Definition of Programming Languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== CPSC 311 2010W1: Definition of Programming Languages ==&lt;br /&gt;
&lt;br /&gt;
Programming languages is a challenging course, littered with deep ideas linked in fascinating ways and occasionally shrouded in strange terminology.  Therefore, you have this wiki to help each other (and the teaching staff!) keep up with course issues. &lt;br /&gt;
The course wiki is available for anyone to contribute to the class&#039;s learning.  It&#039;s also a great way to earn some bonus points.&lt;br /&gt;
&lt;br /&gt;
Feel free to add useful possible content to this list:&lt;br /&gt;
&lt;br /&gt;
* Exam Appendices&lt;br /&gt;
** [[Course:CPSC311/2010WT1/Midterm Exam 1 Appendix | Midterm #1 Appendix]]: fill it in, we&#039;ll print it for the exam!&lt;br /&gt;
** [[Course:CPSC311/2010WT1/Midterm Exam 2 Appendix | Midterm #2 Appendix]]: fill it in, we&#039;ll print it for the exam!&lt;br /&gt;
* A repository of people&#039;s [[Course:CPSC311/2010WT1/Lecture Notes | Lecture Notes]].  Post yours in case you miss class and want to catch up!&lt;br /&gt;
* A collection of [[Course:CPSC311/2010WT1/Handy Links | Handy Links]] to help you find course-related info.&lt;br /&gt;
* Our very own [[Course:CPSC311/2010WT1/Glossary | Glossary]] of the many terms we&#039;ll run into.&lt;br /&gt;
* [[Course:CPSC311/2010WT1/Assignment 1 Gotchas | Assignment #1 Gotchas]], pesky things that might trip you up on rudimentary interpreters&lt;br /&gt;
* [[Course:CPSC311/2010WT1/Assignment 2 Gotchas | Assignment #2 Gotchas]], pesky things that might trip you up on extended interpreters&lt;br /&gt;
* [[Course:CPSC311/2010WT1/Assignment 3 Gotchas | Assignment #3 Gotchas]], pesky things that might trip you up as you get lazy&lt;br /&gt;
&lt;br /&gt;
== Handy List of All Our Pages ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;dpl&amp;gt;&lt;br /&gt;
titlematch={{PAGENAME}}/%&lt;br /&gt;
namespace={{NAMESPACE}}&lt;br /&gt;
shownamespace=false&lt;br /&gt;
&amp;lt;/dpl&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:CPSC]]&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Midterm_Exam_1_Appendix&amp;diff=52898</id>
		<title>Course:CPSC311/2010WT1/Midterm Exam 1 Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Midterm_Exam_1_Appendix&amp;diff=52898"/>
		<updated>2010-10-07T17:46:44Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Definitions = &lt;br /&gt;
&#039;&#039;&#039;Abstract syntax&#039;&#039;&#039; - idealized syntax, designed for convenient use by the underlying interpreter/compiler; result of parsing a concrete syntax expression&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Concrete syntax&#039;&#039;&#039; - syntax written by the user; consumed by the parser&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Closure&#039;&#039;&#039; - a wrapper that contains an expression (function/ binding) and its environment; expression saved until it needs to be used&lt;br /&gt;
&lt;br /&gt;
= Static vs. Dynamic Scoping =&lt;br /&gt;
&#039;&#039;&#039;Static Scope:&#039;&#039;&#039; In a language with static scope, the scope of an identifier&#039;s binding is a syntactically delimited region.  A typical region would be the body of a function or other binding construct.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Dynamic Scope:&#039;&#039;&#039; In a language with dynamic scope, the scope of an identifier&#039;s binding is the entire remainder of the execution during which that binding is in effect. That is, in a language with dynamic scope, if a function g binds identifier n and then invokes f, then f can refer to n—and so can every other function invoked by g until it completes its execution—even though f has no locally visible binding for n.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;from PLAI, p. 36&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Closures and First-Class vs. First-Order Functions =&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-order Functions&#039;&#039;&#039; are not values in the language. They can only be defined in a designated portion of&lt;br /&gt;
the program, where they must be given names for use in the remainder of the program.  (For a language like C, the &amp;quot;designated portion&amp;quot; is anywhere in the top-level of a source file, but not, for example, within the body of another function.)  The functions&lt;br /&gt;
in F1WAE are of this nature, which explains the 1 in the name of the language.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Higher-order Functions&#039;&#039;&#039; can return other functions as values and take other functions as parameters.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-class Functions&#039;&#039;&#039; are values with all the rights of other values. In particular, they can be supplied as the&lt;br /&gt;
value of arguments to functions, returned by functions as answers, constructed as values by expressions, and stored in data structures.&lt;br /&gt;
&lt;br /&gt;
= Deferred Substitution =&lt;br /&gt;
&#039;&#039;&#039;Deferred Substitution&#039;&#039;&#039; Initially, we have no substitutions to perform, so the repository&lt;br /&gt;
is empty. Every time we encounter a substitution (in the form of a with or application), we augment the&lt;br /&gt;
repository with one more entry, recording the identiﬁer’s name and the value (if eager) or expression (if&lt;br /&gt;
lazy) it should eventually be substituted with. We continue to evaluate without actually performing the&lt;br /&gt;
substitution. &lt;br /&gt;
&lt;br /&gt;
A side-effect of deferred substitution is dynamic scoping, which we were able to fix by using closures to store the environment from which the function was created in.&lt;br /&gt;
&lt;br /&gt;
= Eager vs. Lazy Evaluation =&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lazy With&#039;&#039;&#039;- we reduce the named expression to a value only when we need to&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lazy Functions&#039;&#039;&#039;- we do not reduce the argument to a value until necessary&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Strictness Points&#039;&#039;&#039;- the points where the implementation of a lazy language forces an expression to reduce to a value (example: in a function application, need to evaluate exactly what function to invoke, can&#039;t just leave it as a function closure)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Eager&#039;&#039;&#039;- the named expression is reduced to a value before being substituting it.&lt;br /&gt;
&lt;br /&gt;
= Backus-Naur Form (BNF) =&lt;br /&gt;
&lt;br /&gt;
A concise specification language for expressing (among other things) the concrete syntax of a programming language.  A BNF often strongly suggests a corresponding abstract syntax.&lt;br /&gt;
&lt;br /&gt;
Here&#039;s an example BNF for a language with arithmetic expressions, with expressions, first-class functions and applications, a conditional statement, and explicit language support for recursion.  This particular language allows an arbitrary number of parameters to a function in its definition or arguments in a function application.&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;CFWAE&amp;gt; ::= &amp;lt;num&amp;gt;&lt;br /&gt;
    | {+ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {- &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {* &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {/ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | &amp;lt;id&amp;gt;&lt;br /&gt;
    | {if0 &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {with {&amp;lt;id&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {fun {&amp;lt;id&amp;gt; ...} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {&amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; ...}&lt;br /&gt;
    | {rec {&amp;lt;symbol&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
&lt;br /&gt;
= Racket syntax, helpful procedures, and thoughts =&lt;br /&gt;
&lt;br /&gt;
 (cond&lt;br /&gt;
    [Condition1 Result1]&lt;br /&gt;
    [Condition2 Result2]&lt;br /&gt;
    ....&lt;br /&gt;
    [ConditionN ResultN])&lt;br /&gt;
&lt;br /&gt;
 (cond&lt;br /&gt;
      [(number? v) ...]&lt;br /&gt;
      [(boolean? v) ...]&lt;br /&gt;
      [(string? v) ...]&lt;br /&gt;
      [else false])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(match data&lt;br /&gt;
   [pattern  action]&lt;br /&gt;
      [(? number?) (foo)]                    ;; check if data is number &lt;br /&gt;
      [(list a b c) (foo a b c) ]            ;; back-reference of list elements&lt;br /&gt;
      [((and n (? number?))) (foo n)]        ;; check if data is number and back-reference it as n&lt;br /&gt;
      [else (foo)]&lt;br /&gt;
)&lt;br /&gt;
;; Keep in mind that in pattern matching&lt;br /&gt;
;; &#039;...&#039; or &#039;___&#039; means 0 or more &lt;br /&gt;
;; &#039;..n&#039; or &#039;__n&amp;quot; means n or more &lt;br /&gt;
;; &#039;_&#039; matches anything&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Variable definition:&#039;&#039;&#039;&lt;br /&gt;
 (define x 3)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Function definition:&#039;&#039;&#039;&lt;br /&gt;
 (define (funcName arg)&lt;br /&gt;
     (+ 1 arg))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Environment:&#039;&#039;&#039; - repository of deferred substitutions&lt;br /&gt;
 (define-type Env&lt;br /&gt;
      [mtEnv]&lt;br /&gt;
      [anEnv (name symbol?)(value CFWAE-Value?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Expression Closures:&#039;&#039;&#039;&lt;br /&gt;
 (define-type CFWAE-Value&lt;br /&gt;
      [numV (num number?)]&lt;br /&gt;
      [exprV (body CFWAE?)(env Env?)] ;; used to hold an unevaluated expression and its environment (used in lazy interp)&lt;br /&gt;
      [closureV (param symbol?)(body CFWAE?)(env Env?)]) ;;used to close over the existing environment to ensure static scope&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;map&#039;&#039;&#039; - performs a procedure on all elements of the list in order&lt;br /&gt;
 map &amp;lt;procedure&amp;gt; &amp;lt;list&amp;gt;&lt;br /&gt;
 (define (parseList list)&lt;br /&gt;
 (map parse list))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Type-case&#039;&#039;&#039;&lt;br /&gt;
 (type-case WAE expr&lt;br /&gt;
             [num (n) expr]&lt;br /&gt;
             ...)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define-type&#039;&#039;&#039; (From assignment 1)&lt;br /&gt;
 (define-type WAE&lt;br /&gt;
  [num (n number?)]&lt;br /&gt;
  [binop (op procedure?) (lhs WAE?) (rhs WAE?)]&lt;br /&gt;
  [with (b Binding?) (body WAE?)]&lt;br /&gt;
  [with* (lob (listof Binding?)) (body WAE?)]&lt;br /&gt;
  [id (name symbol?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Recursion interp&#039;&#039;&#039; (From lecture)                                            &lt;br /&gt;
 (define (interp exp env)                                                      &lt;br /&gt;
  (type-case CFAE exp                                                         &lt;br /&gt;
      ...&lt;br /&gt;
     (id  (name) (lookup name env))&lt;br /&gt;
     (fun (arg-name body) (closureV arg-name body env))&lt;br /&gt;
     (if0 (tst thn els) &lt;br /&gt;
          (type-case CFAE-value (interp tst env) &lt;br /&gt;
             (numV (n) (if (zero? n) (interp thn env) (interp els env)))&lt;br /&gt;
             (else (error &amp;quot;non numeric if0 test value&amp;quot;))))&lt;br /&gt;
     (app (fun-exp arg-exp)&lt;br /&gt;
          (let ((the-fun (interp fun-exp env))&lt;br /&gt;
                (the-arg (interp arg-exp env)))&lt;br /&gt;
            (type-case CFAE-value the-fun&lt;br /&gt;
              (closureV (arg-name body closure-env)&lt;br /&gt;
                        (interp body (anEnv arg-name the-arg&lt;br /&gt;
                                            closure-env)))&lt;br /&gt;
              (else (error &amp;quot;You can only apply closures!&amp;quot;)))))&lt;br /&gt;
     (rec (bound-id named-exp body)&lt;br /&gt;
       (local ([define new-env (anEnv bound-id &lt;br /&gt;
                                      (numV 0)&lt;br /&gt;
                                      env)]&lt;br /&gt;
               [define named-value (interp named-exp new-env)])&lt;br /&gt;
         (begin&lt;br /&gt;
           (set-anEnv-val! new-env named-value)  ;; Patches (numV 0) out for the correct value&lt;br /&gt;
           (interp body new-env))))&lt;br /&gt;
       &lt;br /&gt;
     &lt;br /&gt;
     ))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Strict code&#039;&#039;&#039; (from lecture)&lt;br /&gt;
 (define (strict val)&lt;br /&gt;
  (if (expV? val)&lt;br /&gt;
      (strict (interp (expV-exp val) (expV-env val)))&lt;br /&gt;
      val))&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Midterm_Exam_1_Appendix&amp;diff=52897</id>
		<title>Course:CPSC311/2010WT1/Midterm Exam 1 Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Midterm_Exam_1_Appendix&amp;diff=52897"/>
		<updated>2010-10-07T17:45:50Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Do not remove:&#039;&#039;&#039; This reference sheet is the appendix for CPSC 311 2010W1 midterm exam #1.  Only the first 4 printed pages are guaranteed to be printed; so be compact!  Deadline for edits: 2000 Wednesday 6 October.&lt;br /&gt;
&lt;br /&gt;
= Definitions = &lt;br /&gt;
&#039;&#039;&#039;Abstract syntax&#039;&#039;&#039; - idealized syntax, designed for convenient use by the underlying interpreter/compiler; result of parsing a concrete syntax expression&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Concrete syntax&#039;&#039;&#039; - syntax written by the user; consumed by the parser&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Closure&#039;&#039;&#039; - a wrapper that contains an expression (function/ binding) and its environment; expression saved until it needs to be used&lt;br /&gt;
&lt;br /&gt;
= Static vs. Dynamic Scoping =&lt;br /&gt;
&#039;&#039;&#039;Static Scope:&#039;&#039;&#039; In a language with static scope, the scope of an identifier&#039;s binding is a syntactically delimited region.  A typical region would be the body of a function or other binding construct.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Dynamic Scope:&#039;&#039;&#039; In a language with dynamic scope, the scope of an identifier&#039;s binding is the entire remainder of the execution during which that binding is in effect. That is, in a language with dynamic scope, if a function g binds identifier n and then invokes f, then f can refer to n—and so can every other function invoked by g until it completes its execution—even though f has no locally visible binding for n.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;from PLAI, p. 36&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Closures and First-Class vs. First-Order Functions =&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-order Functions&#039;&#039;&#039; are not values in the language. They can only be defined in a designated portion of&lt;br /&gt;
the program, where they must be given names for use in the remainder of the program.  (For a language like C, the &amp;quot;designated portion&amp;quot; is anywhere in the top-level of a source file, but not, for example, within the body of another function.)  The functions&lt;br /&gt;
in F1WAE are of this nature, which explains the 1 in the name of the language.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Higher-order Functions&#039;&#039;&#039; can return other functions as values and take other functions as parameters.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-class Functions&#039;&#039;&#039; are values with all the rights of other values. In particular, they can be supplied as the&lt;br /&gt;
value of arguments to functions, returned by functions as answers, constructed as values by expressions, and stored in data structures.&lt;br /&gt;
&lt;br /&gt;
= Deferred Substitution =&lt;br /&gt;
&#039;&#039;&#039;Deferred Substitution&#039;&#039;&#039; Initially, we have no substitutions to perform, so the repository&lt;br /&gt;
is empty. Every time we encounter a substitution (in the form of a with or application), we augment the&lt;br /&gt;
repository with one more entry, recording the identiﬁer’s name and the value (if eager) or expression (if&lt;br /&gt;
lazy) it should eventually be substituted with. We continue to evaluate without actually performing the&lt;br /&gt;
substitution. &lt;br /&gt;
&lt;br /&gt;
A side-effect of deferred substitution is dynamic scoping, which we were able to fix by using closures to store the environment from which the function was created in.&lt;br /&gt;
&lt;br /&gt;
= Eager vs. Lazy Evaluation =&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lazy With&#039;&#039;&#039;- we reduce the named expression to a value only when we need to&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lazy Functions&#039;&#039;&#039;- we do not reduce the argument to a value until necessary&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Strictness Points&#039;&#039;&#039;- the points where the implementation of a lazy language forces an expression to reduce to a value (example: in a function application, need to evaluate exactly what function to invoke, can&#039;t just leave it as a function closure)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Eager&#039;&#039;&#039;- the named expression is reduced to a value before being substituting it.&lt;br /&gt;
&lt;br /&gt;
= Backus-Naur Form (BNF) =&lt;br /&gt;
&lt;br /&gt;
A concise specification language for expressing (among other things) the concrete syntax of a programming language.  A BNF often strongly suggests a corresponding abstract syntax.&lt;br /&gt;
&lt;br /&gt;
Here&#039;s an example BNF for a language with arithmetic expressions, with expressions, first-class functions and applications, a conditional statement, and explicit language support for recursion.  This particular language allows an arbitrary number of parameters to a function in its definition or arguments in a function application.&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;CFWAE&amp;gt; ::= &amp;lt;num&amp;gt;&lt;br /&gt;
    | {+ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {- &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {* &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {/ &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | &amp;lt;id&amp;gt;&lt;br /&gt;
    | {if0 &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {with {&amp;lt;id&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {fun {&amp;lt;id&amp;gt; ...} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
    | {&amp;lt;CFWAE&amp;gt; &amp;lt;CFWAE&amp;gt; ...}&lt;br /&gt;
    | {rec {&amp;lt;symbol&amp;gt; &amp;lt;CFWAE&amp;gt;} &amp;lt;CFWAE&amp;gt;}&lt;br /&gt;
&lt;br /&gt;
= Racket syntax, helpful procedures, and thoughts =&lt;br /&gt;
&lt;br /&gt;
 (cond&lt;br /&gt;
    [Condition1 Result1]&lt;br /&gt;
    [Condition2 Result2]&lt;br /&gt;
    ....&lt;br /&gt;
    [ConditionN ResultN])&lt;br /&gt;
&lt;br /&gt;
 (cond&lt;br /&gt;
      [(number? v) ...]&lt;br /&gt;
      [(boolean? v) ...]&lt;br /&gt;
      [(string? v) ...]&lt;br /&gt;
      [else false])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(match data&lt;br /&gt;
   [pattern  action]&lt;br /&gt;
      [(? number?) (foo)]                    ;; check if data is number &lt;br /&gt;
      [(list a b c) (foo a b c) ]            ;; back-reference of list elements&lt;br /&gt;
      [((and n (? number?))) (foo n)]        ;; check if data is number and back-reference it as n&lt;br /&gt;
      [else (foo)]&lt;br /&gt;
)&lt;br /&gt;
;; Keep in mind that in pattern matching&lt;br /&gt;
;; &#039;...&#039; or &#039;___&#039; means 0 or more &lt;br /&gt;
;; &#039;..n&#039; or &#039;__n&amp;quot; means n or more &lt;br /&gt;
;; &#039;_&#039; matches anything&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Variable definition:&#039;&#039;&#039;&lt;br /&gt;
 (define x 3)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Function definition:&#039;&#039;&#039;&lt;br /&gt;
 (define (funcName arg)&lt;br /&gt;
     (+ 1 arg))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Environment:&#039;&#039;&#039; - repository of deferred substitutions&lt;br /&gt;
 (define-type Env&lt;br /&gt;
      [mtEnv]&lt;br /&gt;
      [anEnv (name symbol?)(value CFWAE-Value?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Expression Closures:&#039;&#039;&#039;&lt;br /&gt;
 (define-type CFWAE-Value&lt;br /&gt;
      [numV (num number?)]&lt;br /&gt;
      [exprV (body CFWAE?)(env Env?)] ;; used to hold an unevaluated expression and its environment (used in lazy interp)&lt;br /&gt;
      [closureV (param symbol?)(body CFWAE?)(env Env?)]) ;;used to close over the existing environment to ensure static scope&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;map&#039;&#039;&#039; - performs a procedure on all elements of the list in order&lt;br /&gt;
 map &amp;lt;procedure&amp;gt; &amp;lt;list&amp;gt;&lt;br /&gt;
 (define (parseList list)&lt;br /&gt;
 (map parse list))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Type-case&#039;&#039;&#039;&lt;br /&gt;
 (type-case WAE expr&lt;br /&gt;
             [num (n) expr]&lt;br /&gt;
             ...)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Define-type&#039;&#039;&#039; (From assignment 1)&lt;br /&gt;
 (define-type WAE&lt;br /&gt;
  [num (n number?)]&lt;br /&gt;
  [binop (op procedure?) (lhs WAE?) (rhs WAE?)]&lt;br /&gt;
  [with (b Binding?) (body WAE?)]&lt;br /&gt;
  [with* (lob (listof Binding?)) (body WAE?)]&lt;br /&gt;
  [id (name symbol?)])&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Recursion interp&#039;&#039;&#039; (From lecture)                                            &lt;br /&gt;
 (define (interp exp env)                                                      &lt;br /&gt;
  (type-case CFAE exp                                                         &lt;br /&gt;
      ...&lt;br /&gt;
     (id  (name) (lookup name env))&lt;br /&gt;
     (fun (arg-name body) (closureV arg-name body env))&lt;br /&gt;
     (if0 (tst thn els) &lt;br /&gt;
          (type-case CFAE-value (interp tst env) &lt;br /&gt;
             (numV (n) (if (zero? n) (interp thn env) (interp els env)))&lt;br /&gt;
             (else (error &amp;quot;non numeric if0 test value&amp;quot;))))&lt;br /&gt;
     (app (fun-exp arg-exp)&lt;br /&gt;
          (let ((the-fun (interp fun-exp env))&lt;br /&gt;
                (the-arg (interp arg-exp env)))&lt;br /&gt;
            (type-case CFAE-value the-fun&lt;br /&gt;
              (closureV (arg-name body closure-env)&lt;br /&gt;
                        (interp body (anEnv arg-name the-arg&lt;br /&gt;
                                            closure-env)))&lt;br /&gt;
              (else (error &amp;quot;You can only apply closures!&amp;quot;)))))&lt;br /&gt;
     (rec (bound-id named-exp body)&lt;br /&gt;
       (local ([define new-env (anEnv bound-id &lt;br /&gt;
                                      (numV 0)&lt;br /&gt;
                                      env)]&lt;br /&gt;
               [define named-value (interp named-exp new-env)])&lt;br /&gt;
         (begin&lt;br /&gt;
           (set-anEnv-val! new-env named-value)  ;; Patches (numV 0) out for the correct value&lt;br /&gt;
           (interp body new-env))))&lt;br /&gt;
       &lt;br /&gt;
     &lt;br /&gt;
     ))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Strict code&#039;&#039;&#039; (from lecture)&lt;br /&gt;
 (define (strict val)&lt;br /&gt;
  (if (expV? val)&lt;br /&gt;
      (strict (interp (expV-exp val) (expV-env val)))&lt;br /&gt;
      val))&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Glossary&amp;diff=52151</id>
		<title>Course:CPSC311/2010WT1/Glossary</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Glossary&amp;diff=52151"/>
		<updated>2010-10-05T19:11:04Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: /* CFWAE */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Definitions==&lt;br /&gt;
&lt;br /&gt;
===Data-type=== &lt;br /&gt;
(in terms of the Racket language)&lt;br /&gt;
&lt;br /&gt;
Introduces a new data-type (an &amp;quot;algebraic&amp;quot; or &amp;quot;variant data type&amp;quot;).  Search the Racket docs for more info: http://docs.racket-lang.org/search/index.html?q=data-type.&lt;br /&gt;
&lt;br /&gt;
An explanation of how define-type works, from another course: [http://wiki.cs.grinnell.edu/mediawiki/index.php/CSC_302:_define-type CSC_302:_define-type]&lt;br /&gt;
&lt;br /&gt;
===WAE===&lt;br /&gt;
&lt;br /&gt;
One of the PLAI languages we&#039;ll work with: a &amp;quot;with arithmetic expression&amp;quot;.  In other words, an arithmetic expression that can use &amp;quot;with&amp;quot; statements in order to bind identifiers (names) to values.  We also use this as the name of the type used to represent our abstract syntax tree.  As defined in the PLAI textbook: [http://www.cs.brown.edu/~sk/Publications/Books/ProgLangs/2007-04-26/plai-2007-04-26.pdf#page=32 pg 32] &lt;br /&gt;
&lt;br /&gt;
===F1WAE===&lt;br /&gt;
&lt;br /&gt;
A language supporting only first-order functions (functions that must be called by their defined name and so cannot be passed as parameters, returned as results, or constructed as values).&lt;br /&gt;
&lt;br /&gt;
===CFWAE===&lt;br /&gt;
&lt;br /&gt;
A language extending FWAE (which has first-class functions implemented using closures) to support conditionals.&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Glossary&amp;diff=52150</id>
		<title>Course:CPSC311/2010WT1/Glossary</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Glossary&amp;diff=52150"/>
		<updated>2010-10-05T19:10:39Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: /* F1WAE */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Definitions==&lt;br /&gt;
&lt;br /&gt;
===Data-type=== &lt;br /&gt;
(in terms of the Racket language)&lt;br /&gt;
&lt;br /&gt;
Introduces a new data-type (an &amp;quot;algebraic&amp;quot; or &amp;quot;variant data type&amp;quot;).  Search the Racket docs for more info: http://docs.racket-lang.org/search/index.html?q=data-type.&lt;br /&gt;
&lt;br /&gt;
An explanation of how define-type works, from another course: [http://wiki.cs.grinnell.edu/mediawiki/index.php/CSC_302:_define-type CSC_302:_define-type]&lt;br /&gt;
&lt;br /&gt;
===WAE===&lt;br /&gt;
&lt;br /&gt;
One of the PLAI languages we&#039;ll work with: a &amp;quot;with arithmetic expression&amp;quot;.  In other words, an arithmetic expression that can use &amp;quot;with&amp;quot; statements in order to bind identifiers (names) to values.  We also use this as the name of the type used to represent our abstract syntax tree.  As defined in the PLAI textbook: [http://www.cs.brown.edu/~sk/Publications/Books/ProgLangs/2007-04-26/plai-2007-04-26.pdf#page=32 pg 32] &lt;br /&gt;
&lt;br /&gt;
===F1WAE===&lt;br /&gt;
&lt;br /&gt;
A language supporting only first-order functions (functions that must be called by their defined name and so cannot be passed as parameters, returned as results, or constructed as values).&lt;br /&gt;
&lt;br /&gt;
===CFWAE===&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Midterm_Exam_1_Appendix&amp;diff=51889</id>
		<title>Course:CPSC311/2010WT1/Midterm Exam 1 Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Midterm_Exam_1_Appendix&amp;diff=51889"/>
		<updated>2010-10-04T20:28:15Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Do not remove:&#039;&#039;&#039; This reference sheet is the appendix for CPSC 311 2010W1 midterm exam #1.  Only the first 4 printed pages are guaranteed to be printed; so be compact!  Deadline for edits: 2000 Wednesday 6 October.&lt;br /&gt;
&lt;br /&gt;
= Static vs. Dynamic Scoping =&lt;br /&gt;
&#039;&#039;&#039;Static Scope:&#039;&#039;&#039; In a language with static scope, the scope of an identifier&#039;s binding is a syntactically delimited region.  A typical region would be the body of a function or other binding construct.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Dynamic Scope:&#039;&#039;&#039; In a language with dynamic scope, the scope of an identifier&#039;s binding is the entire remainder of the execution during which that binding is in effect. That is, in a language with dynamic scope, if a function g binds identifier n and then invokes f, then f can refer to n—and so can every other function invoked by g until it completes its execution—even though f has no locally visible binding for n.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;from PLAI, p. 36&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Closures and First-Class vs. First-Order Functions =&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-order Functions&#039;&#039;&#039; are not values in the language. They can only be defined in a designated portion of&lt;br /&gt;
the program, where they must be given names for use in the remainder of the program.  (For a language like C, the &amp;quot;designated portion&amp;quot; is anywhere in the top-level of a source file, but not, for example, within the body of another function.)  The functions&lt;br /&gt;
in F1WAE are of this nature, which explains the 1 in the name of the language.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Higher-order Functions&#039;&#039;&#039; can return other functions as values.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;First-class Functions&#039;&#039;&#039; are values with all the rights of other values. In particular, they can be supplied as the&lt;br /&gt;
value of arguments to functions, returned by functions as answers, and stored in data structures.&lt;br /&gt;
&lt;br /&gt;
= Deferred Substitution =&lt;br /&gt;
&#039;&#039;&#039;Deferred Substitution&#039;&#039;&#039; Initially, we have no substitutions to perform, so the repository&lt;br /&gt;
is empty. Every time we encounter a substitution (in the form of a with or application), we augment the&lt;br /&gt;
repository with one more entry, recording the identiﬁer’s name and the value (if eager) or expression (if&lt;br /&gt;
lazy) it should eventually be substituted with. We continue to evaluate without actually performing the&lt;br /&gt;
substitution.&lt;br /&gt;
&lt;br /&gt;
= Eager vs. Lazy Evaluation =&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lazy With&#039;&#039;&#039;- we reduce the named expression to a value only when we need to&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lazy Functions&#039;&#039;&#039;- we do not reduce the argument to a value until necessary&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Strictness Points&#039;&#039;&#039;- the points where the implementation of a lazy language forces an expression to reduce to a value&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Eager&#039;&#039;&#039;- the named expression is reduced to a value before being substituting it.&lt;br /&gt;
&lt;br /&gt;
= Racket syntax, helpful procedures, and thoughts =&lt;br /&gt;
&lt;br /&gt;
 (cond&lt;br /&gt;
    [Condition1 Result1]&lt;br /&gt;
    [Condition2 Result2]&lt;br /&gt;
    ....&lt;br /&gt;
    [ConditionN ResultN])&lt;br /&gt;
&lt;br /&gt;
 (cond&lt;br /&gt;
      [(number? v) ...]&lt;br /&gt;
      [(boolean? v) ...]&lt;br /&gt;
      [(string? v) ...]&lt;br /&gt;
      [else false])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(match data&lt;br /&gt;
   [pattern  action]&lt;br /&gt;
      [(? number?) (foo)]                    ;; check if data is number &lt;br /&gt;
      [(list a b c) (foo a b c) ]            ;; back-reference of list elements&lt;br /&gt;
      [((and n (? number?))) (foo n)]        ;; check if data is number and back-reference it as n&lt;br /&gt;
      [else (foo)]&lt;br /&gt;
)&lt;br /&gt;
;; Keep in mind that in pattern matching&lt;br /&gt;
;; &#039;...&#039; or &#039;___&#039; means 0 or more &lt;br /&gt;
;; &#039;..n&#039; or &#039;__n&amp;quot; means n or more &lt;br /&gt;
;; &#039;_&#039; matches anything&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Variable definition:&#039;&#039;&#039;&lt;br /&gt;
 (define x 3)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Function definition:&#039;&#039;&#039;&lt;br /&gt;
 (define (funcName arg)&lt;br /&gt;
     (+ 1 arg))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;map&#039;&#039;&#039; performs a procedure on all elements of the list in order&lt;br /&gt;
 (define (parseList list)&lt;br /&gt;
 (map parse list))&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Midterm_Exam_1_Appendix&amp;diff=50345</id>
		<title>Course:CPSC311/2010WT1/Midterm Exam 1 Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Midterm_Exam_1_Appendix&amp;diff=50345"/>
		<updated>2010-09-27T21:13:54Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Do not remove:&#039;&#039;&#039; This reference sheet is the appendix for CPSC 311 2010W1 midterm exam #1.  Only the first 4 printed pages are guaranteed to be printed; so be compact!  Deadline for edits: 2000 Wednesday 6 October.&lt;br /&gt;
&lt;br /&gt;
= Static vs. Dynamic Scoping =&lt;br /&gt;
&lt;br /&gt;
= Closures and First-Class vs. First-Order Functions =&lt;br /&gt;
&lt;br /&gt;
= Racket syntax, helpful procedures, and thoughts =&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Midterm_Exam_1_Appendix&amp;diff=50342</id>
		<title>Course:CPSC311/2010WT1/Midterm Exam 1 Appendix</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Midterm_Exam_1_Appendix&amp;diff=50342"/>
		<updated>2010-09-27T21:10:57Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: Created page with &amp;#039;= CPSC 311 2010W1 Midterm Exam #1 Reference Sheet =  &amp;#039;&amp;#039;&amp;#039;Do not remove:&amp;#039;&amp;#039;&amp;#039; This reference sheet is the appendix for CPSC 311 2010W1 midterm exam #1.  Only the first 4 printed page…&amp;#039;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= CPSC 311 2010W1 Midterm Exam #1 Reference Sheet =&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Do not remove:&#039;&#039;&#039; This reference sheet is the appendix for CPSC 311 2010W1 midterm exam #1.  Only the first 4 printed pages are guaranteed to be printed; so be compact!  Deadline for edits: 2000 Wednesday 6 October.&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1&amp;diff=50341</id>
		<title>Course:CPSC311/2010WT1</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1&amp;diff=50341"/>
		<updated>2010-09-27T21:08:33Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: /* CPSC 311 2010W1: Definition of Programming Languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== CPSC 311 2010W1: Definition of Programming Languages ==&lt;br /&gt;
&lt;br /&gt;
Programming languages is a challenging course, littered with deep ideas linked in fascinating ways and occasionally shrouded in strange terminology.  Therefore, you have this wiki to help each other (and the teaching staff!) keep up with course issues. &lt;br /&gt;
The course wiki is available for anyone to contribute to the class&#039;s learning.  It&#039;s also a great way to earn some bonus points.&lt;br /&gt;
&lt;br /&gt;
Feel free to add useful possible content to this list:&lt;br /&gt;
&lt;br /&gt;
* Exam Appendices&lt;br /&gt;
** [[Course:CPSC311/2010WT1/Midterm Exam 1 Appendix | Midterm #1 Appendix]]: fill it in, we&#039;ll print it for the exam!&lt;br /&gt;
* A repository of people&#039;s [[Course:CPSC311/2010WT1/Lecture Notes | Lecture Notes]].  Post yours in case you miss class and want to catch up!&lt;br /&gt;
* A collection of [[Course:CPSC311/2010WT1/Handy Links | Handy Links]] to help you find course-related info.&lt;br /&gt;
* Our very own [[Course:CPSC311/2010WT1/Glossary | Glossary]] of the many terms we&#039;ll run into.&lt;br /&gt;
* [[Course:CPSC311/2010WT1/Assignment 1 Gotchas | Assignment #1 Gotchas]], pesky things that might trip you up on rudimentary interpreters&lt;br /&gt;
* [[Course:CPSC311/2010WT1/Assignment 2 Gotchas | Assignment #2 Gotchas]], pesky things that might trip you up on extended interpreters&lt;br /&gt;
* [[Course:CPSC311/2010WT1/Assignment 3 Gotchas | Assignment #3 Gotchas]], pesky things that might trip you up as you get lazy&lt;br /&gt;
&lt;br /&gt;
== Handy List of All Our Pages ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;dpl&amp;gt;&lt;br /&gt;
titlematch={{PAGENAME}}/%&lt;br /&gt;
namespace={{NAMESPACE}}&lt;br /&gt;
shownamespace=false&lt;br /&gt;
&amp;lt;/dpl&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:CPSC]]&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Lecture_Notes/2010-09-13/NNek&amp;diff=46809</id>
		<title>Course:CPSC311/2010WT1/Lecture Notes/2010-09-13/NNek</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Lecture_Notes/2010-09-13/NNek&amp;diff=46809"/>
		<updated>2010-09-14T16:14:23Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== CPSC 311 September 13 2010 ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(define-type OpMapping&lt;br /&gt;
	[opMapping (op symbol?) (impl procedure?)])&lt;br /&gt;
&lt;br /&gt;
(opMapping &#039;+ +)&lt;br /&gt;
&lt;br /&gt;
Racket is cool. Why? Functions can be passed as values, second thing is that Racket Data looks like Racket programs, or vice versa&lt;br /&gt;
&lt;br /&gt;
Concrete Syntax: (+ 5 7) == 5 + 7 == (5 7 +)&lt;br /&gt;
&lt;br /&gt;
Abstract Syntax, a single way to represent the different forms of concrete syntax, generally forms trees&lt;br /&gt;
&lt;br /&gt;
Statement, executes next statement after executing itself&lt;br /&gt;
&lt;br /&gt;
Expression, returns a value (in Racket, expressions are functions)&lt;br /&gt;
&lt;br /&gt;
Normally: Lexer (text) -&amp;gt; (tokens) Parser (abstract syntax tree) -&amp;gt; Interpreter (value)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;&lt;br /&gt;
Useful for checking for errors on tests:&lt;br /&gt;
&lt;br /&gt;
(define (failed-tests)&lt;br /&gt;
         (reverse (filter (lambda (x) (not (symbol=? (first x) &#039;good))) plai-all-test-results)))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alt-p previous, alt-n next&lt;br /&gt;
&lt;br /&gt;
Case, checks a symbol, cond checks a condition&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Lecture_Notes/2010-09-13/NNek&amp;diff=46808</id>
		<title>Course:CPSC311/2010WT1/Lecture Notes/2010-09-13/NNek</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Lecture_Notes/2010-09-13/NNek&amp;diff=46808"/>
		<updated>2010-09-14T16:13:49Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== CPSC 311 September 13 2010 ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(define-type OpMapping&lt;br /&gt;
	[opMapping (op symbol?) (impl procedure?)]&lt;br /&gt;
)&lt;br /&gt;
(opMapping ‘+ +)&lt;br /&gt;
&lt;br /&gt;
Racket is cool. Why? Functions can be passed as values, second thing is that Racket Data looks like Racket programs, or vice versa&lt;br /&gt;
&lt;br /&gt;
Concrete Syntax: (+ 5 7) == 5 + 7 == (5 7 +)&lt;br /&gt;
&lt;br /&gt;
Abstract Syntax, a single way to represent the different forms of concrete syntax, generally forms trees&lt;br /&gt;
&lt;br /&gt;
Statement, executes next statement after executing itself&lt;br /&gt;
&lt;br /&gt;
Expression, returns a value (in Racket, expressions are functions)&lt;br /&gt;
&lt;br /&gt;
Normally: Lexar (text) -&amp;gt; Parser (tokens) (abstract syntax tree) -&amp;gt; Interpreter (value)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;&lt;br /&gt;
Useful for checking for errors on tests:&lt;br /&gt;
&lt;br /&gt;
(define (failed-tests)&lt;br /&gt;
         (reverse (filter (lambda (x) (not (symbol=? (first x) &#039;good))) plai-all-test-results)))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alt-p previous, alt-n next&lt;br /&gt;
&lt;br /&gt;
Case, checks a symbol, cond checks a condition&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Lecture_Notes/2010-09-10/NNek&amp;diff=46807</id>
		<title>Course:CPSC311/2010WT1/Lecture Notes/2010-09-10/NNek</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Lecture_Notes/2010-09-10/NNek&amp;diff=46807"/>
		<updated>2010-09-14T16:13:26Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== CPSC 311 September 10 2010 ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Redefining an existing function shadows the old one&lt;br /&gt;
&lt;br /&gt;
Lambda: introduces a function without a name, becomes a function value&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;ie:&lt;br /&gt;
(define (only-pos lon)&lt;br /&gt;
   (filter (lambda (num) ( &amp;gt; num  0)) lon))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
‘(num) does not work, as it will return the list (num)&lt;br /&gt;
&lt;br /&gt;
If statement:&lt;br /&gt;
(cond&lt;br /&gt;
[boolean-expr result-expr]&lt;br /&gt;
[]&lt;br /&gt;
[]&lt;br /&gt;
[else ])&lt;br /&gt;
&lt;br /&gt;
(cons 1 2) gives (1 . 2), why does our cons work? Because we are always appending a listof numbers as the second argument.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Lecture_Notes&amp;diff=46799</id>
		<title>Course:CPSC311/2010WT1/Lecture Notes</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Lecture_Notes&amp;diff=46799"/>
		<updated>2010-09-14T03:35:29Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Lecture Notes ==&lt;br /&gt;
&lt;br /&gt;
Perhaps a format like this would work well?&lt;br /&gt;
&lt;br /&gt;
* 2010-09-08&lt;br /&gt;
** [[Course:CPSC311/2010WT1/Lecture Notes/2010-09-08/Steve | Steve&#039;s notes]]&lt;br /&gt;
* 2010-09-10&lt;br /&gt;
** [[Course:CPSC311/2010WT1/Lecture Notes/2010-09-10/NNek | NNek&#039;s Notes]]&lt;br /&gt;
* 2010-09-13&lt;br /&gt;
** [[Course:CPSC311/2010WT1/Lecture Notes/2010-09-13/NNek | NNek&#039;s notes]]&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Lecture_Notes/2010-09-10/Steve&amp;diff=46798</id>
		<title>Course:CPSC311/2010WT1/Lecture Notes/2010-09-10/Steve</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Lecture_Notes/2010-09-10/Steve&amp;diff=46798"/>
		<updated>2010-09-14T03:34:58Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: moved Course:CPSC311/2010WT1/Lecture Notes/2010-09-10/Steve to Course:CPSC311/2010WT1/Lecture Notes/2010-09-10/NNek:&amp;amp;#32;Attribute to author.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;#REDIRECT [[Course:CPSC311/2010WT1/Lecture Notes/2010-09-10/NNek]]&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Lecture_Notes/2010-09-10/NNek&amp;diff=46797</id>
		<title>Course:CPSC311/2010WT1/Lecture Notes/2010-09-10/NNek</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Lecture_Notes/2010-09-10/NNek&amp;diff=46797"/>
		<updated>2010-09-14T03:34:58Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: moved Course:CPSC311/2010WT1/Lecture Notes/2010-09-10/Steve to Course:CPSC311/2010WT1/Lecture Notes/2010-09-10/NNek:&amp;amp;#32;Attribute to author.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CPSC 311 September 10 2010&lt;br /&gt;
&lt;br /&gt;
Redefining an existing function shadows the old one&lt;br /&gt;
&lt;br /&gt;
Lambda: introduces a function without a name, becomes a function value&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;ie:&lt;br /&gt;
(define (only-pos lon)&lt;br /&gt;
   (filter (lambda (num) ( &amp;gt; num  0)) lon))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
‘(num) does not work, as it will return the list (num)&lt;br /&gt;
&lt;br /&gt;
If statement:&lt;br /&gt;
(cond&lt;br /&gt;
[boolean-expr result-expr]&lt;br /&gt;
[]&lt;br /&gt;
[]&lt;br /&gt;
[else ])&lt;br /&gt;
&lt;br /&gt;
(cons 1 2) gives (1 . 2), why does our cons work? Because we are always appending a listof numbers as the second argument.&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Lecture_Notes/2010-09-13/Steve&amp;diff=46796</id>
		<title>Course:CPSC311/2010WT1/Lecture Notes/2010-09-13/Steve</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Lecture_Notes/2010-09-13/Steve&amp;diff=46796"/>
		<updated>2010-09-14T03:34:28Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: moved Course:CPSC311/2010WT1/Lecture Notes/2010-09-13/Steve to Course:CPSC311/2010WT1/Lecture Notes/2010-09-13/NNek:&amp;amp;#32;Attribute to notes author.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;#REDIRECT [[Course:CPSC311/2010WT1/Lecture Notes/2010-09-13/NNek]]&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Lecture_Notes/2010-09-13/NNek&amp;diff=46795</id>
		<title>Course:CPSC311/2010WT1/Lecture Notes/2010-09-13/NNek</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Lecture_Notes/2010-09-13/NNek&amp;diff=46795"/>
		<updated>2010-09-14T03:34:28Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: moved Course:CPSC311/2010WT1/Lecture Notes/2010-09-13/Steve to Course:CPSC311/2010WT1/Lecture Notes/2010-09-13/NNek:&amp;amp;#32;Attribute to notes author.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CPSC 311 September 13 2010&lt;br /&gt;
&lt;br /&gt;
(define-type OpMapping&lt;br /&gt;
	[opMapping (op symbol?) (impl procedure?)]&lt;br /&gt;
)&lt;br /&gt;
(opMapping ‘+ +)&lt;br /&gt;
&lt;br /&gt;
Racket is cool. Why? Functions can be passed as values, second thing is that Racket Data looks like Racket programs, or vice versa&lt;br /&gt;
&lt;br /&gt;
Concrete Syntax: (+ 5 7) == 5 + 7 == (5 7 +)&lt;br /&gt;
&lt;br /&gt;
Abstract Syntax, a single way to represent the different forms of concrete syntax, generally forms trees&lt;br /&gt;
&lt;br /&gt;
Statement, executes next statement after executing itself&lt;br /&gt;
&lt;br /&gt;
Expression, returns a value (in Racket, expressions are functions)&lt;br /&gt;
&lt;br /&gt;
Normally: Lexar (text) -&amp;gt; Parser (tokens) (abstract syntax tree) -&amp;gt; Interpreter (value)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;&lt;br /&gt;
Useful for checking for errors on tests:&lt;br /&gt;
&lt;br /&gt;
(define (failed-tests)&lt;br /&gt;
         (reverse (filter (lambda (x) (not (symbol=? (first x) &#039;good))) plai-all-test-results)))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alt-p previous, alt-n next&lt;br /&gt;
&lt;br /&gt;
Case, checks a symbol, cond checks a condition&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Glossary&amp;diff=46775</id>
		<title>Course:CPSC311/2010WT1/Glossary</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Glossary&amp;diff=46775"/>
		<updated>2010-09-13T22:23:45Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: /* Data-type */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Definitions==&lt;br /&gt;
&lt;br /&gt;
===WAE===&lt;br /&gt;
&lt;br /&gt;
One of the PLAI languages we&#039;ll work with: a &amp;quot;with arithmetic expression&amp;quot;.  In other words, an arithmetic expression that can use &amp;quot;with&amp;quot; statements in order to bind identifiers (names) to values.  We also use this as the name of the type used to represent our abstract syntax tree.  (A link into the textbook would be handy; someone want to splice it in?  Syntax for links into PDFs available on the notes page of the website.)&lt;br /&gt;
&lt;br /&gt;
===Data-type=== &lt;br /&gt;
(in terms of the Racket language)&lt;br /&gt;
&lt;br /&gt;
Introduces a new data-type (an &amp;quot;algebraic&amp;quot; or &amp;quot;variant data type&amp;quot;).  Search the Racket docs for more info: http://docs.racket-lang.org/search/index.html?q=data-type.&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
	<entry>
		<id>https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Glossary&amp;diff=46774</id>
		<title>Course:CPSC311/2010WT1/Glossary</title>
		<link rel="alternate" type="text/html" href="https://wiki.ubc.ca/index.php?title=Course:CPSC311/2010WT1/Glossary&amp;diff=46774"/>
		<updated>2010-09-13T22:21:57Z</updated>

		<summary type="html">&lt;p&gt;SteveWolfman: /* WAE */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Definitions==&lt;br /&gt;
&lt;br /&gt;
===WAE===&lt;br /&gt;
&lt;br /&gt;
One of the PLAI languages we&#039;ll work with: a &amp;quot;with arithmetic expression&amp;quot;.  In other words, an arithmetic expression that can use &amp;quot;with&amp;quot; statements in order to bind identifiers (names) to values.  We also use this as the name of the type used to represent our abstract syntax tree.  (A link into the textbook would be handy; someone want to splice it in?  Syntax for links into PDFs available on the notes page of the website.)&lt;br /&gt;
&lt;br /&gt;
===Data-type=== &lt;br /&gt;
(in terms of the Racket language)&lt;/div&gt;</summary>
		<author><name>SteveWolfman</name></author>
	</entry>
</feed>