Programming)Languages) and)Techniques (CIS120)
Transcript of Programming)Languages) and)Techniques (CIS120)
![Page 1: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/1.jpg)
Programming Languages and Techniques
(CIS120)
Lecture 6January 27th, 2016
Datatypes and Trees
![Page 2: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/2.jpg)
Announcements• Great job on HW1!
• Homework 2 is available– due Tuesday, February 2nd
• Lecture attendance grade (i.e. clickers)– Flexibility for occasional missed lectures due to minor
emergencies (i.e. it’s OK to miss a few lectures)
• Please complete the CIS 120 Demographics Survey– See Piazza (or this week’s labs)
• Read Chapter 6 and 7
CIS120
![Page 3: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/3.jpg)
Datatypes and Trees
![Page 4: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/4.jpg)
Building Datatypes• Programming languages provide a variety of ways of creating and manipulating structured data
• We have already seen:– primitivedatatypes (int, string, bool, … )– lists (int list, string list, string list list, … )– tuples (int * int, int * string, …)
• Rest of Today: – user-‐defined datatypes– type abbreviations
CIS120
![Page 5: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/5.jpg)
HW 2 Case Study: Evolutionary Trees• Problem: reconstruct evolutionary trees from biological data.
– What are the relevant abstractions?– How can we use the language features to define them?– How do the abstractions help shape the program?
CIS120 Suggested reading: Dawkins, The Ancestor's Tale: A Pilgrimage to the Dawn of Evolution
![Page 6: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/6.jpg)
DNA Computing Abstractions• Nucleotide
– 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
CIS120
![Page 7: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/7.jpg)
Simple User-‐Defined Datatypes• OCaml lets programmers define new datatypes
• The constructors are the values of the datatype– e.g. A is a nucleotide and [A; G; C] is a nucleotide list
CIS120
type nucleotide =| A| C| G| T
type day =| Sunday| Monday| Tuesday| Wednesday| Thursday| Friday| Saturday
type name(must be lowercase)
constructor names (tags)(must be capitalized)
‘type’ keyword
![Page 8: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/8.jpg)
Pattern Matching Simple Datatypes• Datatype values can be analyzed by pattern matching:
• 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)
CIS120
let string_of_n (n:nucleotide) : string =begin match n with| A -> “adenine”| C -> “cytosine”| G -> “guanine”| T -> “thymine”end
![Page 9: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/9.jpg)
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.
CIS120
![Page 10: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/10.jpg)
Type Abbreviations• OCaml also lets us name types without make new
abstractions:
• i.e. a codon is the same thing a triple of nucleotideslet x : codon = (A,C,C)
• Makes code easier to read & write
CIS120
type helix = nucleotide listtype codon = nucleotide * nucleotide
* nucleotide
type name definition in terms of existing types
no constructors!
type keyword
![Page 11: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/11.jpg)
Data-‐Carrying Constructors• Datatype constructors can also carry values
• Values of type ‘measurement’ include:MissingNucCount(A, 3)CodonCount((A,G,T), 17)
CIS120
type measurement =| Missing| NucCount of nucleotide * int | CodonCount of codon * int
Constructors may take a tuple of arguments
keyword ‘of’
![Page 12: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/12.jpg)
Pattern Matching Datatypes • Pattern matching notation combines syntax of tuples and simple datatype constructors:
• Datatype patterns bind variables (e.g. ‘n’) just like lists and tuples
CIS120
let get_count (m:measurement) : int =begin match m with| Missing -> 0| NucCount(_, n) -> n| CodonCount(_, n) -> nend
![Page 13: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/13.jpg)
CIS120
What is the type of this expression?
[A;C]
1. nucleotide2. helix3. nucleotide list4. string * string5. nucleotide * nucleotide6. none (expression is ill typed)
Clickers, please…
type nucleotide = | A | C | G | Ttype helix = nucleotide list
Answer: both 2 and 3
![Page 14: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/14.jpg)
CIS120
What is the type of this expression?
(A, “A”)
1. nucleotide2. nucleotide list3. helix4. nucleotide * string5. string * string6. none (expression is ill typed)
Clickers, please…
type nucleotide = | A | C | G | Ttype helix = nucleotide list
Answer: 4
![Page 15: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/15.jpg)
type tree =| Leaf of helix | Node of tree * helix * tree
Recursive User-‐defined Datatypes • Datatypes can mention themselves!
• Recursive datatypes can be taken apart by pattern matching (and recursive functions).
CIS120
recursive definition
Node carries a tuple of values
base constructor(nonrecursive)
![Page 16: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/16.jpg)
• 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],Node (Leaf [G;C], [G], Leaf []))
Syntax for User-‐defined Types
CIS120
type tree =| Leaf of helix | Node of tree * helix * tree
Constructors(note capitalization)
![Page 17: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/17.jpg)
CIS120
How would you construct this tree in OCaml?
1. Leaf [A;T]2. Node (Leaf [G], [A;T], Leaf [A])3. Node (Leaf [A], [A;T], Leaf [G])4. Node (Leaf [T], [A;T],
Node (Leaf [G;C], [G], Leaf []))5. None of the above
Clickers, please…
[A] [G]
[A;T]
type tree =| Leaf of helix | Node of tree * helix * tree
Answer: 3
![Page 18: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/18.jpg)
CIS120
Have you ever programmed with trees before?
1. yes2. no3. not sure
Clickers, please…
![Page 19: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/19.jpg)
Trees are everywhere
![Page 20: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/20.jpg)
Family trees
CIS120
![Page 21: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/21.jpg)
Organizational charts
CIS120
![Page 22: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/22.jpg)
Game trees
CIS120
![Page 23: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/23.jpg)
Natural-‐Language Parse Trees
CIS120
![Page 24: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/24.jpg)
FilesystemDirectory Structure
CIS120
![Page 25: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/25.jpg)
CIS120
Domain Name Hierarchy
edu com gov mil org net
cornell … upenn cisco…yahoo nasa … nsf arpa … navy …
cis seas wharton …
![Page 26: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/26.jpg)
Binary Trees
A particular form of tree-‐structured data
![Page 27: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/27.jpg)
Binary Trees
CIS120
3
2
0 1
2
3 1
root node
root’s right child
root’s left child
left subtree
leaf node
A binary tree is either empty, or a nodewith at most two children, both of which are also binary trees.
A leaf is a node whose children are both empty.
empty
![Page 28: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/28.jpg)
Binary Trees in OCaml
CIS120
type tree =| Empty| Node of tree * int * tree
3
1 2
4
let t : tree =Node (Node (Empty, 1, Empty),
3,Node (Empty, 2,
Node (Empty, 4, Empty)))
=
![Page 29: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/29.jpg)
Representing trees
CIS120
5
1
0 3
7
9
8
type tree =| Empty| Node of tree * int * tree
Empty
Node (Empty, 0, Empty)
Node (Node (Empty, 0, Empty),1,Node (Empty, 3, Empty))
![Page 30: Programming)Languages) and)Techniques (CIS120)](https://reader031.fdocuments.in/reader031/viewer/2022022010/6210ca1d587ad76dc179ae95/html5/thumbnails/30.jpg)
Demo
see trees.mltreeExamples.ml