On every sheet please give your first name, last name, and matriculation number.

Similar documents
Fun with Phantom Types

Data Structures and Algorithms Written Examination

POLYTYPIC PROGRAMMING OR: Programming Language Theory is Helpful

Regular Expressions and Automata using Haskell

Creating, Solving, and Graphing Systems of Linear Equations and Linear Inequalities

COMPSCI 105 S2 C - Assignment 2 Due date: Friday, 23 rd October 7pm

Ralf Hinze Generic Programs and Proofs

Section IV.1: Recursive Algorithms and Recursion Trees

Termination Checking: Comparing Structural Recursion and Sized Types by Examples

Deterministic Discrete Modeling

Full and Complete Binary Trees

Chapter 15 Functional Programming Languages

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Glossary of Object Oriented Terms

CIS 500 Software Foundations Midterm I, Review Questions

Programming Fundamentals. Lesson 20 Sections

CSI 333 Lecture 1 Number Systems

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

ALLIED PAPER : DISCRETE MATHEMATICS (for B.Sc. Computer Technology & B.Sc. Multimedia and Web Technology)

COMPUTER SCIENCE TRIPOS

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Programming and Reasoning with Side-Effects in IDRIS

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

Binary Number System. 16. Binary Numbers. Base 10 digits: Base 2 digits: 0 1

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Answer Key for California State Standards: Algebra I

s = s = s =

This unit will lay the groundwork for later units where the students will extend this knowledge to quadratic and exponential functions.

The Needle Programming Language

Written examination in Computer Networks

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

Programming and Reasoning with Algebraic Effects and Dependent Types

Collatz Sequence. Fibbonacci Sequence. n is even; Recurrence Relation: a n+1 = a n + a n 1.

Programming Language Rankings. Lecture 15: Type Inference, polymorphism & Type Classes. Top Combined. Tiobe Index. CSC 131! Fall, 2014!

How To Understand The Theory Of Computer Science

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

1 if 1 x 0 1 if 0 x 1

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Advanced Functional Programming (9) Domain Specific Embedded Languages

Compiler Construction

CHAPTER 3. Methods of Proofs. 1. Logical Arguments and Formal Proofs

Data Structure with C

Rolle s Theorem. q( x) = 1

a) What is the major difference between a program that runs under a virtual machine vs. one that does not?

ML for the Working Programmer

Chapter 7: Functional Programming Languages

UNIVERSITY OF LONDON (University College London) M.Sc. DEGREE 1998 COMPUTER SCIENCE D16: FUNCTIONAL PROGRAMMING. Answer THREE Questions.

Programming Languages

Activity 1: Using base ten blocks to model operations on decimals

CS 103X: Discrete Structures Homework Assignment 3 Solutions

Software Systems Engineering

10CS35: Data Structures Using C

PES Institute of Technology-BSC QUESTION BANK

Symbol Tables. Introduction

Chapter 3. if 2 a i then location: = i. Page 40

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Base Conversion written by Cathy Saxton

Anatomy of Programming Languages. William R. Cook

CS/COE

Chapter 6: Programming Languages

C H A P T E R Regular Expressions regular expression

Nonlinear Algebraic Equations. Lectures INF2320 p. 1/88

Asymptotic Improvement of Computations over Free Monads

Review: Participation Constraints

Types and Programming Languages

1 3 4 = 8i + 20j 13k. x + w. y + w

Chapter 4 Lecture Notes

Analyse et Conception Formelle. Lesson 5. Crash Course on Scala

Mathematical Induction. Lecture 10-11

Instructions for Using Your Laboratory Notebook

C++ Programming Language

Undergraduate Notes in Mathematics. Arkansas Tech University Department of Mathematics

Algebra I Notes Relations and Functions Unit 03a

Student Performance Q&A:

MATH 425, PRACTICE FINAL EXAM SOLUTIONS.

CS101 Lecture 11: Number Systems and Binary Numbers. Aaron Stevens 14 February 2011

Math 319 Problem Set #3 Solution 21 February 2002

MATHEMATICS. Y5 Multiplication and Division 5330 Square numbers, prime numbers, factors and multiples. Equipment. MathSphere

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Management Information Systems 260 Web Programming Fall 2006 (CRN: 42459)

The design recipe. Programs as communication. Some goals for software design. Readings: HtDP, sections 1-5

Introduction to Data Structures

36 CHAPTER 1. LIMITS AND CONTINUITY. Figure 1.17: At which points is f not continuous?

Software Systems Engineering

3. Mathematical Induction

1. Admission requirements for the master program

Testing and Tracing Lazy Functional Programs using QuickCheck and Hat

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Elementary Number Theory and Methods of Proof. CSE 215, Foundations of Computer Science Stony Brook University

Functional Programming in C++11

1. Give the 16 bit signed (twos complement) representation of the following decimal numbers, and convert to hexadecimal:

Pearson Algebra 1 Common Core 2015

Vieta s Formulas and the Identity Theorem

The Lean Theorem Prover

CHAPTER 2. Logic. 1. Logic Definitions. Notation: Variables are used to represent propositions. The most common variables used are p, q, and r.

Transcription:

RHEINISCH- WESTFÄLISCHE TECHNISCHE HOCHSCHULE AACHEN LEHR- UND FORSCHUNGSGEBIET INFORMATIK 2 RWTH Aachen D-52056 Aachen GERMANY http://verify.rwth-aachen.de/ LuFG Informatik II Functional Programming Exam, March 19, 2010 Prof. Dr. Jürgen Giesl Carsten Fuhs First name: Last name: Matr. number: Course of study (please mark exactly one): Master of Informatik Bachelor of Informatik for Master s studies On every sheet please give your first name, last name, and matriculation number. You must solve the exam without consulting any extra documents (e.g., course notes). Make sure your answers are readable. Do not use red pens or pencils. Please answer the exercises on the exercise sheets. If needed, also use the back sides of the exercise sheets. Answers on extra sheets can only be accepted if they are clearly marked with your name, your matriculation number, and the exercise number. Cross out text that should not be considered in the evaluation. Students that try to cheat do not pass the exam. At the end of the exam, please return all sheets together with the exercise sheets. Total number of points Number of points obtained Exercise 1 18 Exercise 2 9 Exercise 3 6 Exercise 4 6 Exercise 5 9 Exercise 6 10 Total 58 Grade -

2 Exercise 1 (4 + 5 + 4 + 5 = 18 points) The following data structure represents polymorphic binary trees that contain values only in special Value nodes that have a single successor: data Tree a = Leaf Node (Tree a) (Tree a) Value a (Tree a) Consider the tree t of characters on the right-hand side. The representation of t as an object of type Tree Char in Haskell would be: (Node (Value a (Value b Leaf)) (Node (Node Leaf Leaf) (Value c Leaf))) Implement the following functions in Haskell. a b c (a) The function foldtree of type (a -> b -> b) -> (b -> b -> b) -> b -> Tree a -> b works as follows: foldtree f g h x replaces all occurrences of the constructor Value in the tree x by f, it replaces all occurrences of the constructor Node in x by g, and it replaces all occurrences of the constructor Leaf in x by h. So for the tree t above, should compute foldtree (:) (++) [] t ((++) ((:) a ((:) b [])) ((++) ((++) [] []) ((:) c []))), which in the end results in "abc" (i.e., in the list [ a, b, c ]). Here, Value is replaced by (:), Node is replaced by (++), and Leaf is replaced by [].

3 (b) Use the foldtree function from (a) to implement the average function which has the type Tree Int -> Int and returns the average of the values that are stored in the tree. This should be accomplished as follows: Use foldtree with suitable functions as arguments in order to compute the sum of the values stored in the trees. Use foldtree with suitable functions as arguments in order to compute the number of Value-objects in the tree. Perform integer division with the pre-defined function div :: Int -> Int -> Int on these values to obtain the result. Here your function is required to work correctly only on those trees that contain the constructor Value at least once.

4 (c) Consider the following data type declaration for natural numbers: data Nats = Zero Succ Nats A graphical representation of the first four levels of the domain fornats could look like this: Succ (Succ Zero) Succ (Succ (Succ )) Succ Zero Succ (Succ ) Zero Succ Sketch a graphical representation of the first three levels of the domain for the data type Tree Bool.

5 (d) Write a Haskell function printstars that first reads a string from the user, then prints this string on the console, converts the string to a number n (using the pre-defined function read) and in the end also prints n times the character * on the console. Also give the type declaration for your function. You may use the do-notation, but you are not obliged to use it. You do not have to check whether the input string is really a number. Some of the following pre-defined functions can be helpful: getline :: IO String reads a string from the user read :: String -> Int converts a string to a number putstr :: String -> IO () writes a string to the console An example run should look as given below. Here the string 7 was read from the user. Main> printstars 7 7*******

6 Exercise 2 (4 + 5 = 9 points) Consider the following Haskell declarations for the fib function, which for a natural number x computes the value fibonacci(x): fib :: Int -> Int fib 0 = 0 fib 1 = 1 fib (x+2) = fib (x+1) + fib x (a) Please give the Haskell declarations for the higher-order function f fib corresponding to fib, i.e., the higher-order function f fib such that the least fixpoint of f fib is fib. In addition to the function declaration(s), please also give the type declaration of f fib. Since you may use full Haskell for f fib, you do not need to translate fib into simple Haskell. (b) We add the Haskell declaration bot = bot. For each n N please determine which function is computed by f fib n bot. Here f fib n bot represents the n-fold application of f fib to bot, i.e., it is short for f fib (f fib... (f fib bot)...). }{{} n times Let f n : Z Z be the function that is computed by f fib n bot. Give f n in closed form, i.e., using a non-recursive definition. In this definition, you may use the function fibonacci : N N where fibonacci(x) computes the x-th Fibonacci number. Here it suffices to give the result of your calculations. You do not need to present any intermediate steps.

7 Exercise 3 (3 + 3 = 6 points) Let D 1, D 2, D 3 be domains with corresponding complete partial orders D1, D2, D3. As we know from the lecture, then also (D2 D 3 ) is a complete partial order on (D 2 D 3 ). Now let f : D 1 D 2 and g : D 1 D 3 be functions. We then define the function h : D 1 (D 2 D 3 ) via h(x) = (f(x), g(x)). (a) Prove or disprove: If f and g are strict functions, then also h is a strict function. (b) Prove or disprove: If f and g are monotonic functions, then also h is a monotonic function.

8 Exercise 4 (6 points) We define the following data structures for natural numbers and polymorphic lists: data Nats = Zero Succ Nats data List a = Nil Cons a (List a) Consider the following expression in complex Haskell: let get n Nil = Zero get Zero (Cons x xs) = x get (Succ n) (Cons x xs) = get n xs in get Please give an equivalent expression let get =... in get in simple Haskell. Your solution should use the functions defined in the transformation from the lecture such as sel n,i, isa constr, and argof constr. However, you do not have to use the transformation rules from the lecture.

9 Exercise 5 (4 + 5 = 9 points) Consider the following data structure for polymorphic lists: data List a = Nil Cons a (List a) (a) Please translate the following Haskell expression into an equivalent lambda term (e.g., using Lam). Recall that pre-defined functions like odd or (+) are translated into constants of the lambda calculus. It suffices to give the result of the transformation. let f = \x -> if (odd x) then (\y -> x) else f ((+) x 3) in f

10 (b) Let δ be the set of rules for evaluating the lambda terms resulting from Haskell, i.e., δ contains at least the following rules: fix λf. f (fix f) times 3 2 6 Now let the lambda term t be defined as follows: t = (λx. (fix λg. x)) (λz. (times 3 2)) Please reduce the lambda term t by WHNO-reduction with the βδ -relation. You have to give all intermediate steps until you reach weak head normal form (and no further steps).

11 Exercise 6 (10 points) Use the type inference algorithm W to determine the most general type of the following lambda term under the initial type assumption A 0. Show the results of all sub-computations and unifications, too. If the term is not well typed, show how and why the W-algorithm detects this. ( (Cons λx. x) y ) The initial type assumption A 0 contains at least the following: A 0 (Cons) = a. (a (List a List a)) A 0 (x) = a. a A 0 (y) = a. a