Programming Languages and Techniques (CIS120)

Similar documents
Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Which of the following expressions has the type int list?

Programming Languages and Techniques (CIS120e)

Programming Languages and Techniques (CIS120e)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CIS 194: Homework 3. Due Wednesday, February 11, Interpreters. Meet SImPL

Name: CIS 120e Midterm I Review

Programming Languages and Techniques (CIS120)

CSCI-GA Scripting Languages

Programming Languages and Techniques (CIS120)

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Note that this is a rep invariant! The type system doesn t enforce this but you need it to be true. Should use repok to check in debug version.

Programming Languages and Techniques (CIS120)

CSC/MAT-220: Lab 6. Due: 11/26/2018

6.00 Introduction to Computer Science and Programming Fall 2008

CS558 Programming Languages

CS558 Programming Languages

Programming Languages and Techniques (CIS120)

Types and Programming Languages. Lecture 5. Extensions of simple types

Friday Four Square! 4:15PM, Outside Gates

Introduction to OCaml

Inductive Data Types

Programming Languages

Programming Languages and Techniques (CIS120)

Functional Programming. Introduction To Cool

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

CIS 120 Midterm I February 16, 2015 SOLUTIONS

CSE 130 [Winter 2014] Programming Languages

Inductive data types (I)

Richard Feynman, Lectures on Computation

Functional programming with OCaml. Programming with OCaml. Fall Software Foundations CIS 500. OCaml and this course. Announcements. features.

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

Advanced features of Functional Programming (Haskell)

CIS 551 / TCOM 401 Computer and Network Security. Spring 2006 Lecture 16

CSE : Python Programming

Logistics. Half of the people will be in Wu & Chen The other half in DRLB A1 Will post past midterms tonight (expect similar types of questions)

UCI University of California, Irvine

Programming Languages

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

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

Lecture 26. Introduction to Trees. Trees

Cunning Plan. One-Slide Summary. Functional Programming. Functional Programming. Introduction to COOL #1. Classroom Object-Oriented Language

Chapter 20: Binary Trees

Programming Languages

Trees. Eric McCreath

Lecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions?

CS 457/557: Functional Languages

Lecture 19: Functions, Types and Data Structures in Haskell

CSE 130 Programming Languages. Lecture 3: Datatypes. Ranjit Jhala UC San Diego

BBM 201 Data structures

61A Lecture 3. Friday, September 5

Programming Languages and Techniques (CIS120)

CSE 214 Computer Science II Introduction to Tree

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

Programming Languages and Techniques (CIS120)

Why Do We Need Trees?

Building Java Programs

Recap: ML s Holy Trinity. Story So Far... CSE 130 Programming Languages. Datatypes. A function is a value! Next: functions, but remember.

CSE 130 Programming Languages. Datatypes. Ranjit Jhala UC San Diego

CIS 120 Midterm II November 8, Name (printed): Pennkey (login id):

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

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

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Programming Languages and Techniques (CIS120)

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:

Data Structures and Algorithms

Programming Languages

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

CSE 143 Lecture 19. Binary Trees. read slides created by Marty Stepp

Definition of a tree. A tree is like a binary tree, except that a node may have any number of children

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

TREES Lecture 12 CS2110 Spring 2019

Programming Languages and Techniques (CIS120)

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Recap: ML s Holy Trinity

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Programming Languages and Techniques (CIS120)

Computer Science CSC324 Wednesday February 13, Homework Assignment #3 Due: Thursday February 28, 2013, by 10 p.m.

Lecture 2: Big-Step Semantics

INF121: Functional Algorithmic and Programming

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

61A Lecture 4. Monday, September 9

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

Programming Languages

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

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

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

To figure this out we need a more precise understanding of how ML works

CIS 120 Midterm II November 8, 2013 SOLUTIONS

Transcription:

Programming Languages and Techniques () Lecture 5 January 22, 2018 Datatypes and Trees

Announcements Homework 1 due tomorrow at 11:59pm! Homework 2 available soon due Tuesday, January 30 th Read Chapters 5 7

Recap: Lists, Recursion, & Tuples

Recursive function patterns Recursive functions over lists follow a general pattern: let rec length (l : string list) : int = begin match l with [] -> 0 ( x :: rest ) -> 1 + length rest end let rec contains (l:string list) (s:string) : bool = begin match l with [] -> false ( x :: rest ) -> s = x contains rest s end

Structural Recursion Over Lists Structural recursion builds an answer from smaller components: let rec f (l : list) : = begin match l with [] -> ( hd :: rest ) -> f rest end The branch for [] calculates the value (f []) directly. this is the base case of the recursion The branch for hd::rest calculates (f(hd::rest)) given hd and (f rest). this is the inductive case of the recursion

Given the definition below, which of the following is correct? let rec f (l1:int list) (l2:int list) : int list = begin match l1 with [] -> l2 x::xs -> x :: (f xs l2) end 1. (f [1;2] [3;4]) = [3;4;1;2] 2. (f [1;2] [3;4]) = [1;2;3;4] 3. (f [1;2] [3;4]) = [4;3;2;1] 4. (f [1;2] [3;4]) = [1;3;2;4] 5. none of the above Answer: 2

let rec f (l1:int list) (l2:int list) : int list = begin match l1 with [] -> l2 x::xs -> x :: f xs l2 end f [1; 2] [3;4] 1 :: (f [2] [3;4]) 1 :: 2 :: (f [] [3;4]) 1 :: 2 :: [3;4] = [1;2;3;4]

Design Pattern for Recursion 1. Understand the problem What are the relevant concepts and how do they relate? 2. Formalize the interface How should the program interact with its environment? 3. Write test cases If the main input to the program is an immutable list, make sure the tests cover both empty and non-empty cases 4. Implement the required behavior If the main input to the program is an immutable list, look for a recursive solution Is there a direct solution for the empty list? Suppose someone has given us a partial solution that works for lists up to a certain size. Can we use it to build a better solution that works for lists that are one element larger?

Forms of Structured Data OCaml provides two ways of packaging multiple values together into a single compound value: Lists: arbitrary-length sequence of values of a single, fixed type example: a list of email addresses Tuples: fixed-length sequence of values of arbitrary types example: tuple of name, phone #, and email

What is the type of this expression? (1, [1], [[1]]) 1. int 2. int list 3. int list list 4. (int * int list) list 5. int * (int list) * (int list list) 6. (int * int * int) list 7. none (expression is ill typed) Answer: 5

What is the type of this expression? [ (1,true); (0, false) ] 1. int * bool 2. int list * bool list 3. (int * bool) list 4. (int * bool) list list 5. none (expression is ill typed) Answer: 3

Datatypes and Trees

Building Datatypes Programming languages provide a variety of ways of creating and manipulating structured data We have already seen: primitive datatypes (int, string, bool, ) lists (int list, string list, string list list, ) tuples (int * int, int * string, ) Rest of Today: user-defined datatypes type abbreviations

HW 2 Case Study: Evolutionary Trees Problem: reconstruct evolutionary trees* from DNA data. What are the relevant abstractions? How can we use the language features to define them? How do the abstractions help shape the program? *Interested? Check out this suggested reading: Dawkins: The Ancestor's Tale: A Pilgrimage to the Dawn of Evolution

Nucleotide DNA Computing Abstractions Adenine (A), Guanine (G), Thymine (T), or Cytosine (C) Helix a sequence of nucleotides: e.g. AGTCCGATTACAGAGA genetic code for a particular species (human, gorilla, etc) Phylogenetic tree Binary tree with helices (species) at the nodes and leaves

Simple User-Defined Datatypes OCaml lets programmers define new datatypes type day = Sunday Monday Tuesday Wednesday Thursday Friday Saturday type keyword type nucleotide = A C G T The constructors are the values of the datatype type name (must be lowercase) constructor names (tags) (must be capitalized) e.g. A is a nucleotide and [A; G; C] is a nucleotide list

Pattern Matching Simple Datatypes Datatype values can be analyzed by pattern matching: let string_of_n (n:nucleotide) : string = begin match n with A -> adenine C -> cytosine G -> guanine T -> thymine end There is one case per constructor you will get a warning if you leave out a case or list one twice As with lists, the pattern syntax follows that of the datatype values (i.e. the constructors)

A Point About Abstraction We could represent data like this by using integers: Sunday = 0, Monday = 1, Tuesday = 2, etc. But: Integers support different operations than days do: Wednesday - Monday = Tuesday (?!) There are more integers than days (What day is 17?) Confusing integers with days can lead to bugs Many scripting languages (PHP, Javascript, Perl, Python, ) violate such abstractions (true == 1 == 1 ), leading to pain and misery Most modern languages (Java, C#, C++, OCaml, ) provide user-defined types for this reason.

Type Abbreviations OCaml also lets us name types without making new abstractions: type helix = nucleotide list type codon = nucleotide * nucleotide * nucleotide type keyword type name definition in terms of existing types no constructors! i.e. a codon is the same thing a triple of nucleotides let x : codon = (A,C,C) Makes code easier to read & write

Data-Carrying Constructors Datatype constructors can also carry values type measurement = Missing NucCount of nucleotide * int CodonCount of codon * int keyword of Constructors may take a tuple of arguments Values of type measurement include: Missing NucCount(A, 3) CodonCount((A,G,T), 17)

Pattern Matching Datatypes Pattern matching notation combines syntax of tuples and simple datatype constructors: let get_count (m:measurement) : int = begin match m with Missing -> 0 NucCount(_, n) -> n CodonCount(_, n) -> n end Datatype patterns bind variables (e.g. n ) just like lists and tuples

type nucleotide = A C G T type helix = nucleotide list What is the type of this expression? (A, A ) 1. nucleotide 2. nucleotide list 3. helix 4. nucleotide * string 5. string * string 6. none (expression is ill typed) Answer: 4

Recursive User-defined Datatypes Datatypes can mention themselves! type tree = Leaf of helix Node of tree * helix * tree base constructor (nonrecursive) Node carries a tuple of values recursive definition Recursive datatypes can be taken apart by pattern matching (and recursive functions).

Syntax for User-defined Types type tree = Leaf of helix Node of tree * helix * tree Example values of type tree let t1 = Leaf [A;G] let t2 = Node (Leaf [G], [A;T], Leaf [A]) let t3 = Node (Leaf [T], [T;T], Constructors (note capitalization) Node (Leaf [G;C], [G], Leaf []))

Trees are everywhere

Family trees

Organizational charts

Filesystem Directory Structure

Domain Name Hierarchy edu com gov mil org net cornell upenn cisco yahoo nasa nsf arpa navy cis seas wharton

Game trees

Natural-Language Parse Trees

Binary Trees A particular form of tree-structured data

Binary Trees 3 root node root s left child 2 2 root s right child left subtree 0 1 3 1 leaf node empty A binary tree is either empty, or a node with at most two children, both of which are also binary trees. A leaf is a node whose children are both empty.

Another Example Tree 0 8-1 1 3 1 7 Trees need not be balanced they may be missing some branches.

Drawn Upside Down 0 root node 8-1 1 3 1 7 leaf node

Binary Trees in OCaml type tree = Empty Node of tree * int * tree let t : tree = Node (Node (Empty, 1, Empty), 3, Node (Empty, 2, Node (Empty, 4, Empty))) = 3 1 2 4

Representing trees type tree = Empty Node of tree * int * tree 5 Node (Node (Empty, 0, Empty), 1, Node (Empty, 3, Empty)) 1 7 Node (Empty, 0, Empty) 0 3 9 Empty 8

More on trees see tree.ml treeexamples.ml

Trees as Containers Like lists, trees aggregate ordered data As we did for lists, we can write a function to determine whether the data structure contains a particular element

Searching for Data in a Tree let rec contains (t:tree) (n:int) : bool = begin match t with Empty -> false Node(lt,x,rt) -> x = n (contains lt n) (contains rt n) end This function searches through the tree, looking for n In the worst case, it might have to traverse the entire tree

Search during (contains t 8) 5 1 7 0 3 9 8