Lecture 32: Declarative Programming (Under the Hood)
Review: A Schemish Prolog
Programs in our language define subsets of Scheme expressions that
will be considered true.
(fact CONCLUSION) means that CONCLUSION is to be taken as
true, for any replacement
Lecture 27: Anatomy of an Interpreter
Last modified: Mon Apr 7 13:57:29 2014
CS61A: Lecture #27
1
Interpreting Scheme
Your project will have a structure similar to the calculator:
Split input into tokens, which are themselves Scheme values.
Parse the t
Lecture #24: Programming Languages and Programs
Metalinguistic Abstraction
A programming language is a notation for describing computations
Weve created abstractions of actionsfunctionsand of things
classes.
or processes.
These range from low-level not
Lecture #25: Calculator
Adminitrivia
Extended TA office hours in labs Tuesday from 11AM.
Exam is at 8PM on Wednesday; rooms to be assigned as happened
last time (not the same rooms: see postings and email to come).
No lecture on Wednesday, but Ill be i
class Range:
def _init_(self, low, high):
self._low = low
self._high = high
def _iter_(self):
return self._generate()
def _generate(self):
i = self._low
while i < self._high:
yield i
i += 1
class LetterIter:
"An iterator over letters.
> a_to_c = LetterIte
Lecture 27: Anatomy of an Interpreter
Interpreting Scheme
Your project will have a structure similar to the calculator:
Split input into tokens, which are themselves Scheme values.
Parse the tokens into Scheme expressions, which are also Scheme
values.
Lecture #26: Announcements
Scheme and Scheme Interpretation
Project 1 revisions: due April 10.
Hackathon: There is a hackathon hosted by H@BBig Hackvs. Stanford this Weekend. Prizes include: Macbook Airs, Retina iPads, Occulus
Rifts, Pebble Smartwatches.
Lecture #26: Announcements
Project 1 revisions: due April 10.
Hackathon: There is a hackathon hosted by H@BBig Hackvs. Stanford this Weekend. Prizes include: Macbook Airs, Retina iPads, Occulus
Rifts, Pebble Smartwatches. You dont need to have hackathon e
Lecture #23: Iterators on Trees
Slight Correction from Last Time
In the last lecture, I defined
class BinTree(Tree):
def _iter_(self): return tree_iter(self)
However, there is already an _iter_ method on BinTree, inherited
from Tree, which iterates over
Lecture #25: Calculator
A Sample Language: Calculator
Adminitrivia
Source: John Denero.
Extended TA office hours in labs Tuesday from 11AM.
Prefix notation expression language for basic arithmetic Python-like
Exam is at 8PM on Wednesday; rooms to be a
CS61A Lecture #28: The Halting Problem and
Incompleteness
The Halting Problem
An interpreter (or compiler) is a program that operates on programs.
In fact, there are numerous other ways to operate on programs. For
example,
Given a one-parameter functio
#
# Compute whether sqrt(51) - 4 < pi using generators #
#
def sqrt(a):
"Yield approximations that converge to the square root of a.
> print_iterator(sqrt(51)
[1, 26.0, 13.98076923076923, 8.81431859062533, 7.300179298823895, .]
"
x = 1
while True:
yield x
Lecture 34: Synchronization and Communication
Problem From Last Time
Simultaneous operations on data from two different programs can
cause incorrect (even bizarre) behavior.
Example: In
Program #1
balance = balance + deposit
Program #2
balance = balance
Lecture 33: Concurrency
Example of Parallelism: Sorting
Moores law (Transistors per chip doubles every N years), where
N is roughly 2 (about 1, 000, 000 increase since 1971).
Sorting a list presents obvious opportunities for parallelization.
Can illust
Lecture 33: Concurrency
Moores law (Transistors per chip doubles every N years), where
N is roughly 2 (about 1, 000, 000 increase since 1971).
Has also applied to processor speeds (with a different exponent).
But predicted to flatten: further increases
Lecture 30: Mostly Project 4 Overview
Stream Example
f = Stream(1,
lambda: Stream(1,
lambda: combine_streams(add, f, f.rest)
This creates a new stream that initially contains only one item and a
lazy continuation:
f = [ 1, lambda: Stream(1, lambda: combi
Lecture 31: Declarative Programming
Imperative vs. Declarative
So far, our programs are explicit directions for solving a problem;
the problem itself is implicit in the program.
Declarative programming turns this around:
A program is a description of t
"This module implements a declarative Logic language using Scheme syntax.
Logic is a variant of the Prolog language, based on scmlog by Paul Hilfinger
and the logic programming example in SICP.
All valid logic expressions are Scheme lists.
Valid forms inc
Lecture 29: Generators, Streams, and Lazy Evaluation
Classes of sequences
Some of the most interesting real-world problems in computer science center around sequential data.
We started with tuples and lists, which are collections of data that
are comput
Lecture #19: Complexity and Orders of Growth, contd.
The Notation
Suppose that f is a one-parameter function on real numbers.
O(f ): functions that eventually grow no faster than f :
g O(f ) means that |g(x)| Cg |f (x)| for all x Mg
where Cg and Mg ar
def desc_time(expr, setup=", number=1000):
time = 1e6 * min(timeit.repeat(expr, setup, number=number) / number
return "cfw_ loops, best of 3: cfw_:.2g usec per loop"\
.format(number, int(time)
def find_first(L, p):
"The index of the first item in list L t
Lecture #16: Inheritance and Interfaces
Inheritance
Classes are often conceptually related, sharing operations and behavior.
One important relation is the subtype or is-a relation.
Examples: A car is a vehicle. A square is a plane geometric figure.
Wh
class Polygon:
def is_simple(self):
"True iff I am simple (non-intersecting)."
def area(self): .
def bbox(self):
"(xlow, ylow, xhigh, yhigh) of bounding rectangle."
def num_sides(self): .
def vertices(self):
"My vertices, ordered clockwise, as a sequence
Lecture #13: More Sequences and Strings
Odds and Ends: Multi-Argument Map
Pythons built-in map function actually applies a function to one or
more sequences:
>
>
(1,
>
(6,
from operator import *
tuple(map(abs, (-1, 2, -4, 5)
2, 4, 5)
tuple(map(add, (1, 2
class Account: # Type name
_total_deposits = 0
# Define/initialize a class attribute
# constructor method
def _init_(self, initial_balance):
self._balance = initial_balance
Account._total_deposits += initial_balance # Use the class name
def balance(self):
def multiple(a, b):
"Return the smallest number n that is a multiple of both a and b.
> multiple(3, 4)
12
> multiple(14, 21)
42
"
factorsa = []
factorsb = []
for x in range (1,a+1,1):
if a%x = 0:
factorsa.append(x)
for y in range (1,b+1,1):
if b%y = 0:
fa
HW_SOURCE_FILE = 'hw02.py'
def square(x):
return x * x
def triple(x):
return 3 * x
def identity(x):
return x
def increment(x):
return x + 1
def product(n, term):
"Return the product of the first n terms in a sequence.
n
- a positive integer
term - a funct
HW_SOURCE_FILE = 'vitamin03.py'
def has_seven(k):
"Returns True if at least one of the digits of k is a 7, False otherwise.
> has_seven(3)
False
> has_seven(7)
True
> has_seven(2734)
True
> has_seven(2634)
False
> has_seven(734)
True
> has_seven(7777)
Tru
from operator import add, sub
def a_plus_abs_b(a, b):
"Return a+abs(b), but without calling abs.
> a_plus_abs_b(2, 3)
5
> a_plus_abs_b(2, -3)
5
"
if b < 0:
f = sub
else:
f = add
return f(a, b)
def two_of_three(a, b, c):
"Return x*x + y*y, where x and y ar
HW_SOURCE_FILE = 'hw03.py'
#
# Questions #
#
def square(x):
return x * x
def triple(x):
return 3 * x
def identity(x):
return x
def increment(x):
return x + 1
from operator import add, mul
def accumulate(combiner, base, n, term):
"Return the result of comb