Exercise 1: Graph coloring (10 points)

Similar documents
Functional Programming

Functional Programming. Pure Functional Programming

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

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages

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

List are immutable Lists have recursive structure Lists are homogeneous

Homework 3 COSE212, Fall 2018

An introduction to Scheme

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

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

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Programming Principles

Programming Principles

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Collections and Combinatorial Search

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

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

More Functions on Lists

User-defined Functions. Conditional Expressions in Scheme

List Functions, and Higher-Order Functions

CS115 - Module 10 - General Trees

CSE 341 Section Handout #6 Cheat Sheet

Modern Programming Languages. Lecture LISP Programming Language An Introduction

CS 314 Principles of Programming Languages

Principles of Programming Languages

Programming Principles

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

CSCI 3155: Homework Assignment 4

CSCI 3155: Homework Assignment 3

Structure and Interpretation of Computer Programs

A Brief Introduction to Scheme (II)

COMPUTER SCIENCE TRIPOS

GADTs. Alejandro Serrano. AFP Summer School. [Faculty of Science Information and Computing Sciences]

CS 320: Concepts of Programming Languages

LECTURE 16. Functional Programming

Principles of Programming Languages 2017W, Functional Programming

GADTs. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 7. [Faculty of Science Information and Computing Sciences]

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Structure and Interpretation of Computer Programs

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

Organization of Programming Languages CS3200/5200N. Lecture 11

6.001: Structure and Interpretation of Computer Programs

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

CompSci 220. Programming Methodology 12: Functional Data Structures

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

Building a system for symbolic differentiation

Functional Programming

Common LISP-Introduction

SCHEME AND CALCULATOR 5b

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

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Chapter 15. Functional Programming Languages

CSE 341 Lecture 16. More Scheme: lists; helpers; let/let*; higher-order functions; lambdas

Lisp. Versions of LISP

Intro. Scheme Basics. scm> 5 5. scm>

GADTs. Wouter Swierstra. Advanced functional programming - Lecture 7. Faculty of Science Information and Computing Sciences

CS 457/557: Functional Languages

You ve encountered other ways of signalling errors. For example, if you lookup an unbound key in a hashtable, Java (and Scala) produce nulls:

A Small Interpreted Language

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Comp 311 Functional Programming. Eric Allen, Two Sigma Investments Robert Corky Cartwright, Rice University Sagnak Tasirlar, Two Sigma Investments

Macros & Streams Spring 2018 Discussion 9: April 11, Macros

Scheme Quick Reference

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

Introductory Scheme. Revision 1

CSCC24 Functional Programming Scheme Part 2

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

Typed Racket: Racket with Static Types

CS558 Programming Languages

CSE 130, Fall 2006: Final Examination

Functional Programming - 2. Higher Order Functions

Scheme Quick Reference

Problem Set 4: Streams and Lazy Evaluation

Lambda Calculus as a Programming Language

COMP 105 Homework: Type Systems

Building a system for symbolic differentiation

Introduction to Programming, Aug-Dec 2006

CS 360: Programming Languages Lecture 10: Introduction to Haskell

C311 Lab #3 Representation Independence: Representation Independent Interpreters

CS61A Midterm 2 Review (v1.1)

Functional Programming. Big Picture. Design of Programming Languages

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Flat (Draft) Pasqualino Titto Assini 27 th of May 2016

Option Values, Arrays, Sequences, and Lazy Evaluation

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

CSE 3302 Programming Languages Lecture 8: Functional Programming

Lisp Basic Example Test Questions

CSCI-GA Final Exam

Programming Principles

Racket Style Guide Fall 2017

CSc 520 Principles of Programming Languages

Documentation for LISP in BASIC

An Introduction to Scheme

Scheme: Strings Scheme: I/O

Transcription:

Exercise 1: Graph coloring (10 points) Graph coloring is the problem of assigning colors to vertices in a non-directed graph, so that no two adjacent nodes have the same color. In other words, if two vertices are adjacent, they must have di erent colors. Each vertex must be assigned exactly one color. Of course, the set of colors is constrained (otherwise we could simply assign a di erent color to each vertex). In this assignment, we will assume there are exactly 3 colors available: Red, Green and Blue. Given the following graph: 1----2----3 / / / / 4----5 We can validly color it with RGB as: 1. Red 2. Green 3. Blue 4. Blue 5. Red The following graph, however, cannot be colored with 3 colors: / \ 1----2 / / / / 3----4 Indeed, the constraints in that graph impose that all four vertices need to have di erent colors, but we only have 3 colors. We represent a graph with the following data structure: final case class Vertex(id: Int, neighbors: List[Int]) final case class Graph(vertices: List[Vertex]) Vertices in the graph all have distinct, strictly positive ids. It is not guaranteed that ids are consecutive integers from 1 to N. It is guaranteed that the neighbors of a Vertex actually exist in the graph (i.e., if neighbors contains an i, then vertices contains a Vertex with id i). Since the graph is non-directed, it is guaranteed that, if the neighbors of a Vertex a contains b, then the neighbors of b contains a. 2

It is not guaranteed that a Vertex is not its own neighbor (note that if that happens, obviously there cannot be any valid coloring for the graph). We want to solve the coloring of a graph: Graph using constraint programming. To do this, we want to generate a set of constraints that encode the properties of a valid coloring for the graph. The complete set of constraints is modeled as a Formula, a data structure defined as follows: sealed abstract class Formula final case class Var(name: String) extends Formula final case class Not(p: Formula) extends Formula final case class And(p: Formula, q: Formula) extends Formula final case class Or(p: Formula, q: Formula) extends Formula final case class Implies(p: Formula, q: Formula) extends Formula A Var is a boolean variable in the constraint. Since we have 3 colors, we cannot simply use Var("1") as the color of vertex 1. Instead, we use one Var per color per vertex: Var("1R") is true if and only if vertex 1 is Red. Similarly, we can have Var("3G") or Var("151B"). Your task: implementthemethod def graphcoloring(graph: Graph): Formula =... which, given an arbitrary non-directed graph Graph, returns a complete Formula encoding all the constraints of the graph coloring problem for that graph. Hint: decompose the problem in smaller functions that generate specific Formulas for some specific parts of the problem constraints. 3

Exercise 2: Streams (10 points) In this task you are going to work on Streams and implement several types of averages on them. For the duration of this task, you can assume that arithmetic operations on Doubles do not su er from loss of precision. Average of last two elements (2 points): Implement a function that given a stream of doubles returns a stream of averages of last two elements. Example: given a stream (1, 1, 4, 5, 7, 0) you should return (1, 2.5, 4.5, 6, 3.5). def pairaverages(data: Stream[Double]): Stream[Double] =??? Average in a arbitrary window (4 points) Implement a function that given a number n and a stream of doubles returns a stream of averages of the last n elements of that stream. Example: given a number 2 and a stream (1, 1, 4, 5, 7) you should return (1, 2.5, 4.5, 6). Example: given a number 3 and a stream (1, 2, 5, 8, 15) you should return (2.67, 5, 9.33) (rounding is only for display here, you should not round yourself). def windowaverage(windowsize: Double, data: Stream[Double]): Stream[Double] =??? Rolling average (4 points) Implement a function that given a stream of doubles returns a stream of averages of all data streamed so-far. Example: given a stream (1, 2, 3, 4, 5) you should return (1, 1.5, 2, 2.5, 3). def rollingaverage(data: Stream[Double]): Stream[Double] =??? 4

Exercise 3: Variable Substitution in Lisp (10 points) After having written your Lisp interpreter, you decide that you want to implement some transformations over your Lisp programs. The first transformation you consider is inlining value definitions. Namely, you want terms of the shape (val name expr rest) to be transformed to some new term rest2 that corresponds to rest where all occurences of name have been replaced by expr. For example, the term (val a 1 (+ a (+ 2 a))) should be rewritten to (+ 1 (+ 2 1)). Scala implementation (5 points) In order to achieve the above task, complete the following function: def substitute(term: Any, symbol: Symbol, replaceby: Any): Any =??? Note that in the example above, you would call substitute(list( +, a, List( +, 2, a)), a, 1) and the expected result is List( +, 1, List( +, 2, 1)) Remember that Lisp terms are of type Any. Composite terms have type List[Any], and variable names are instances of Symbol (you can use == between symbols). You may use any method in the Scala standard library (see appendix for most typical ones). Hint: You can perform type checks in pattern matching by using the following syntax in the case statement: case x: Type => Translation into Lisp (5 points) You now want to implement the same functionality within the Lisp language itself. This means your input now consists of a Lisp list. Complete the following function definition (using the syntactic sugar proposed in the assignment): (def (substitute term symbol replaceby)??? rest) The example above would this time correspond to (substitute (+ a (+ 2 a)) a 1) and your function should output (+ 1 (+ 2 1)) Note: The term (+ a (+ 2 a)) corresponds to the Lisp list 5

(cons + (cons a (cons (cons + (cons 2 (cons a nil))) nil))) The available Lisp API can be found in the appendix. You may use the predicates iscons? and isnil? to determine whether a given term respectively corresponds to an instance of cons or nil. If you wish to use a function that was not defined in the appendix, please provide its definition as well. Hint: Remember Lisp (non-)syntax of prefix notation, for example, comparisons are written as (= a b). Note: Indent your code properly when answering this question, syntax is very important here. 6

Appendix A non-exhaustive (but useful) API Here are some methods from the Scala standard library that you may find useful: on List (containing elements of type A): xs ++ (ys: List[A]): List[A]: appends the list ys to the right of xs, returning a List[A]. xs.apply(n: Int): A, or xs(n: Int): A: returns the n-th element of xs. Throws an exception if there is no element at that index. xs.drop(n: Int): List[A]: returns a List[A] that contains all elements of xs except the first n ones. If there are less than n elements in xs, returnstheemptylist. xs.filter(p: A => Boolean): List[A]: returns all elements from xs that satisfy the predicate p as a List[A]. xs.flatmap[b](f: A => List[B]): List[B]: applies f to every element of the list xs, and flattens the result into a List[B]. xs.foldleft[b](z: B)(op: (B, A) => B): B: applies the binary operator op to a start value and all elements of the list, going left to right. xs.map[b](f: A => B): List[B]: applies f to every element of the list xs and returns a new list of type List[B]. xs.nonempty: Boolean: returns true if the list has at least one element, false otherwise. xs.reverse: List[A]: reverses the elements of the list xs. xs.take(n: Int): List[A]: returns a List[A] containing the first n elements of xs. If there are less than n elements in xs, returnstheseelements. xs.tomap: Map[K, V]: provided A is a pair (K, V), converts this list to a Map[K, V]. xs.zip(ys: List[B]): List[(A, B)]: zips elements of xs and ys in a pairwise fashion. If one list is longer than the other one, remaining elements are discarded. Returns a List[(A, B)]. def sliding(size: Int, step: Int): Stream[Stream[A]]: groups elements in fixed size blocks by passing a sliding window over them. Blocks all contain size elements. step is the distance between the first elements of successive groups. You can use the same API for Stream, replacing List by Stream. on Stream (containing elements of type A): xs #:: (ys: => Stream[A]): Stream[A]: Builds a new stream starting with the element xs, and whose future elements will be those of ys. on Stream (the object): Stream.from(i: Int): Stream[Int]: Creates an infinite stream of integers starting at i. 7

on Map (containing keys of type K, values of type V): mp.get(k: otherwise. K): Option[V]: For a given key k, returnssome(v) if a value exists in the map mp, None Lisp API Here are a few Lisp functions you may find useful: (iscons? term): returns 1 if the provided term is equal to (cons x y) for some x and y, and 0 otherwise. (isnil? term): returns1 if the provided term is equal to nil, and 0 otherwise. (cons x y): constructs a list with head x and tail y (corresponds to :: in Scala). nil: constructs an empty list (corresponds to Nil in Scala). (car x): head of the list x. (cdr x): tail of the list x. (if cond then else): returnsthetermthen if cond DOES NOT evaluate to 0, and else otherwise. You may also find the following conditional construct useful: (cond (test1 expr1)... (testn exprn) (else exprelse)) The (test expr) pairs are successfully considered until a test evaluates to something di erent from 0, in which case the corresponding expr is returned, or until the (else exprelse) pair is reached in which case exprelse is returned. Don t forget the syntax for 1. defining values: (val name body rest) 2. defining functions: (def (name arg1... argn) body rest) 8