Programming Paradigms Written Exam

Similar documents
Programming Paradigms Written Exam (6 CPs)

Programming Paradigms Written Exam (6 CPs)

Programming Paradigms Written Exam (6 CPs)

Programming Paradigms Written Exam (6 CPs)

Programming Paradigms Written Exam (6 CPs)

Programming Paradigms Written Exam (6 CPs)

CSCE 314 Programming Languages

Shell CSCE 314 TAMU. Higher Order Functions

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

Exam Datastrukturer. DIT960 / DIT961, VT-18 Göteborgs Universitet, CSE

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

COS 126 General Computer Science Spring Written Exam 1

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

(ii) Define a function ulh that takes a list xs, and pairs each element with all other elements in xs.

UNIVERSITETET I OSLO

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

York University. AP/ITEC Section M INTRODUCTION TO DATA STRUCTURES Winter Midterm Test

Csci 102: Sample Exam

Haskell Types, Classes, and Functions, Currying, and Polymorphism

Functional Programming in Haskell Part I : Basics

CSE Programming Languages Final exam - Spring Answer Key

Foundations of Computation

CS 340 Spring 2019 Midterm Exam

Topic 9: Type Checking

Topic 9: Type Checking

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

Consider the following EBNF definition of a small language:

A general introduction to Functional Programming using Haskell

Lecture #23: Conversion and Type Inference

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

An introduction introduction to functional functional programming programming using usin Haskell

Structure and Interpretation of Computer Programs

REPETITION CONTROL STRUCTURE LOGO

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

York University AS/AK/ITEC INTRODUCTION TO DATA STRUCTURES. Midterm Sample I. Examiner: S. Chen Duration: One Hour and 30 Minutes

Programming Paradigms

Type Systems, Type Inference, and Polymorphism

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

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

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

CPS 506 Comparative Programming Languages. Programming Language Paradigm

CMSC 331 Final Exam Section 0201 December 18, 2000

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

QUIZ: What value is stored in a after this

On Academic Dishonesty. Declarative Computation Model. Single assignment store. Single assignment store (2) Single assignment store (3)

CSCI-GA Final Exam

Topic B: Backtracking and Lists

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

CS150 Sample Final. Name: Section: A / B

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

Lecture 8: Summary of Haskell course + Type Level Programming

CPE Summer 2015 Exam I (150 pts) June 18, 2015

First Haskell Exercises

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

Chapter 15. Functional Programming Languages

CS101 PLEDGED SPRING 2001

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

Programming Language Concepts: Lecture 14

1.2 Adding Integers. Contents: Numbers on the Number Lines Adding Signed Numbers on the Number Line

Haskell Review COMP360

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Exam 1 Format, Concepts, What you should be able to do, and Sample Problems

Lecture 19: Functions, Types and Data Structures in Haskell

Student Number: Comments are not required except where indicated, although they may help us mark your answers.

CS 170 Exam 2. Version: A Spring Name (as in OPUS) (print): Instructions:

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Structure and Interpretation of Computer Programs

YOUR NAME PLEASE: *** SOLUTIONS ***

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

Section 003 Fall CS 170 Exam 2. Name (print): Instructions:

THE AUSTRALIAN NATIONAL UNIVERSITY Mid Semester Examination September COMP1730 / COMP6730 Programming for Scientists

Introduction to Programming, Aug-Dec 2006

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Shell CSCE 314 TAMU. Functions continued

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

WELCOME! (download slides and.py files and follow along!) LECTURE 1

Software Development Techniques. 26 November Marking Scheme

You have 90 minutes to complete the exam of InformatikIIb. The following rules apply:

CSc 372, Fall 2001 Final Examination December 14, 2001 READ THIS FIRST

CIS 110 Introduction to Computer Programming Summer 2018 Midterm. Recitation ROOM :

Department of Electrical Engineering and Computer Sciences Fall 2000 Instructor: Dan Garcia CS 3 Final Exam

Database Management Systems Written Examination

CSE413 Midterm. Question Max Points Total 100

CS 11 Haskell track: lecture 1

Introduction. chapter Functions

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

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

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions:

Final Examination Term: 162 Year: 2016/2017 (v2)

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

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

THE CONCEPTION AND APPLICATION OF PFL : A PROCESS FUNCTIONAL PROGRAMMING LANGUAGE

UNIVERSITY OF TORONTO SCARBOROUGH. Wnter 2016 EXAMINATIONS. CSC A20H Duration 2 hours 45 mins. No Aids Allowed

The University of Nottingham

Principles of Programming Languages

CIS 110 Fall 2014 Introduction to Computer Programming 8 Oct 2014 Midterm Exam Name:

Transcription:

Programming Paradigms Written Exam 17.06.2014 First name Student number Last name Signature Instructions for Students Write your name and student number on the exam sheet and on every solution sheet you hand in and also sign them. This is a closed book exam: the only resources allowed are blank paper and pens (do not use pencils). Write neatly and clearly. The clarity of your explanations will affect your grade. The duration of the exam is 2 hours. Good luck! Do not write in this space Question Marks Achieved 1 25 2 10 3 10 4 10 5 10 6 10 7 15 8 10 Total 100 1

Exercise 1 (25 marks) a. (5 marks) Briefly describe the main differences between a compiled language and an interpreted language. b. (5 marks) When you move the execution of an Erlang program from a single-processor machine to a multi-core machine or a cluster of computers, do you have to rewrite or adapt your program? Explain your answer. c. (5 marks) Briefly describe the difference between the following expressions in Prolog:?- 7 = 3 + 4?- 7 is 3 + 4 What will be the result of executing these expressions? d. (5 marks) Given is the expression [20, 20.0, 20.0]. Is the expression legal in Ruby? Is the expression legal in Haskell? Explain your answers and briefly describe the meaning of this expression. e. (5 marks) Briefly explain the concept of list comprehension in Haskell and give a short example. 2

Exercise 2 (10 marks) Write a Ruby function palindrome(a) to check whether an array of characters represents a palindrome, i.e., is identical to the reversed array. For instance, the array [ A, B, B, A ] represents a palindrom, while [ A, B, B, A, C ] does not. You are not allowed to use Ruby s array method reverse. Exercise 3 (10 marks) Write a Ruby function to calculate the fuel consumption of a car (in liters) for road trips it has taken. The input parameters of the function are fuelconsumpt, measured in liters per 100 kilometers, and trips[], an array containing the distances of road trips (in kilometers) taken by that car. Compute the overall fuel consumption for the voyages stored in trips. Exercise 4 (10 marks) Write a Prolog program that removes all duplicates from a list and returns the duplicate-free list. For instance, given the list L1 = [a,b,a,c,a,a,b], the duplicate-free list is L2 = [c,a,b]. Note that the order of the elements in L2 does not matter. You can use the built-in predicate member(x,l) to check whether X is a member in L. Exercise 5 (10 marks) Write a Prolog program that finds all solutions to the equation x x = y + y with x being in the range of [1, 3] and y in the range of [1, 5]. Exercise 6 (10 marks) Write a function loop for an Erlang process that does the following. When it receives the message "event", it ss a message containing the current count of events to a process registered with an atom called observer, displays the count on the screen, and increments the current event count. If it receives the message "bye", it displays Bye on the screen and quits. Exercise 7 (15 marks) Write a function isbalanced in Haskell that checks whether a string containing open and closed parentheses is balanced. A string of parentheses is balanced when every open one has a corresponding closed one and at any point there are not more closed ones than open ones. An empty string is balanced. For example, (()) is balanced (())() is balanced ()(() is not balanced ()) is not balanced Exercise 8 (10 marks) Look at the following recursive Haskell program. a. (4 marks) Briefly describe what the program does. mystery :: [a] -> Integer mystery [] = 0 mystery (h:t) = 1 + mystery t b. (6 marks) Transform this program into a tail-recursive one. 3

Solution 1 a. In a compiled language, a program (the source code) is translated into a form that can be run directly on a computer s processor. Interpreted languages are processed by a higher-level virtual machine. Usually, a program is translated on the fly, i.e., a statement is tanslated and the immediately executed. b. No, Erlang programs don t have to be modified to run on a multi-core. The main reason for is are that processes do not share any resources and communicate exclusively by message passing. Therefore it makes no difference whether they run on the same machine or on different machines. The Erlang virtual machine will automatically adapt and use the underlying hardware. c. In the expression 7 = 3 + 4, the unification operator = tries to match the left-hand and right-hand side. This is not possible here, since 3+4 is not evaluated, hence the two sides are different, and the goal fails. In the expression 7 is 3 + 4, the is operator first evaluates the right-hand side to the value 7 and then matches it to the left-hand side; the goal succeeds. d. The expression is legal in Ruby and represents an array of an integer, a string, and a float. Ruby allows arrays of objects with different type. This is also called Duck typing. The expression is not legal in Haskell. Haskell is a strongly typed language and does not allow an array to contain elements with different type. e. List comprehension in Haskell is a compact way to specify complex and possibly infinite lists by specifying an output function, a variable, an input set and a predicate. Example: The list of even numbers between 1 and 100 can be defined as [x x <- [1..100], mod x 2 == 0] Solution 2 def palindrome(a) if a.length == 1 a.length == 0 true if a[0] == a[-1] palindrome(a[1..-2]) false 4

Solution 3 def fuel(fuelconsumpt, trips=[]) sum = 0.0 trips.each{ x sum = sum + (x/100 * fuelconsumpt) } return sum Solution 4 rmdup( [], [] ). rmdup( [H T], R ):- member( H, T ),!, rmdup(t,r). rmdup( [H T], [H Rest] ):- rmdup( T, Rest ). Solution 5 x(1). x(2). x(3). y(1). y(2). y(3). y(4). y(5). eq(x,y) :- x(x), y(y), A is X*X, A is Y+Y. Solution 6 -module(eventcount). -export([loop/1]). loop(counter) -> receive "event" -> observer! Counter, io:format("event ~p~n",[counter]), loop(counter+1); "bye" -> io:format("bye~n"). 5

Solution 7 module Paran where isbalanced :: [Char] -> Int -> Bool isbalanced [] 0 = True isbalanced [] nonzero = False isbalanced (h:t) x = if x < 0 then False if h == ( then isbalanced t (x+1) isbalanced t (x-1) Solution 8 a. The program computes the length of an array. b. Tail-recursive version: pass the length of the list seen so far as a second parameter; when the list is empty, the second parameter contains the length of the list. module Tail ( len ) where len :: [a] -> Integer -> Integer len [] x = x len (h:t) x = len t (x+1) 6