Download Programming Languages and Techniques (CIS120) Lecture 3 and more Lecture notes Programming Languages in PDF only on Docsity!
Programming Languages
and Techniques
(CIS120)
Lecture 3
September 6, 2017
Value-Oriented Programming (conFnued)
Lists and Recursion
CIS 120 Announcements
• Homework 1: OCaml Finger Exercises
- Due: Tuesday 9/12 at midnight
- Safari automaFcally unzips submit.zip: see Piazza for how to disable that
• Reading: Please read up to Chapter 3
• QuesFons?
- Post to Piazza (privately if you need to include code!)
- Look at HW1 FAQ
• TA office hours: on course Calendar webpage
• RecitaFons start today!
Review: Value-Oriented Programming
• Ocaml promotes a value-oriented style
- We’ve seen that there are a few commands … print_endline, run_test … but these are used rarely
- Most of what we write is expressions denoFng values
• We can think of an OCaml expression as just a way of
wriFng down a value
• We can visualize running an OCaml program as a
sequence of calcula2on or simplifica2on steps that
eventually lead to this value
CIS
Purity
• The meaning (“denotaFon”) of a pure expression
doesn’t change over Fme:
CIS let atts : int = (attendees 500) let atts2 : int = atts + atts let atts2 : int = (attendees 500)
- (attendees 500) Both ways of compuFng ads2 yield the same value (in contrast with an imperaFve language, which runs adendees’ effects twice.)
FuncFon Calls
Once a funcFon has been declared, it can be invoked
by wriFng the funcFon name followed by a list of
arguments. This is a func2on applica2on expression.
(Note that the list of arguments is not parenthesized.)
CIS total_snds 5 30 22
CalculaFng With FuncFons
- To calculate the value of a funcFon applicaFon, first calculate values for its arguments and then subs2tute them for the parameters in the body of the funcFons. total_snds ( 2 + 3 ) 12 17 ⟼ total_snds 5 12 17 ⟼ ( 5 * 60 + 12 ) * 60 + 17 subst. the args in the body ⟼ ( 300 + 12 ) * 60 + 17 ⟼ 312 * 60 + 17 ⟼ 18720 + 17 ⟼ 18737 CIS let total_secs (hours:int) (minutes:int) (seconds:int) : int = (hours * 60 + minutes) * 60 + seconds
A Design Problem / SituaFon
Suppose we are asked by Penn to design a new email system for noFfying instructors and students of emergencies or unusual events. What should we be able to do with this system? Subscribe students to the list, query the size of the list, check if a parFcular email is enrolled, compose messages for all the list, filter the list to just students, etc.
Design Padern
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
How does the program behave on typical inputs? On unusual ones? On erroneous ones?
4. Implement the behavior
Oien by decomposing the problem into simpler ones and applying the same recipe to each
2. Formalize the interface
- Represent an email by a string (the email address itself)
- Represent an email list using an immutable list of strings
- Represent the collecFon of instructor emails using a toplevel defini2on
- Define the interface to the funcFons: let subscribe (email : string) (lst : string list) : string list = … let length (lst : string list) : int = … let contains (lst : string list) (email : string) : bool = … let instructor_emails : string list = …
3. Write test cases
let l1 : string list = [ "[email protected]"; "[email protected]"; "[email protected]" ] let l2 : string list = [ "[email protected]" ] let l3 : string list = [] let test () : bool = (length l1) = 3 ;; run_test “length l1” test let test () : bool = (length l2) = 1 ;; run_test “length l2” test let test () : bool = (length l3) = 0 ;; run_test “length l3” test Define email lists for tesFng. Include a variety of lists of different sizes and incl. some instructor and non-instructor emails as well.
What is a list?
• Here, the ‘::’ operator constructs a new list from a
head element and a shorter list.
- This operator is pronounced “cons” (for “construct”)
• Importantly, there are no other kinds of lists.
• Lists are an example of an induc2ve datatype.
A list value is either:
[] the empty list, someFmes called nil
or
v :: tail a head value v, followed by a list of the
remaining elements, the tail
Example Lists
To build a list, cons together elements, ending with the
empty list:
1::2::3::4::[] “abc”::”xyz”::[] a list of (four) ints a list of (two) strings (false::[])::(true::[])::[] a list of lists that each contain booleans [] the empty list
Convenient Syntax
Much simpler notaFon: enclose a list of elements in
[ and ] separated by ;
[1;2;3;4] [“abc”;”xyz”] a list of (four) ints a list of (two) strings [[false];[true]] a list of lists that each contain booleans [] the empty list
NOT Lists
These are not lists:
[1;true;3;4] 1:: different element types 2 is not a list 3::[]::[] different element types