CMSC351
Homework 7
Due: Friday, October 21, 2016
Put your name and section number on your solution and (if more than one sheet) staple.
Turn in Section 4 of the NP-completeness homework separately.
You are going to estimate, by hand, the size of the backt

def numberSign(num)
case
when num < 0 then return "negative"
when num > 0 then return "positive"
else return "zero"
end
end
for num in (-2.2)
print num, " is ", numberSign(num), "\n"
end

def even(x)
for i in (0.x)
if i % 2 = 0
puts i
end
end
print "\n"
end
def also_even(x)
x.times cfw_ |i|
puts i unless i % 2 != 0
print "\n"
end
def another_even(x)
0.upto(x-1) cfw_ |i|
if i % 2 = 0
puts i
end
print "\n"
end
even(8)
also_

public class Method cfw_
public static Integer[] sayN(String message, int n) cfw_
int i = 0;
while (i < n) cfw_
System.out.println(message);
i = i + 1;
Integer[] res = new Integer[2];
res[0] = i; res[1] = i;
return res;
public static void m

Last Lecture
CMSC 330: Organization of
Programming Languages
Ruby language
Implicit variable declarations
Dynamic typing
Many control statements
Classes & objects
Ruby Regular Expressions
CMSC 330
Strings
1
CMSC 330
Introduction
2
String Processing i

def numberName(num)
if num = 0 then
return "zero"
elsif num = 1 then
return "one"
else
return "superfluous"
end
end
for num in 0.3
print num, " is ", numberName(num), "\n"
end

CMSC 330: Organization of
Programming Languages
Lets, Tuples, Records
CMSC 330 - Spring 2017
1
Let Expressions
Enable binding variables in other expressions
These are different from the let definitions weve
been using at the top-level
They are expressi

CMSC 330: Organization of
Programming Languages
Functional Programming with Lists
CMSC 330 - Spring 2017
1
Lists in OCaml
The basic data structure in OCaml
Lists can be of arbitrary length
Implemented as a linked data structure
Lists must be homogeneo

CMSC351
Homework 8
Due: Friday, November 11, 2016
Turn in Section 5 of the NP-completeness homework separately.
Both assignments will be turned in on ELMS.
1. (a) Assume you have an alphabet of letters from i though r. Illustrate the operation of
radix so

CMSC351
Homework 0
Due: Friday, September 2, 2016
Problem 1. Use mathematical induction to show the following:
(a)
n
X
i(i + 1) =
i=1
n(n + 1)(n + 2)
3
(b)
n
X
2i = 2n+1 1
i=0
Problem 2.
(a) Assume bx = a. What is x (in terms of a and b)?
(b) Using only p

CMSC351
Homework 4
Due: Friday, September 30, 2016
Put your name and section number on your solution and (if more than one sheet) staple.
Section 3 of the NP-completeness homework will be due Friday, October 7.
1. Assume your machine has 64 bit words. Ass

CMSC351
Homework 9
Due: Friday, November 18, 2016
Section 6 of the NP-completeness homework will be due Friday, December 2.
1. For the following two questions, briefly show your calculations.
(a) Exactly how many comparisons does Bubble Sort use on a list

CMSC351
Homework 5
Due: Friday, October 7, 2016
Turn in Section 3 of the NP-completeness homework separately.
1. Assume that you guess that
n
X
k 2 = an3 + bn2 + cn + d .
k=1
Use constructive induction to find the constants a, b, c, and d and prove that y

CMSC351
Homework 2
Due: Friday, September 16, 2016
Put your name and section number on your solution and (if more than one sheet) staple.
Assume that you have a sorted list of n (distinct) elements, where you know that exactly one
pair of elements has bee

CMSC351
Homework 6
Due: Friday, October 13, 2016
Put your name and section number on your solution and (if more than one sheet) staple.
Section 4 of the NP-completeness homework will be due Friday, October 21.
1. Assume that your machine has an instructio

CMSC351
Homework 3
Due: Friday, September 23, 2016
Put your name and section number on your solution and (if more than one sheet) staple.
Turn in Section 2 of the NP-completeness homework separately.
1. Consider the following recurrence, defined for n a p

Anonymous Functions
CMSC 330: Organization of
Programming Languages
Recall code blocks in Ruby
(1.10).each cfw_ |x| print x
Here, we can think of cfw_ |x| print x as a function
We can do this (and more) in Ocaml
range_each (1,10) (fun x -> print_int x)

OCaml Data
So far, weve seen the following kinds of data
Basic types (int, float, char, string)
CMSC 330: Organization of
Programming Languages
Lists
One kind of data structure
A list is either [ ] or h:t, deconstructed with pattern matching
Tuples
OCa

CMSC 330: Organization of
Programming Languages
Logic Programming with Prolog
CMSC 330 Fall 2016
1
Background
1972, University of Aix-Marseille
Original goal: Natural language processing
At first, just an interpreter written in Algol
Compiler created at

CMSC 330: Organization of
Programming Languages
Names & Binding,
Type Systems
CMSC330 Fall 2016
Language Features Covered Thus Far
Ruby
Implicit declarations
Dynamic typing
cfw_x=1
cfw_ x = 1 ; x = foo
OCaml
Functional programming
Type inference
Higher

CMSC 330: Organization of
Programming Languages
Parameter Passing
CMSC 330 Fall 2016
1
Programming Language Features (cont.)
Names & binding
Namespaces
Static (lexical) scopes
Dynamic scopes
Parameter passing
Call by value
Call by reference
Call by

Lists In Prolog
[a, b, 1, hi, [X, 2] ]
But really represented as compound terms
[ ] is an atom
[a, b, c] is represented as .(a, .(b, .(c, [])
Matching over lists
?- [X, 1, Z] = [a, _, 17]
X = a,
Z = 17.
CMSC 330 Fall 2016
30
List Deconstruction
Syntacti

CMSC 330: Organization of
Programming Languages
Polymorphism
CMSC 330 Fall 2016
1
Polymorphism
Definition
Feature that allows values of different data types to
be handled using a uniform interface
Applicable to
Functions
Same function applied to differe

CMSC 330: Organization of
Programming Languages
Operational Semantics
CMSC 330 Fall 2016
1
Recall Architecture of Compilers, Interpreters
Scanner
Parser
Source
Static
Analyzer
Intermediate
Representation
Front End
Back End
Front end: syntax, (possibly) ty

CMSC 330: Organization of
Programming Languages
Parsing
CMSC 330 - Fall 2016
1
Gradescope
Sign up: Gradescope.com
CMSC330 Invite code: posted on piazza
Full Name and Student id#
First letter of you last name
Last five digits of your ID
Email: You can us

Clause Tree
Clause tree
Shows (recursive) evaluation of all clauses
Shows value (instance) of variable for each clause
Clause tree is true if all leaves are true
Factorial example
factorial(0,1).
factorial(N,F) :N > 0,
N1 is N-1,
factorial(N1,F1),
F is