Programming Language Concepts, cs2104 Tutorial 1. Answers

Similar documents
Programming Language Concepts, CS2104 Lecture 3

Declarative Computation Model. Conditional. Case statement. Procedure values (2) Procedure values. Sequential declarative computation model

Outline. A recursive function follows the structure of inductively-defined data.

Declarative Programming Techniques

Declarative Programming Techniques

Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL. C. Varela; Adapted w. permission from S. Haridi and P. Van Roy 1

INF2220: algorithms and data structures Series 1

Accumulators. A Trivial Example in Oz. A Trivial Example in Prolog. MergeSort Example. Accumulators. Declarative Programming Techniques

Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections , 3.2, , 4.7.2)

Reasoning about Programs

searching algorithms

Lecture 7: Trees and Structural Induction

Mathematical Induction

1. Prove: A full m- ary tree with i internal vertices contains n = mi + 1 vertices.

CMSC 132, Object-Oriented Programming II Summer Lecture 12

Functional Data Structures

Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL. C. Varela; Adapted w. permission from S. Haridi and P. Van Roy 1

Foundations of Mathematics MATH 220 FALL 2017 Lecture Notes

Seminaar Abstrakte Wiskunde Seminar in Abstract Mathematics Lecture notes in progress (27 March 2010)

Extending the Lambda Calculus: An Eager Functional Language

Formal Methods for Probabilistic Systems

MATH 433 Applied Algebra Lecture 14: Functions. Relations.

RVs and their probability distributions

1 Probability Review. CS 124 Section #8 Hashing, Skip Lists 3/20/17. Expectation (weighted average): the expectation of a random quantity X is:

Logic as a Tool Chapter 1: Understanding Propositional Logic 1.1 Propositions and logical connectives. Truth tables and tautologies

CS 250/251 Discrete Structures I and II Section 005 Fall/Winter Professor York

Data Structures and Algorithms Winter Semester

Structure. Background. them to their higher order equivalents. functionals in Standard ML source code and converts

Notes on Inductive Sets and Induction

CS 61B Asymptotic Analysis Fall 2017

Any Wizard of Oz fans? Discrete Math Basics. Outline. Sets. Set Operations. Sets. Dorothy: How does one get to the Emerald City?

Roy L. Crole. Operational Semantics Abstract Machines and Correctness. University of Leicester, UK

Introduction to (Logic and Functional) Programming

An example of a decidable language that is not a CFL Implementation-level description of a TM State diagram of TM

Hashing. Dictionaries Chained Hashing Universal Hashing Static Dictionaries and Perfect Hashing. Philip Bille

Hashing. Hashing. Dictionaries. Dictionaries. Dictionaries Chained Hashing Universal Hashing Static Dictionaries and Perfect Hashing

Section 3.2 Recursively Defined Functions and Procedures

Functor Pulling. Lambert Meertens

The heart of group theory

Principles of Program Analysis: Control Flow Analysis

Checkpoint Questions Due Monday, October 1 at 2:15 PM Remaining Questions Due Friday, October 5 at 2:15 PM

First Order Predicate Logic

In mathematics there are endless ways that two entities can be related

Operationally-Based Theories of Program Equivalence

Turing Machines A Turing Machine is a 7-tuple, (Q, Σ, Γ, δ, q0, qaccept, qreject), where Q, Σ, Γ are all finite

Associated code files to read, execute in Hugs, modify: Intro.lhs, Trans1.hs, IntroFuns.lhs, ListOps.hs, Tuples.lhs. 12/5/2003. Author: J. Nino.

(Ordinary) mathematical induction is a way to prove things about natural numbers. We can write it as a rule: φ(0) x. φ(x) φ(succ(x)) x.

Lecture 4: Probability, Proof Techniques, Method of Induction Lecturer: Lale Özkahya

Asymptotic Algorithm Analysis & Sorting

Lecture 2: Randomized Algorithms

Fundamentals of Pure Mathematics - Problem Sheet

Probability Theory and Simulation Methods

Ch 3.2: Direct proofs

Theorem. For every positive integer n, the sum of the positive integers from 1 to n is n(n+1)

Fundamental Algorithms 11

1 Introduction (January 21)

TECHNISCHE UNIVERSITEIT EINDHOVEN Faculteit Wiskunde en Informatica. Final exam Logic & Set Theory (2IT61) (correction model)

Discrete Mathematics and Probability Theory Fall 2013 Vazirani Note 1

Jacques Fleuriot. Automated Reasoning Rewrite Rules

Equations of Predicate Calculus

An example of a decidable language that is not a CFL Implementation-level description of a TM State diagram of TM

A General Method for the Proof of Theorems on Tail-recursive Functions

Wed Feb The vector spaces 2, 3, n. Announcements: Warm-up Exercise:

MATH CSE20 Homework 5 Due Monday November 4

Iris: Higher-Order Concurrent Separation Logic. Lecture 6: Case Study: foldr

Linear Algebra I. Ronald van Luijk, 2015

In-Class Soln 1. CS 361, Lecture 4. Today s Outline. In-Class Soln 2

CS156: The Calculus of Computation

n n P} is a bounded subset Proof. Let A be a nonempty subset of Z, bounded above. Define the set

PROBLEM SET 3: PROOF TECHNIQUES

Automated Reasoning Lecture 5: First-Order Logic

SMT and Z3. Nikolaj Bjørner Microsoft Research ReRISE Winter School, Linz, Austria February 5, 2014

Logic for Computer Science - Week 4 Natural Deduction

Section 4.4 Functions. CS 130 Discrete Structures

Proof strategies, or, a manual of logical style

Limitations of OCAML records

Compiling Techniques

Data Structures and and Algorithm Xiaoqing Zheng

Algorithms lecture notes 1. Hashing, and Universal Hash functions

ALGEBRA. 1. Some elementary number theory 1.1. Primes and divisibility. We denote the collection of integers

MAS114: Exercises. October 26, 2018

Induction and Recursion

CS 2110: INDUCTION DISCUSSION TOPICS

20.1 2SAT. CS125 Lecture 20 Fall 2016

Proofs Regarding Specification of Spark Aggregation

CH 24 IDENTITIES. [Each product is 35] Ch 24 Identities. Introduction

Lecture 2: Proof of Switching Lemma

Finite Automata Theory and Formal Languages TMV027/DIT321 LP Recap: Logic, Sets, Relations, Functions

Computability Theory. CS215, Lecture 6,

Geometric Steiner Trees

On a diophantine equation of Andrej Dujella

Lecture 12. Statement Logic as a word algebra on the set of atomic statements. Lindenbaum algebra.

Verifying Relational Properties of Functional Programs by First-Order Refinement

Midterm 1. Your Exam Room: Name of Person Sitting on Your Left: Name of Person Sitting on Your Right: Name of Person Sitting in Front of You:

6.001, Fall Semester, 1998 Problem Set 5 2 (define (adjoin-term term series) (cons-stream term series) Since this does not produce an error, he shows

PHIL 422 Advanced Logic Inductive Proof

Propositional Logic. CS 3234: Logic and Formal Systems. Martin Henz and Aquinas Hobor. August 26, Generated on Tuesday 31 August, 2010, 16:54

How Computers Work. Computational Thinking for Everyone. Rex Page University of Oklahoma work supported by National Science Foundation Grant

Compute the Fourier transform on the first register to get x {0,1} n x 0.

Fall Lecture 5

Transcription:

Programming Language Concepts, cs2104 Tutorial 1. Answers Exercise 1. (Variables and Cells) Given: local X in X=23 local X in X=44 {Browse X} The second uses a cell: local X in X={NewCell 23} X:=44 {Browse @X} In the first, the identifier X refers to two different variables. In the second, X refers to a cell. What does Browse display in each fragment? Explain. First program: 23, since {Browse X} is visible to the outermost local statement. Second program: 44, because X is a multiple-assignment variable (cell). Exercise 2. (Accumulators) This exercise investigates how to use cells together with functions. Let us define a function {Accumulate N} that accumulates all its inputs, i.e., it adds together all the arguments of all calls. Here is an example: {Browse {Accumulate 5}} {Browse {Accumulate 100}} {Browse {Accumulate 45}} This should display 5, 105, and 150, assuming that the accumulator contains zero at the start. Here is a wrong way to write Accumulate: fun {Accumulate N} Acc in Acc={NewCell 0} Acc:=@Acc+N @Acc What is wrong with this definition? How would you correct it? It will display: 5 100 45 The reason is because the cell Acc is local to every call of Accumulate. A possible solution is to make the cell Acc visible for all calls of Accumulate. Acc={NewCell 0} fun {Accumulate N} Acc:=@Acc+N @Acc {Browse {Accumulate 5}} {Browse {Accumulate 100}} {Browse {Accumulate 45}}

Exercise 3. (Values as trees) Given is the following declaration and assignment. R=r(1:[a b c] 4:[d [e [f]]] z:q(g h [10 11 [12 13]])) Draw the value as a tree. In the following you have to give an expression composed of dot, width, and label functions that return the desired value. For example, for the value g the expression is R.z.1 and for r it is {Label R}. If there is more than one possibility, give at least two expressions. 1. b 2. q 3. 12 4. nil 5. 6. 3 7. h 8. 2 1. the value b can be given by the expression: R.1.2.1 2. the value q can be given by the expression: {Label R.z} 3. the value 12 can be given by the expression: R.z.3.2.2.1.1 4. the value nil can be given by the expressions: a. R.1.2.2.2 b. R.4.2.2 5. the value can be given by the expressions: a. {Label R.1} b. {Label R.1.2} 6. the value 3 can be given by the expressions: a. {Width R} b. {Width R.z} 7. the value h can be given by the expression: R.z.2 8. the value 2 can be given by the expressions: a. {Width R.1} b. {Width R.1.2} Exercise 4. (Traversing trees) We have seen a way to traverse in preorder (first the root, then the left child, followed by the right child). It is: Root=node(left:X1 right:x2 value:0) X1=node(left:X3 right:x4 value:1) X2=node(left:X5 right:x6 value:2) X3=node(left:nil right:nil value:3) X4=node(left:nil right:nil value:4) X5=node(left:nil right:nil value:5) X6=node(left:nil right:nil value:6) {Browse Root} proc {Preorder X} if X \= nil then {Browse X.value} if X.left \= nil then {Preorder X.left} if X.right \= nil then {Preorder X.right} {Preorder Root} Design the other known strategies, namely traverse in inorder (first the left child, then the root, followed by the right child) and postorder (first the left child, then the right child, followed by the root). proc {Inorder X} // pre X \= nil if X.left \= nil then {Inorder X.left} {Browse X.value} if X.right \= nil then {Inorder X.right}

proc {Postorder X} // pre x \= nil if X.left \= nil then {Postorder X.left} if X.right \= nil then {Postorder X.right} {Browse X.value} Using case: proc {Inorder X} case X of nil then skip [] node(left:l value:v right:r) then {Inorder L} {Browse V} {Inorder R} Exercise 5. (Pattern Matching for Head and Tail) Give definitions for Head and Tail that use pattern matching. fun {Head X _} X fun {Tail _ X} X {Browse {Head [1 2 3]}} {Browse {Tail [1 2 3]}} Exercise 6. (Length of a List) Try the version of Length as presented in the lecture. Since X from the pattern is not used in the right-hand side of the case, it can be replaced with the universal operator ( _ will not be bound to anything). Write other version, using the equality operator. Is there any more efficient way to implement Length? Using universal operator ( _ ): fun {Length Xs} nil then 0 [] _ Xr then 1+{Length Xr} Using the equality operator: fun {Length L} if L==nil then 0 else 1 + {Length {Tail L}} ================================================================================ = Programming Language Concepts, cs2104 Tutorial 2. Answers

Exercise 1. (Finding an Element in a List) Give a definition of {Member Xs Y} that tests whether Y is an element of Xs. For this assignment you have to use the truth values true and false. The equality test (that is ==) returns truth values and a function returning truth values can be used as condition in an ifexpression. For example, the call {Member [a b c] b} should return true, whereas {Member [a b c] d} should return false. fun {Member Xs Y} nil then false [] H T then if H==Y then true else {Member T Y} {Browse {Member [a b c] d}} {Browse {Member [a b c] b}} Exercise 2. (Taking and Dropping Elements) Write two functions {Take Xs N} and {Drop Xs N}. The call {Take Xs N} returns the first N elements of Xs whereas the call {Drop Xs N} returns Xs without its first N elements. For example, {Take [1 4 3 6 2] 3} returns [1 4 3] and {Drop [1 4 3 6 2] 3} returns [6 2]. Solution 1. fun {Take Xs N} if N==0 then nil else if Xs \= nil then Xs.1 {Take Xs.2 N-1} else error {Browse {Take [1 4 3 6 2] 3}} {Browse {Take [1 4 3 6 2] 7}} fun {Drop Xs N} if N==0 then Xs else if Xs \= nil then {Drop Xs.2 N-1} else error {Browse {Drop [1 4 3 6 2] 4}} {Browse {Drop [1 4 3 6 2] 6}} Solution 2. The Take function is: fun {TakeAux Xs N I} nil then if I =< N then error [] H T andthen I =< N then H {TakeAux T N I+1}

else nil fun {Take Xs N} {TakeAux Xs N 1} {Browse {Take [1 4 3 6 2] 3}} {Browse {Take [1 4 3 6 2] 7}} The Drop function is: fun {DropAux Xs N I} nil then nil [] H T then if I < N then {DropAux T N I+1} else T fun {Drop Xs N} {DropAux Xs N 1} {Browse {Drop [1 4 3 6 2] 7}} {Browse {Drop [1 4 3 6 2] 3}} % returns [6 2]. Exercise 3. (Zip and UnZip) Two important functions that convert pairlists to pairs of lists and vice versa are Zip and UnZip. a) Implement a function Zip that takes a pair Xs#Ys of two lists Xs and Ys (of the same length) and returns a pairlist, where the first field of each pair is taken from Xs and the second from Ys. For example, {Zip [a b c]#[1 2 3]} returns the pairlist [a#1 b#2 c#3]. b) The function UnZip does the inverse, for example {UnZip [a#1 b#2 c#3]} returns [a b c]#[1 2 3]. Give a specification and implementation of UnZip. a) The first solution refers to the case when the lists have the same length: fun {Zip Xs#Ys} case Xs#Ys of nil#nil then nil [] (X Xr)#(Y Yr) then X#Y {Zip Xr#Yr} {Browse {Zip [a b c]#[1 2 3]}} a) The second solution covers the cases when the lists may have also different lengths: fun {Zip X} case X of nil#nil then nil [] X#nil then {Browse First list is too long } X [] nil#x then {Browse Second list is too long } X [] (H1 T1)#(H2 T2) then H1#H2 {Zip T1#T2} {Browse {Zip [a b c]#[1 2 3]}}

{Browse {Zip [a b c d]#[1 2 3]}} {Browse {Zip [a b c]#[1 2 3 4]}} b) A condensed solution: fun {UnZip XYs} case XYs of nil then nil#nil [] X#Y XYr then Xr#Yr={UnZip XYr} in (X Xr)#(Y Yr) {Browse {UnZip [a#1 b#2 c#3]}} b) An equivalent solution, where the code is explained in some details: fun {Unzip X} case X of nil then nil#nil [] (H1#H2 T) then Local Xr Yr %Xr and Yr are local variables in Xr#Yr={Unzip T} %when coming back from the recursion, %Xr and Yr will be bound (H1 Xr)#(H2 Yr) %construct the returned value {Browse {Unzip [a#1 b#2 c#3]}} Exercise 4. (Finding the Position of an Element in a List) Write a function {Position Xs Y} that returns the first position of Y in the list Xs. The positions in a list start with 1. For example, {Position [a b c] c} returns 3 and {Position [a b c b] b} returns 2. Try two versions: 1) one that assumes that Y is an element of Xs and 2) one that returns 0, if Y does not occur in Xs. Solution 1. First version (assumes that element is included): fun {Position Xs Y} X Xr then if X==Y then 1 else 1+{Position Xr Y} Solution 2. Second version (not very efficient): fun {Position Xs Y} case Xs of nil then 0 [] X Xr then if X==Y then 1 else N={Position Xr Y} in if N==0 then 0 else N+1

Solution 3. We give an Oz program which embeds both versions, being a better solution because it is using a tail-recursive version with an accumulator: fun {PositionAux Xs Y Pos} nil then 0 [] H T then if H==Y then Pos else {PositionAux T Y Pos+1} fun {Position Xs Y} {PositionAux Xs Y 1} {Browse {Position [a b c] c}} {Browse {Position [a b c b] b}} Exercise 5. (Arithmetic Expressions Evaluation) Suppose that you are given an arithmetic expression described by a tree constructed from tuples as follows: 1. An integer is described by a tuple int(n), where N is an integer. 2. An addition is described by a tuple add(x Y), where both X and Y are arithmetic expressions. 3. A multiplication is described by a tuple mul(x Y), where both X and Y are arithmetic expressions. Implement a function Eval that takes an arithmetic expression and returns its value. For example, add(int(1) mul(int(3) int(4))) is an arithmetic expression and its evaluation returns 13. (a complete solution has been done in Lecture 4, when dealing with exceptions) fun {Eval X} case X of int(n) then N [] add(x Y) then {Eval X}+{Eval Y} [] mul(x Y) then {Eval X}*{Eval Y} {Browse {Eval add(int(1) mul(int(3) int(4)))}} ================================================================================ =========== Programming Language Concepts, cs2104 Tutorial 3. Answers %Exercise1 fun {Filter P Ls} case Ls of

nil then nil [] H T then if {P H} then H {Filter P T} else {Filter P T} {Browse {Filter (fun {$ N} if N mod 2==0 then N>=0 else false ) [1 ~2 3 ~4]}} %Exercise2 fun {FoldL Op Z L} case L of nil then Z [] H T then {FoldL Op {Op Z H} T} fun {FoldR Op Z L} case L of nil then Z [] H T then {Op H {FoldR Op Z T}} fun {Max2 Z H} if Z==neginf then H else if Z>H then Z else H fun {MaxL L} {FoldL Max2 neginf L} fun {MaxR L} {FoldR fun {$ H Z} {Max2 Z H} neginf L} {Browse {MaxL [2 4 ~6 100 40]}} {Browse {MaxR [2 4 ~6 100 40]}} {Browse {MaxL [~6 ~10]}} {Browse {MaxR [~6 ~2]}} {Browse {MaxL nil}} {Browse {MaxR nil}} % Exercise : Try implement Filter using FoldR! %Exercise3 fun {MapTuple T F} local W={Width T} NT={MakeTuple {Label T} W} in for I in 1..W do NT.I={F T.I} NT {Browse {MapTuple a(1 2 3 4) fun {$ N} N*N }} %Exercise4

fun {Fact N} if N==0 then 1 else N*{Fact N-1} fun {FactList N} if N==0 then nil else {Fact N} {FactList N-1} fun {FactLTup N} if N==0 then 1#nil else case {FactLTup N-1} of F#L then local NF=N*F in NF#(NF L) {Browse {FactList 10}} {Browse {FactLTup 10}.2}