Your cart is currently empty!
[3 pts] Imagine a parallel universe in which Ruby is statically typed. Give the type of the simple method or if there is a type error, explain why there is one and if the same program would error with real Ruby. def simple(x) if x == x then 1 else false …
def simple(x) if x == x then
1
else false
end end
def sum(xs) total = 0 xs.each do |x|
total = total + x end
total end
sum([1, 2, 3])
let simple x = if x = x then
0
else
1 + (fun x -> x)
in simple 1
2
Regular Expressions
false
Fill in the blank below with your regular expression.
def foods?(str) | |
str =~ / | / |
end
Fill in the blank below with your regular expression.
def odd_as?(str) | |
str =~ / | / |
end
3
Ruby Execution
Next to each Ruby snippet, write the output after executing it. If there is an error, then write “error.”
cmsc = [131, 132, 216, 250, 330, 351] cmsc.reverse!
puts(cmsc[0])
hash = Hash.new(-1) ruby = “Ruby” ocaml = “OCaml” hash[ruby] = 3 hash[ocaml] = 4
puts(hash[ocaml + ruby]) puts(hash[ocaml] + hash[ruby])
def together(arr) str = “” arr.each do |x|
str += x.to_s end
str end
puts(together([1, 2, 3, 4, 5]))
4
def double(val) yield(2 * val)
end
arr = [“Alpha”, “Beta”, “Charlie”] double(1) { |x| puts(arr[x]) }
5
Ruby Programming
You’ve recently been hired by the newest pizzeria in town and the owner has asked you to create a way to take orders and pass them along to the delivery driver. Each order comes as a string that contains pizza toppings and the associated address. Due to huge success, each address is limited to a single order at a given time, so if a new order comes from the same address, the old order is replaced.
“3900”
[“4131”, “3900”, “7000”]
[“9001”]
[]
6
Implement your solution in the class skeleton below.
class Pizzeria
def initialize()
end
def take_order(order)
end
def give_order(location)
end
def route()
end
end
7
OCaml Typing
Recall the definition of ‘a option.
type ‘a option = Some of ‘a | None
Determine the type of the following expressions. If there is an error, write “error” and give a brief explanation.
fun x -> [5] :: x
[3; 4.0]
let f x y z = x +. y +. z in f 1.0 2.0
Write an expression that has the following type, without using type annotations.
8
OCaml Execution
Recall the definitions of map, fold_left, and fold_right.
let rec map f xs =
match xs with
|[]->[]
| x :: xt -> (f x) :: (map f xt)
let rec fold_right f xs a = match xs with
| [] -> a
| x :: xt -> f x (fold_right f xt a)
let rec fold_left f a xs = match xs with
| [] -> a
| x :: xt -> fold_left f (f a x) xt
Write the final value of the following OCaml expressions next to each snippet. If there is an error, write “error.”
let x = 3 in
let f x y = x + y in f 5 2
let f a x = x :: a in fold_left f [] [1; 2; 3]
map (fun a x -> if (x mod 3) = 0 then x :: a else a) [1; 3; 11; 27]
let f x y z = if y = x then y :: z else z in fold_right (f 3) [1; 2; 3; 3; 2; 1] []
9
OCaml Programming
Consider the argmin (f : (int -> int)) (xs : int list) : int function that returns the last element of the list xs that minimizes the given function f. You may assume the function only operates on non-empty lists. Here are some examples:
0
2
1
List.fold_left
(fun acc x -> | |
if | then |
else
)
10
let (min_x, _) =
List.fold_left | ||||||
(fun (x’, y’) x -> | ||||||
let y = | in | |||||
then | ||||||
if | ||||||
else
)
(x, f x)
xt
in min_x
11
Finite Automata
Is the above finite automaton an NFA or DFA? (Select the most specific option.)
(abc⋆)j(a⋆)
12
with their appropriate subsets.
13