UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

Similar documents
UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08025 INFORMATICS 1 - INTRODUCTION TO COMPUTATION

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

Module Title: Informatics 1 Functional Programming (first sitting) Exam Diet (Dec/April/Aug): December 2014 Brief notes on answers:

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS

STACKS AND QUEUES. Problem Solving with Computers-II

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Informatics 1 Functional Programming Lecture 4. Lists and Recursion. Don Sannella University of Edinburgh

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Stacks and their Applications

CS 206 Introduction to Computer Science II

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08022 COMPUTER PROGRAMMING SKILLS AND CONCEPTS

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 4

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Lecture Data Structure Stack

Formal Languages and Automata Theory, SS Project (due Week 14)

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CS115 - Module 10 - General Trees

Operators. Java operators are classified into three categories:

SECTION II: LANGUAGE BASICS

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION B /13:30-14:50 MC-4021/RCH-211

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

Operators in C. Staff Incharge: S.Sasirekha

The Stack and Queue Types

University of Washington CSE 140 Data Programming Winter Final exam. March 11, 2013

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

1 Lexical Considerations

Lexical Considerations

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

CS 340 Spring 2019 Midterm Exam

Lecture 19: Functions, Types and Data Structures in Haskell

Principles of Programming Languages 2017W, Functional Programming

Syntax and Grammars 1 / 21

Compilers. Compiler Construction Tutorial The Front-end

CSCE 314 Programming Languages

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

LAB A Translating Data to Binary

Stacks. Revised based on textbook author s notes.

Chapter 20: Binary Trees

Shell CSCE 314 TAMU. Higher Order Functions

Lexical Considerations

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Chapter 4. Flow of Control

Information Science 1

Functional Programming Mid-term exam Tuesday 3/10/2017

by Pearson Education, Inc. All Rights Reserved. 2

Containers: Stack. The Stack ADT. The Stack ADT. The Stack ADT

A LISP Interpreter in ML

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08008 INFORMATICS 2A: PROCESSING FORMAL AND NATURAL LANGUAGES

How to Design Programs

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Functional Programming in Haskell Part I : Basics

CSc 372, Spring 1996 Mid-term Examination Solution Key

Problem Score Max Score 1 Syntax directed translation & type

Decaf Language Reference Manual

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

An introduction introduction to functional functional programming programming using usin Haskell

SMURF Language Reference Manual Serial MUsic Represented as Functions

Lecture 5: Lazy Evaluation and Infinite Data Structures

Haskell Overview II (2A) Young Won Lim 9/26/16

GBIL: Generic Binary Instrumentation Language. Language Reference Manual. By: Andrew Calvano. COMS W4115 Fall 2015 CVN

CPSC 121 Some Sample Questions for the Final Exam Tuesday, April 15, 2014, 8:30AM

TDDD55 - Compilers and Interpreters Lesson 3

15-122: Principles of Imperative Computation, Fall 2015

The Design Recipe Fall 2017

Expression and Operator

Informatics Ingeniería en Electrónica y Automática Industrial

COMPILERS AND INTERPRETERS Lesson 4 TDDD16

Haskell Overview II (2A) Young Won Lim 8/9/16

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Homework 3 COSE212, Fall 2018

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

CPSC 411, 2015W Term 2 Midterm Exam Date: February 25, 2016; Instructor: Ron Garcia

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

CS 3L (Clancy) Solutions and grading standards for exam 1

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

This is an individual assignment and carries 100% of the final CPS 1000 grade.

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Stack Abstract Data Type

FOR INTERNAL SCRUTINY (date of this version: 17/2/2016) UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS

Dynamic Types, Concurrency, Type and effect system Section and Practice Problems Apr 24 27, 2018

CS 135 Fall 2018 Final Exam Review. CS 135 Fall 2018 Final Exam Review 1

PHPoC vs PHP > Overview. Overview

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

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

Programming Languages and Techniques (CIS120)

four last forms can be transformed to first form axiomatic versus operational interpretation c Anne E. Haxthausen, Spring OH4 p.

Lecture 12 Integers. Computer and Network Security 19th of December Computer Science and Engineering Department

PHPoC. PHPoC vs PHP. Version 1.1. Sollae Systems Co., Ttd. PHPoC Forum: Homepage:

Transcription:

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING Friday 20 th December 2013 14:30 to 16:30 INSTRUCTIONS TO CANDIDATES 1. Note that ALL QUESTIONS ARE COMPULSORY. 2. DIFFERENT QUESTIONS MAY HAVE DIFFERENT NUMBERS OF TOTAL MARKS. Take note of this in allocating time to questions. 3. This is an OPEN BOOK examination: notes and printed material are allowed, but no electronic devices or electronic media. Convener: J. Bradfield External Examiner: C. Johnson THIS EXAMINATION WILL BE MARKED ANONYMOUSLY

1. (a) Write a function f :: String -> Int that converts a list of base-4 digits to the corresponding numerical value. For example: f "203" = (2 * 4^2) + (0 * 4^1) + (3 * 4^0) = 35 f "13" = 7 f "1302" = 114 f "130321" = 1849 Use basic functions, list comprehension, and library functions, but not recursion. You may assume that the input is a string of base-4 digits. Credit may be given for indicating how you have tested your function. [Hint: Start by reversing the order of the digits in the list. ] [12 marks] (b) Write a second function g :: String -> Int that behaves like f, this time using basic functions, library functions and recursion, but not list comprehension. Credit may be given for indicating how you have tested your function. [Hint: Start by reversing the order of the digits in the list. ] [12 marks] Page 1 of 4

2. (a) Write a function p :: [Int] -> Bool that, given a non-empty list of numbers, returns True if each positive number in the list is greater than or equal to twice the first number in the list. For example: p [2,6,-3,18,-7,10] = True p [13] = True p [-3,6,1,-6,9,18] = True p [5,-2,-6,7] = False The function should give an error if applied to an empty list. Use basic functions, list comprehension, and library functions, but not recursion. (b) Write a second function q :: [Int] -> Bool that behaves like p, this time using basic functions and recursion, but not list comprehension or library functions. Credit may be given for indicating how you have tested your function. (c) Write a third function r :: [Int] -> Bool that also behaves like p, this time using the following higher-order library functions: map filter foldr :: (a -> b) -> [a] -> [b] :: (a -> Bool) -> [a] -> [a] :: (a -> b -> b) -> b -> [a] -> b Do not use recursion or list comprehension. Credit may be given for indicating how you have tested your function. [12 marks] Page 2 of 4

3. The following data type represents arithmetic expressions over a single variable: data Expr = X -- variable Const Int -- integer constant Neg Expr -- negation Expr :+: Expr -- addition Expr :*: Expr -- multiplication The template file includes code that enables QuickCheck to generate arbitary values of type Expr, to aid testing. Note: In this question, you will need to convert integers to and from strings: show 234 = "234" and read "234" :: Int = 234. (a) Polish Prefix Notation (PPN) is a parenthesis-free way of writing arithmetic expressions. Operators precede all of their operands, like so: X 3 in PPN is * X 3 (X 3) in PPN is - * X 3 (5 + X) 17 in PPN is * + 5 - X 17 (15 + (7 (X + 1))) 3 in PPN is * + 15 - * 7 + X 1 3 Write a function ppn :: Expr -> [String] which converts an expression to its equivalent in PPN. We will represent PPN expressions as lists of strings. For instance, for the third example above: ppn ((Const 5 :+: Neg X) :*: Const 17) should produce ["*", "+", "5", "-", "X", "17"] (b) The algorithm for evaluating an PPN expression uses a list for storing intermediate results. Starting with an empty list, and given a value for the variable X, we scan the expression from right to left until it is exhausted: If the next item is the variable X, add its value to the front of the list. If the next item is a constant, add it to the front of the list. If the next item is an operator with n arguments, then remove the first n items from the front of the list, apply the corresponding operation to them, and add the result to the front of the list. If there were fewer than n items on the list, then the original expression was ill-formed. If the next item is something else, then the original expression was illformed. At this point, there should be one item on the list, and that is the result; otherwise the original expression was ill-formed. Page 3 of 4

Implement this as a function evalppn :: [String] -> Int -> Int, where the first argument is the PPN expression and the second argument is the value of X. For example: evalppn ["*", "X", "3"] 10 = 30 evalppn ["-", "*", "X", "3"] 20 = -60 evalppn ["*", "+", "5", "-", "X", "17"] 10 = -85 evalppn ["*", "+", "15", "-", "*", "7", "+", "X", "1", "3"] 2 = -18 evalppn ["+", "-", "*", "X", "3"] 20 should produce an error, because there are not enough items on the list of intermediate values to perform the final addition. The template file includes a function evalexpr :: Expr -> Int -> Int to evaluate expressions, where evalexpr e n produces the value of e when the variable X has value n. Evaluation of an expression using evalexpr should produce the same result as evaluation of its PPN version using evalppn. Page 4 of 4