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 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

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

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 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

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

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

Introduction
That's it for the basics of Ruby
If you need other material for your project, come to
CMSC 330: Organization of
Programming Languages
office hours or check out the documentation
Next up: How do regular expressions (REs)
really work?
Mixture

class Point
attr_accessor "x", "y"
def initialize(x=1, y=1)
@x = x
@y = y
end
def +(other)
cp = Point.new
cp.x = @x + other.x
cp.y = @y + other.y
return cp
end
def [](index)
if index.is_a? Fixnum then
case index
when 0 then return @x
when

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

# Exercise 1
# Regular Expressions
# Create a regex which matches simplified US telephone numbers and
# uses back references i.e. ()'s to separate the numbers into their 3 parts
r = /
"808-355-5585" =~ r
puts $1
puts $2
puts $3
# Create a regex that match

class Set # Capital letter
def initialize # called via .new method
@s = cfw_
end
def insert(x)
@s[x] = true
end
def member?(x) # Style!
if @s[x] != nil then
return true
else
return false
end
end
def intersect(s)
intersection = Set.new
@s

class Point
attr_accessor "x", "y"
def initialize(x=1, y=1)
@x = x
@y = y
end
def +(other)
cp = Point.new
cp.x = @x + other.x
cp.y = @y + other.y
return cp
end
include Comparable
# def <=>(other)
# if @x < other.x then
# return -1
# el

a = [1,2,3]
puts "a is of type #cfw_a.class" # string interpolation!
# Arrays are normally indexed 0-based
for i in (0.a.size)
puts "a[#cfw_i] = #cfw_a[i]"
end
# Arrays can be indexed backwards with negative numbers (1-based)
for i in (-a.size.-1)
puts

# Everything is an object!
print "1.0 is of class ", 1.0.class, "\n"
s = "\"a double-quoted string\"
print s + " is of class ", s.class, "\n"
s = '\'a single-quoted string\'
print s + " is of class ", s.class, "\n"
print "\n"
print (3 = 3), " is of class

Building Security In
CMSC 330 Fall 2016
1
Security breaches
Just a few:
TJX (2007) - 94 million records*
Adobe (2013) - 150 million records, 38 million users
eBay (2014) - 145 million records
Anthem (2014) - Records of 80 million customers
Target (2013)