cse 130 : fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 ·...

71
1

Upload: others

Post on 11-Jun-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

1

Page 2: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

CSE 130 : Fall 2012

Programming Languages

Ranjit JhalaUC San Diego

Lecture 1: Hello, world.

2

Page 3: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

A Programming Language• Two variables

– x, y

• Three operations– x++– x--– (x=0)? L1:L2;

L1: x++; y--; (y=0)?L2:L1L2: …

Fact: This is “equivalent to” to every PL!

Good luck writing quicksort … or Windows, Google, Spotify!

3

Page 4: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

So why study PL ?

“A different language is a different vision of life”

- Federico Fellini

4

Page 5: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

So why study PL ?

Programming language

shapesProgramming thought

5

Page 6: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

So why study PL ?

Language affects how: • Ideas are expressed• Computation is expressed

6

Page 7: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

To Free Your Mind

Course Goals

7

Page 8: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Learn New Languages/Constructs

New ways to:- describe- organize- think about computation

8

Page 9: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Goal: Make Your Programs

• Readable • Correct• Extendable• Modifiable• Reusable

9

Page 10: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Learn How To Learn10

Page 11: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Goal: How to learn new PLs

No Java (C#) 15 (10) years agoAJAX? Python? Ruby? Erlang? F#?...

Learn the anatomy of a PL • Fundamental building blocks• Different guises in different PLs

Re-learn the PLs you already know

11

Page 12: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

To Design New Languages

12

Page 13: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Goal: How to design new PLs…“who, me ?”

Buried in every extensible system is a PL • Emacs, Android: Lisp• Word, Powerpoint: Macros, VBScript• Unreal: UnrealScript (Game Scripting)• Facebook: FBML, FBJS• SQL, Renderman, LaTeX, XML …

13

Page 14: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Choose Right Language 14

Page 15: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Enables you to choose right PL

“…but isn’t that decided by • libraries, • standards, • and my boss ?”Yes.

My goal: educate tomorrow’s tech leaders & bosses, so you’ll make informed choices

15

Page 16: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Speaking of Right and Wrong...

16

Page 17: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Imperative Programming

17

Page 18: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

x = x+1

18

Page 19: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

WTF?x = x+1

19

Page 20: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Imperative = Mutation

20

Page 21: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Imperative = Mutation

Bad!

21

Page 22: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Don’t take my word for it

John Carmack Creator of FPS: Doom, Quake,...

22

Page 23: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Don’t take my word for it

Tim Sweeney (Epic, Creator of UNREAL)

“In a concurrent world,imperative is the wrong default”

23

Page 24: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Functional Programming

24

Page 25: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Func%onal  Programming  ?

No  Assignment.No  Muta%on.No  Loops.

25

Page 26: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

OMG! Who uses FP?!

26

Page 27: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

So,  Who  Uses  FP  ?

MapReduce27

Page 28: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

So,  Who  Uses  FP  ?

Linq,  F#28

Page 29: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

So,  Who  Uses  FP  ?

Erlang29

Page 30: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

So,  Who  Uses  FP  ?

Scala30

Page 31: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

So,  Who  Uses  FP  ?

Wall  Street(all  of  the  above)

31

Page 32: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

So,  Who  Uses  FP  ?

...CSE  13032

Page 33: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Course Mechanics

33

Page 34: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Mechanics

cseweb.ucsd.edu/classes/sp12/cse130-a/

Nothing printed, everything on Webpage!

34

Page 35: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Peer Instruction (ish)

35

Page 36: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Peer Instruction/Clickers• Make class interactive

– Help YOU and ME understand whats tricky

• Clickers Not Optional – Cheap ones are fine– 5% of your grade – Respond to 75% questions

• Seating in groups (links on Piazza)

• Bring laptop if you have one

36

Page 37: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

In Class Exercises 1.  Solo  Vote:  Think  for  yourself,  select  answer

2.  Discuss:  Analyze  Problem  in  Groups  of  3• Prac%ce  analyzing,  talking  about  tricky  no%ons• Reach  consensus• Have  ques%ons,  raise  your  hand!

3.  Group  Vote:  Everyone  in  group  votes• Must  have  same  vote  to  get  points

4.  Class-­‐wide  Discussion:• What  did  you  find  easy/hard?• Ques%ons  from  here  show  up  in  exams

37

Page 38: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Requirements and Grading• The good news: No Homework

• In-Class Exercises: 5%• Midterm: 30%• Programming Assignments (7-8): 30%• Final: 35%

Grading on a curve. Two hints/rumors:1. Lot of work2. Don’t worry (too much) about grade

38

Page 39: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

No Recommended Text

• Online lecture notes

• Resources posted on webpage

• Pay attention to lecture and section!

• Do assignments yourself!

39

Page 40: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Suggested Homeworks

• On webpage after Thursday lecture

• Based on lectures, section of previous Tue, Thu

• Recommended, ungraded, HW problems are sample exam questions

• Webpage has first samples already

40

Page 41: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Weekly Programming AssignmentsSchedule up on webpage

Due on Friday 5 PM

Deadline Extension:– Four “late days”, used as “whole unit”– 5 mins late = 1 late day– Plan ahead, no other extensions

PA #1 online, due 4/13, 5:00 PM

41

Page 42: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Plan

1. FP, Ocaml, 4 weeks2. OO, Scala, 4 weeks 3. Logic, Prolog, 1 week

42

Page 43: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Weekly Programming Assignments

Unfamiliar languages+ Unfamiliar environments

Start Early!

43

Page 44: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Scoring = Style + Test suite

Weekly Programming Assignments

No Compile, No Score

44

Page 45: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Forget Java, C, C++ …… other 20th century PLs

Weekly Programming Assignments

Don’t complain … that Ocaml is hard… that Ocaml is @!%@#

45

Page 46: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

It is not.

Immerse yourself in new language

46

Page 47: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

It is not.

Free

Immerse yourself in new language

your mind.47

Page 48: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Word from our sponsor …• Programming Assignments done ALONE

• We use plagiarism detection software– I am an expert– Have code from all previous classes– MOSS is fantastic, plagiarize at your own risk

• Zero Tolerance– offenders punished ruthlessly

• Please see academic integrity statement

48

Page 49: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

49

Page 50: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

To Ask Me Questions?50

Page 51: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Say hello to OCamlvoid sort(int arr[], int beg, int end){ if (end > beg + 1){ int piv = arr[beg]; int l = beg + 1; int r = end; while (l != r-1){ if(arr[l] <= piv) l++; else swap(&arr[l], &arr[r--]); } if(arr[l]<=piv && arr[r]<=piv) l=r+1; else if(arr[l]<=piv && arr[r]>piv) {l++; r--;} else if (arr[l]>piv && arr[r]<=piv) swap(&arr[l++], &arr[r--]); else r=l-1; swap(&arr[r--], &arr[beg]); sort(arr, beg, r); sort(arr, l, end); }}

Quicksort in C

Quicksort in Ocaml

let rec sort xs = match xs with [] -> [] |(h::t) -> let(l,r)= List.partition ((<=) h) t in (sort l)@h::(sort r)

51

Page 52: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Why readability matters…

sort=:(($:@(<#[),(=#[),$:@(>#[))({~ ?@#))^: (1:<#)

Quicksort in J

52

Page 53: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Say hello to OCaml

Quicksort in OCaml

let rec sort xs = match xs with | [] -> [] | h::t -> let (l,r)= List.partition ((<=) h) t in (sort l)@h::(sort r)

53

Page 54: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Plan (next 4 weeks)

1. Fast forward• Rapid introduction to whats in ML

2. Rewind

3. Slow motion• Go over the pieces individually

54

Page 55: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

ML: History, Variants“Meta Language”Designed by Robin MilnerTo manipulate theorems & proofs

Several dialects:• Standard ML (SML)

– Original syntax • Objective Caml: (Ocaml)

– “The PL for the discerning hacker”– State-of-the-art, extensive library, tool, user support

• F# (Ocaml+.NET) released in Visual Studio

55

Page 56: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

ML’s holy trinity

• Everything is an expression• Everything has a value• Everything has a type

Expression Value

Type

56

Page 57: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Interacting with ML“Read-Eval-Print” Loop

Repeat:1. System reads expression e2. System evaluates e to get value v3. System prints value v and type t

What are these expressions, values and types ?

57

Page 58: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Base type: Integers

Complex expressions using “operators”:(why the quotes ?)

• +, -, * • div, mod

2 2

int

2+2 4

2 * (9+10) 38

2 * (9+10) -12 26

58

Page 59: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Base type: Strings

Complex expressions using “operators”:(why the quotes ?)

• Concatenation ^

“ab” “ab”

string

“ab” ^ “xy” “abxy”

59

Page 60: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Base type: Booleans

Complex expressions using “operators”:• “Relations”: = , <, <=, >= • &&, ||, not

true true

bool

false false

1 < 2 true

“aa” = “pq” false

(“aa” = “pq”) && (1<2) false(“aa” = “aa”) && (1<2) true

60

Page 61: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Type Errors

Untypable expression is rejected• No casting, No coercing• Fancy algorithm to catch errors• ML’s single most powerful feature (why ?)

(2+3) || (“a” = “b”)

(2 + “a”)

“pq” ^ 9

61

Page 62: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Complex types: Product (tuples)

(2+2 , 7>8); (4,false)

int * bool

62

Page 63: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Complex types: Product (tuples)

(9-3,“ab”^“cd”,(2+2 ,7>8)) (6, “abcd”,(4,false))

(int * string * (int * bool))

• Triples,…• Nesting:

– Everything is an expression– Nest tuples in tuples

63

Page 64: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Complex types: Lists[]; [] ’a list

• Unbounded size• Can have lists of anything (e.g. lists of lists)• but …

[1;2;3]; [1;2;3] int list

[“a”;“b”; “c”^“d”]; [“a”;“b”; “cd”] string list

[1+1;2+2;3+3;4+4]; [2;4;6;8] int list

[(1,“a”^“b”);(3+4,“c”)]; [(1,“ab”);(7,“c”)] (int*string) list

[[1];[2;3];[4;5;6]]; (int list) list[[1];[2;3];[4;5;6]];

64

Page 65: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Complex types: Lists

All elements must have same type

[1; “pq”];

65

Page 66: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Complex types: ListsList operator “Cons”

[1] int list

1::[2]; [1;2] int list

1::[“b”; “cd”];

“a”::[“b”;“c”]; [“a”;“b”;“c”] string list

1::[];

::

Can only “cons” element to a list of same type

66

Page 67: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Complex types: Lists

List operator “Append”

int list

[“a”]@[“b”]; [“a”;“b”] string list

1 @ [2;3];

[]@[1]; [1] string list

[1;2]@[3;4;5];

@

Can only append two lists

[1;2;3;4;5]

[1] @ [“a”;“b”];… of the same type

67

Page 68: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Complex types: Lists

List operator “head”

int

hd ([“a”]@[“b”]); “a” string

hd [];

hd [1;2];

hd

Only take the head a nonempty list

1

68

Page 69: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Complex types: Lists

List operator “tail”

int list

tl ([“a”]@[“b”]); [“b”] string list

tl [];

tl [1;2;3];

tl

Only take the tail of nonempty list

[2;3]

69

Page 70: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

Recap: Tuples vs. Lists ?What’s the difference ?• Tuples:

– Different types, but fixed number:

• pair = 2 elts

• triple = 3 elts

• Lists:– Same type, unbounded number:

(3, “abcd”) (int * string)

(3, “abcd”,(3.5,4.2)) (int * string * (float* float))

[3;4;5;6;7] int list

70

Page 71: CSE 130 : Fall 2012cseweb.ucsd.edu/classes/fa12/cse130-a/static/lec1-intro.pdf · 2012-12-04 · CSE 130 : Fall 2012 Programming Languages Ranjit Jhala UC San Diego Lecture 1: Hello,

So far, a fancy calculator…

… what do we need next ?

71