final-soln-spr10

final-soln-spr10 - CMSC330 Spring 2010 Final Exam Solutions...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CMSC330 Spring 2010 Final Exam Solutions 1. (8 pts) Programming languages a. (4 pts) Briefly define a weak type system. Provide a code fragment example. Allows one type to be used as another type (or to be easily cast as another type). For instance, in C int can be used as bool in if statements “if (1) { … }”. b. (4 pts) Recall that functional languages allow functions to be passed as arguments; e.g., map takes a function as an argument and applies it to elements of a list. Java does not directly support passing functions/methods as arguments. Briefly describe how you can encode function-passing in Java to implement something like map. (1 sentence should be sufficient). Pass to the function an object A implementing an interface with a known method B, then in the function invoke A.B( ). 2. (6 pts) Ruby a. (4 pts) Name an important difference between Ruby’s nil and Java’s null . nil is an object, while null is not. nil can be treated as false, while null cannot. b. (2 pts) What is the output (if any) of the following Ruby program? Write FAIL if code does not execute. a = { } # Output = nil a[1] = “foo” puts a[0] 3. (18 pts) Regular expressions & finite automata a. (5 pts) Give a DFA that is equivalent to the regular expression a*b*c. 1 3 2 4 a c b c c a b b b. (3 pts) Give a regular expression (formal REs or in Ruby) that accepts all 3-digit binary numbers. If you choose to use Ruby, you may not use the {3} feature. (0|1)(0|1)(0|1) or /[01][01][01]/ /[01]{3}/ not allowed
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
c. (10 pts) Convert the following NFA to a DFA using the subset construction algorithm. Be sure to label each state in the DFA with the corresponding state(s) in the NFA. NFA DFA resulting from subset construction 1 3 2 4 a a b 1,2 1,2 3,4 1 2,4 a a a b 4. (6 pts) OCaml Types and Type Inference a. (2 pts) Write an OCaml expression with the following type int -> int -> int fun x y -> x+y b. (4 pts) Give the type of the following OCaml expression let f x y = y (x+1) int -> (int -> ‘a) -> ‘a 5. (6 pts) OCaml programming Write a function convert that takes as its argument a function of type (‘a -> (‘b -> ‘c)) and returns a function of type (‘a * ‘b) -> ‘c. Example: let add x y = x + y ;; let addPair = (convert add) ;; addPair (1,2) = 3 let convert f (a,b) = f a b ;; let convert f = function (a,b) -> f a b ;; let convert = fun f (a,b) -> f a b ;; let convert f x = match x with (a,b) -> f a b ;;
Background image of page 2
6. (15 pts) OCaml polymorphic types & higher-order functions Consider the OCaml type shape implementing a square and rectangular shapes: type shape = Square of int (* height *) | Rectangle of int * int (* height & width *) a. (5 pts) Write a function area
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

final-soln-spr10 - CMSC330 Spring 2010 Final Exam Solutions...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online