prac4-soln-fall09

prac4-soln-fall09 - CMSC 330, Fall 2009, Practice Problem 4...

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

View Full Document Right Arrow Icon
CMSC 330, Fall 2009, Practice Problem 4 Solutions 1. OCaml and Functional Programming a. Define functional programming Programs are expression evaluations b. Define imperative programming Programs change the value of variables c. Define higher-order functions Functions can be passed as arguments and returned as results d. Describe the relationship between type inference and static types Variable has a fixed type that can be inferred by looking at how variable is used in the code e. Describe the properties of OCaml lists Entity containing 0 or more elements of the same type. Type of list is determined by type of element. f. Describe the properties of OCaml tuples Entity containing 2 or more elements of possibly different types. Type of tuple is determined by type and number of elements. g. Define pattern variables in OCaml Variables making up patterns used by “match” h. Describe the usage of “_” in OCaml Pattern variable that can match anything but does not add binding i. Describe polymorphism Function that can take different types for same formal parameter j. Write a polymorphic OCaml function let f x = x // ‘a -> ‘a, x can be of any type k. Describe variable binding A variable (symbol) is associated with a value in an expression (or environment) l. Describe scope Portion of program where variable binding is visible m. Describe lexical scoping Variable binding determined by nearest scope in text of program n. Describe dynamic scoping Variable binding determined by nearest runtime function invocation o. Describe environment Collection of variable bindings p. Describe closure Function code + environment pair, may be invoked as function q. Describe currying Functions consume one argument at a time, returning closures until all arguments are consumed
Background image of page 1

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

View Full DocumentRight Arrow Icon
2. OCaml Types & Type Inference Give the type of the following OCaml expressions: a. [] // ‘a list b. 1::[] // int list c. 1::2::[] // int list d. [1;2;3] // int list e. [[1];[1]] // int list list f. (1) // int g. (1,”bar”) // int * string h. ([1,2], [“foo”,”bar”]) // (int * int) list * (string * string) list i. [(1,2,”foo”);(3,4,”bar”)] // (int * int * string) list j. let f x = 1 // ‘a -> int k. let f (x) = x *. 3.14 // float -> float l. let f (x,y) = x // ‘a * ‘b -> ‘a m. let f (x,y) = x+y // int * int -> int n. let f (x,y) = (x,y) // ‘a * ‘b -> ‘a * ‘b o. let f (x,y) = [x,y] // ‘a * ‘b -> (‘a * ‘b) list p. let f x y = 1 // ‘a -> ‘b -> int q. let f x y = x*y // int -> int -> int r. let f x y = x::y // ‘a -> ‘a list -> ‘a list s. let f x = match x with [] -> 1 // ‘a list -> int t. let f x = match x with (y,z) -> y+z // int * int -> int u. let f (x::_) -> x // ‘a list -> ‘a v. let f (_::y) = y // ‘a list -> ‘a list w. let f (x::y::_) = x+y // int list -> int x. let f = fun x -> x + 1 // int -> int y. let rec x = fun y -> x y // ‘a -> ‘b z. let rec f x = if (x = 0) then 1 else 1+f (x-1) // int -> int aa. let f x y z = x+y+z in f 1 2 3
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

prac4-soln-fall09 - CMSC 330, Fall 2009, Practice Problem 4...

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

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