CMSC 330, Fall 2013, Practice Problems 3

Similar documents
CMSC 330, Fall 2013, Practice Problem 3 Solutions

CMSC330 Spring 2014 Midterm 2 Solutions

CMSC330 Fall 2014 Midterm 2 Solutions

CSE 341 Section 5. Winter 2018

CMSC330 Spring 2014 Midterm #2. Score 1 OCaml types & type

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

Introduction to OCaml

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

CSC/MAT-220: Lab 6. Due: 11/26/2018

CS 11 Ocaml track: lecture 2

CMSC 330: Organization of Programming Languages

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

CSE 130, Fall 2005: Final Examination

A Brief Introduction to Standard ML

CSE 130, Fall 2006: Final Examination

CIS 120 Midterm I February 16, 2015 SOLUTIONS

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

CMSC330 Spring 2008 Final Exam

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

CSCI-GA Scripting Languages

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

CS Lectures 2-3. Introduction to OCaml. Polyvios Pratikakis

CIS 120 Midterm I October 2, Name (printed): Username (login id):

CMSC330 Fall 2010 Final Exam Solutions

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

CMSC330 Spring 2012 Final Exam Solutions

CMSC330 Fall 2013 Practice Problems 6 Solutions

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

Mini-ML. CS 502 Lecture 2 8/28/08

CMSC330 Spring 2016 Midterm #2 9:30am/12:30pm/3:30pm

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

CMSC330 Spring 2015 Final Exam 9:30am/11:00am/12:30pm

COSE212: Programming Languages. Lecture 4 Recursive and Higher-Order Programming

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

Programming Languages

CSE341 Spring 2017, Midterm Examination April 28, 2017

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

OCaml. History, Variants. ML s holy trinity. Interacting with ML. Base type: Integers. Base type: Strings. *Notes from Sorin Lerner at UCSD*

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

Hard deadline: 3/28/15 1:00pm. Using software development tools like source control. Understanding the environment model and type inference.

CSci 4223 Lecture 12 March 4, 2013 Topics: Lexical scope, dynamic scope, closures

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

CMSC 330: Organization of Programming Languages. Closures (Implementing Higher Order Functions)

max function Next concat function max function What s the pattern? concat function More on recursion Higher-order functions

Programming Languages and Techniques (CIS120)

F28PL1 Programming Languages. Lecture 14: Standard ML 4

Functional Programming. Pure Functional Programming

Recap: Functions as first-class values

Programming Paradigms

CMSC 330: Organization of Programming Languages. Objects and Abstract Data Types

Programming Languages and Techniques (CIS120)

CMSC330. Objects, Functional Programming, and lambda calculus

CMSC330 Spring 2015 Final Exam 9:30am/11:00am/12:30pm

CMSC 330: Organization of Programming Languages. OCaml Data Types

Programming Languages

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Haskell Types COMP360

The Substitution Model

Programming in Standard ML: Continued

CSE341: Programming Languages Lecture 7 First-Class Functions. Dan Grossman Winter 2013

Specification and Verification in Higher Order Logic

The Typed Racket Guide

CMSC 330: Organization of Programming Languages

Introduction to OCaml

LECTURE 16. Functional Programming

Redefinition of an identifier is OK, but this is redefinition not assignment; Thus

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

Some Advanced ML Features

CS350: Data Structures Binary Search Trees

Programming Languages and Techniques (CIS120)

CMSC 330: Organization of Programming Languages. Closures (Implementing Higher Order Functions)

Programming Languages. Tail Recursion. CSE 130: Winter Lecture 8: NOT TR. last thing function does is a recursive call

CSE 130, Winter 2011: Final Examination

CMSC 330: Organization of Programming Languages

CS 321 Programming Languages

CSCC24 Functional Programming Typing, Scope, Exceptions ML

CSE 130: Programming Languages. Polymorphism. Ranjit Jhala UC San Diego

CSE 130 [Winter 2014] Programming Languages

Introduction to Functional Programming and Haskell. Aden Seaman

Any questions. Say hello to OCaml. Say hello to OCaml. Why readability matters. History, Variants. Plan (next 4 weeks)

CMSC 330, Fall 2018 Midterm 2

Programming Languages

SNU Programming Language Theory

Anonymous Functions CMSC 330: Organization of Programming Languages

Factorial. Next. Factorial. How does it execute? Tail recursion. How does it execute? More on recursion. Higher-order functions

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Introduction to SML Getting Started

Begin at the beginning

CSE341 Autumn 2017, Midterm Examination October 30, 2017

List Processing in Ocaml

We have the pointers reference the next node in an inorder traversal; called threads

Transcription:

CMSC 330, Fall 2013, Practice Problems 3 1. OCaml and Functional Programming a. Define functional programming b. Define imperative programming c. Define higher-order functions d. Describe the relationship between type inference and static types e. Describe the properties of OCaml lists f. Describe the properties of OCaml tuples g. Define pattern variables in OCaml h. Describe the usage of _ in OCaml i. Describe polymorphism j. Write a polymorphic OCaml function k. Describe variable binding l. Describe scope m. Describe lexical scoping n. Describe dynamic scoping o. Describe environment p. Describe closure q. Describe currying 2. OCaml Types & Type Inference Give the type of the following OCaml expressions: a. [] b. 1::[] c. 1::2::[] d. [1;2;3] e. [[1];[1]] f. (1) g. (1, bar ) h. ([1,2], [ foo, bar ]) i. [(1,2, foo );(3,4, bar )] j. let f x = 1 k. let f (x) = x *. 3.14 l. let f (x,y) = x m. let f (x,y) = x+y n. let f (x,y) = (x,y) o. let f (x,y) = [x,y] p. let f x y = 1 q. let f x y = x*y r. let f x y = x::y s. let f x = match x with [] -> 1 t. let f x = match x with (y,z) -> y+z u. let f (x::_) = x v. let f (_::y) = y w. let f (x::y::_) = x+y

x. let f = fun x -> x + 1 y. let rec x = fun y -> x y z. let rec f x = if (x = 0) then 1 else 1+f (x-1) aa. let f x y z = x+y+z in f 1 2 3 bb. let f x y z = x+y+z in f 1 2 cc. let f x y z = x+y+z in f dd. let rec f x = match x with (_::t) -> 1 + f t ee. let rec f x = match x with (h::t) -> h + f t ff. let rec f = function (h::t) -> h + (2*(f t)) gg. let rec func (f, l1, l2) = match l1 with [] -> [] (h1::t1) -> match l2 with [] -> [f h1] (h2::t2) -> [f h1; f h2] 3. OCaml Types & Type Inference Write an OCaml expression with the following types: a. int list b. int * int c. int -> int d. int * int -> int e. int -> int -> int f. int -> int list -> int list g. int list list -> int list h. a -> a i. a * b -> a j. a -> b -> a k. a -> b -> b l. a list * b list -> ( a * b) list m. int -> (int -> int) n. (int -> int) -> int o. (int -> int) -> (int -> int) -> int p. ( a -> b) * ( c * c -> a) * c -> b

4. OCaml Programs What is the value of the following OCaml expressions? If an error exists, describe the error. a. 2 ; 3 b. 2 ; 3 + 4 c. (2 ; 3) + 4 d. if 1<2 then 3 else 4 e. let x = 1 in 2 f. let x = 1 in x+1 g. let x = 1 in x ; x+1 h. let x = (1, 2) in x ; x+1 i. (let x = (1, 2) in x) ; x+1 j. let x = 1 in let y = x in y k. let x = 1 let y = 2 in x+y l. let x = 1 in let x = x+1 in let x = x+1 in x m. let x = x in let x = x+1 in let x = x+1 in x n. let rec x y = x in 1 o. let rec x y = y in 1 p. let rec x y = y in x 1 q. let x y = fun z -> z+1 in x r. let x y = fun z -> z+1 in x 1 s. let x y = fun z -> z+1 in x 1 1 t. let x y = fun z -> x+1 in x 1 u. let rec x y = fun z -> x+1 in x 1 v. let rec x y = fun z -> x+y in x 1 w. let rec x y = fun z -> x y in x 1 x. let rec x y = fun z -> x z in x 1 y. let x y = y 1 in 1 z. let x y = y 1 in x aa. let x y = y 1 in x 1 bb. let x y = y 1 in x fun z -> z + 1 cc. let x y = y 1 in x (fun z -> z + 1) dd. let a = 1 in let f x y z = x+y+z+a in f 1 2 3 ee. let a = 1 in let f x y z = x+y+z+a in f 1 2-3

5. OCaml Programming a. Write an OCaml function named fib that takes an int x, and returns the Fibonacci number for x. Recall that fib(0) = 0, fib(1) = 1, fib(2) = 1, fib(3) = 2. b. Write a function find_suffixes which applied to a list lst returns a list of all the suffixes of lst. For instance, suffixes [1;2;5] = [ [1;2;5] ; [2;5] ; [5] ] c. Write an OCaml function named map_odd which takes a function f and a list lst, applies the function to every other element of the list, starting with the first element, and returns the result in a new list. d. Use map_odd and fib applied to the list [1;2;3;4;5;6;7] to calculate the Fibonacci numbers for 1, 3, 5, and 7. e. Using map, write a function triple which applied to a list of ints lst returns a list with all elements of lst tripled in value. f. Using fold, write a function all_true which applied to a list of booleans lst returns true only if all elements of lst are true. g. Using fold and anonymous helper functions, write a function product which applied to a list of ints lst returns the product of all the elements in lst. h. Using fold and anonymous helper functions, write a function find_min which applied to a list of ints lst returns the smallest element in lst. i. Using the fold function and anonymous helper functions, write a function count_vote which applied to a list of booleans lst returns a tuple (x,y) where x is the number of true elements and y is the number of false elements. j. Using the function count_vote, write a function majority which applied to a list of booleans lst returns true if 1/2 or more elements of lst are true.

6. OCaml Polymorphic Types Consider a OCaml module Bst that implements a binary search tree: module Bst = struct type bst = Empty Node of int * bst * bst let empty = Empty (* empty binary search tree *) let is_empty = function (* return true for empty bst *) Empty -> true Node (_, _, _) -> false let rec insert n = function (* insert n into binary search tree *) Empty -> Node (n, Empty, Empty) Node (m, left, right) -> if m = n then Node (m, left, right) else if n < m then Node(m, (insert n left), right) else Node(m, left, (insert n right)) (* Implement the following functions val min : bst -> int val remove : int -> bst -> bst val fold : ('a -> int -> 'a) -> 'a -> bst -> 'a val size : bst -> int *) let rec min = (* return smallest value in bst *) let rec remove n t = (* tree with n removed *) let rec fold f a t = (* apply f to nodes of t in inorder *) let size t = (* # of non-empty nodes in t *) end a. Is insert tail recursive? Explain why or why not. b. Implement min as a tail-recursive function. Raise an exception for an empty bst. Any reasonable exception is fine. c. Implement remove. The result should still be a binary search tree. d. Implement fold as an inorder traversal of the tree so that the code List.rev (fold (fun a m -> m::a) [] t) will produce an (ordered) list of values in the binary search tree. e. Implement size using fold.