CMSC 330: Organization of Programming Languages. Lets, Tuples, Records

Similar documents
Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

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

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

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

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

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CMSC 330: Organization of Programming Languages. Closures (Implementing Higher Order Functions)

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

CMSC 330: Organization of Programming Languages. Closures (Implementing Higher Order Functions)

Programming Languages

Programming Languages

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Programming Languages

Anonymous Functions CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. OCaml Data Types

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

CSE 130 [Winter 2014] Programming Languages

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

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

CS Lecture 7: The dynamic environment. Prof. Clarkson Spring Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

OCaml. History, Variants. ML s holy trinity. Interacting with ML. Base type: Integers. Base type: Strings. *Notes from Sorin Lerner at UCSD*

CMSC 330: Organization of Programming Languages. Rust Basics

CMSC 330, Fall 2013, Practice Problem 3 Solutions

Questions? Static Semantics. Static Semantics. Static Semantics. Next week on Wednesday (5 th of October) no

CS Lectures 2-3. Introduction to OCaml. Polyvios Pratikakis

Array. Prepared By - Rifat Shahriyar

CMSC 631. Functional Programming with OCaml

CSE 341 Section 5. Winter 2018

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

CSCI 2041: Basic OCaml Syntax and Features

Any questions. Say hello to OCaml. Say hello to OCaml. Why readability matters. History, Variants. Plan (next 4 weeks)

Begin at the beginning

Fundamental Concepts and Definitions

So what does studying PL buy me?

CSE 374 Programming Concepts & Tools

Flow Control. CSC215 Lecture

Programming Languages

CS558 Programming Languages. Winter 2013 Lecture 3

CMSC 330: Organization of Programming Languages

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Scope. Chapter Ten Modern Programming Languages 1

Derived and abstract data types. TDT4205 Lecture 15

Programming Languages and Techniques (CIS120)

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

CSCE 314 Programming Languages. Type System

Programming Languages

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CMSC 330: Organization of Programming Languages. Basic OCaml Modules

a data type is Types

An introduction introduction to functional functional programming programming using usin Haskell

Programming Languages and Compilers (CS 421)

A Functional Evaluation Model

CS558 Programming Languages

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

CMSC 330: Organization of Programming Languages. Operational Semantics

Programming Languages

Side note: Tail Recursion. Begin at the beginning. Side note: Tail Recursion. Base Types. Base Type: int. Base Type: int

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

CMSC 330: Organization of Programming Languages

Programming Languages

The Warhol Language Reference Manual

22c:111 Programming Language Concepts. Fall Types I

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Course outline. CSE 341: Programming Languages. Why study programming languages? Course motivation and objectives. 1 lecture: Concepts

Programming Languages and Techniques (CIS120)

QUIZ. Can you find 5 errors in this code?

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

CMSC 330: Organization of Programming Languages. Lambda Calculus

CSE 341, Autumn 2005, Assignment 3 ML - MiniML Interpreter

High Performance Computing in C and C++

CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm

Kurt Schmidt. October 30, 2018

Introduction to OCaml

Section 2.2 Your First Program in Java: Printing a Line of Text

CSE 307: Principles of Programming Languages

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Programming Languages and Techniques (CIS120)

COMP520 - GoLite Type Checking Specification

Programming Languages and Techniques (CIS120)

1 Terminology. 2 Environments and Static Scoping. P. N. Hilfinger. Fall Static Analysis: Scope and Types

CMSC330 Fall 2013 Practice Problems 6 Solutions

Recap: Functions as first-class values

Lecture #23: Conversion and Type Inference

Low-Level Programming in C

QUIZ. What is wrong with this code that uses default arguments?

Names, Scope, and Bindings

Lectures 5-6: Introduction to C

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

1 Lexical Considerations

Transcription:

CMSC 330: Organization of Programming Languages Lets, Tuples, Records CMSC330 Spring 2018 1

Let Expressions Enable binding variables in other expressions These are different from the let definitions we ve been using at the top-level They are expressions, so they have a value Syntax let x = e1 in e2 x is a bound variable e1 is the binding expression e2 is the body expression 2

Let Expressions Syntax let x = e1 in e2 Evaluation Evaluate e1 to v1 Substitute v1 for x in e2 yielding new expression e2 Evaluate e2 to v2 Result of evaluation is v2 Example let x = 3+4 in 3*x Ølet x = 7 in 3*x Ø3*7 Ø21 3

Let Expressions Syntax let x = e1 in e2 Type checking If e1 : t1 and e2 : t (assuming x : t1) Then let x = e1 in e2 : t Example: let x = 3+27 in x*3 3+27 : int x*3 : int (assuming x:int) so let x = 3+27 in x*3 : int 4

Let Definitions vs. Let Expressions At the top-level, we write let x = e;; (* no in e2 part *) This is called a let definition, not a let expression Because it doesn t, itself, evaluate to anything Omitting in means from now on : # let pi = 3.14;; (* pi is now bound in the rest of the top-level scope *) 5

Top-level expressions We can write any expression at top-level, too e;; This says to evaluate e and then ignore the result Equivalent to let _ = e;; Useful when e has an effect, such as reading/writing a file, printing to the screen, etc. let x = 37;; let y = x + 5;; print_int y;; print_string "\n";; When run, outputs 42 to the screen 6

Let Expressions: Scope In let x = e1 in e2, variable x is not visible outside of e2 let pi = 3.14 in pi *. 3.0 *. 3.0;; print_float pi;; error: pi not bound bind pi (only) in body of let (which is pi *. 3.0 *. 3.0) 7

Binding in other languages Compare to similar usage in Java/C let pi = 3.14 in pi *. 3.0 *. 3.0;; pi;; (* pi unbound! *) { float pi = 3.14; pi * 3.0 * 3.0; } pi; /* pi unbound! */ 8

Examples Scope of Let bindings x;; (* Unbound value x *) let x = 1 in x + 1;; (* 2 *) let x = x in x + 1;; (* Unbound value x *) 9

Examples Scope of Let bindings let x = 1 in ( x + 1 + x ) ;; (* 3 *) (let x = 1 in x + 1) ;; x;; (* Unbound value x *) let x = 4 in ( let x = x + 1 in x );; (* 5 *) 10

Shadowing Names Shadowing is rebinding a name in an inner scope to have a different meaning May or may not be allowed by the language C int i; void f(float i) { } { } char *i = NULL;... OCaml let g = 3;; let g x = x + 3;; Java void h(int i) { { float i; // not allowed... } } 11

Let Expressions in Functions You can use let inside of functions for local vars let area r = let pi = 3.14 in pi *. r *. r And you can use many lets in sequence let area d = let pi = 3.14 in let r = d /. 2.0 in pi *. r *. r 12

Nested Let Expressions Uses of let can be nested in OCaml Nested bound variables (pi and r) invisible outside Similar scoping possibilities C and Java let res = (let area = (let pi = 3.14 in let r = 3.0 in pi *. r *. R) in area /. 2.0);; float res; { float area; { float pi = 3.14 float r = 3.0; area = pi * r * r; } res = area / 2.0; } 13

Quiz 1 Which of these is not an expression that evaluates to 3? A. let x=3 B. let x=2 in x+1 C. let x=3 in x D. 3 14

Quiz 1 Which of these is not an expression that evaluates to 3? A. let x=3 ---> not an expression B. let x=2 in x+1 C. let x=3 in x D. 3 15

Quiz 2: What does this evaluate to? let x = 2 in let y = 3 in x + y A. 2 B. 3 C. 4 D. 5 16

Quiz 2: What does this evaluate to? let x = 2 in let y = 3 in x + y A. 2 B. 3 C. 4 D. 5 17

Quiz 3: What does this evaluate to? let x = 6 in let y = 4 in let x = 8 in x = 10-y A. 6 B. true C. 12 D. false 18

Quiz 3: What does this evaluate to? let x = 6 in let y = 4 in let x = 8 in x = 10-y A. 6 B. true C. 12 D. false 19

Quiz 4: What does this evaluate to? let x = 3 in let y = x+2 in let x = 8 in y A. 5 B. 12 C. 10 D. false 20

Quiz 4: What does this evaluate to? let x = 3 in let y = x+2 in let x = 8 in y A. 5 B. 12 C. 10 D. false 21

Tuples Constructed using (e1,..., en) Deconstructed using pattern matching Patterns involve parens and commas, e.g., (p1,p2, ) Tuples are similar to C structs But without field labels Allocated on the heap Tuples can be heterogenous Unlike lists, which must be homogenous (1, ["string1";"string2"]) is a valid tuple 22

Tuple Types Tuple types use * to separate components Type joins types of its components Examples (1, 2) : (1, "string", 3.5) : (1, ["a"; "b"], 'c') : [(1,2)] : [(1, 2); (3, 4)] : [(1,2); (1,2,3)] : 23

Tuple Types Tuple types use * to separate components Type joins types of its components Examples (1, 2) : (1, "string", 3.5) : (1, ["a"; "b"], 'c') : [(1,2)] : [(1, 2); (3, 4)] : [(1,2); (1,2,3)] : int * int int * string * float int * string list * char (int * int) list (int * int) list error Because the first list element has type int * int, but the second has type int * int * int list elements must all be of the same type 24

Pattern Matching Tuples # let plusthree t = match t with (x, y, z) -> x + y + z;; plusthree : int*int*int -> int = <fun> # let plusthree (x, y, z) = x + y + z;; plusthree : int*int*int -> int = <fun> # let addone (x, y, z) = (x+1, y+1, z+1);; addone : int*int*int -> int*int*int = <fun> # plusthree (addone (3, 4, 5));; - : int = 15 Remember, semicolon for lists, comma for tuples [1, 2] = [(1, 2)] which is a list of size one (1; 2) Warning: This expression should have type unit 25

Tuples Are A Fixed Size This OCaml definition # let foo x = match x with (a, b) -> a + b (a, b, c) -> a + b + c;; Would yield this error message This pattern matches values of type 'a * 'b * 'c but is here used to match values of type 'd * 'e Tuples of different size have different types Thus never more than one match case with tuples 27

Records Records: identify elements by name Elements of a tuple are identified by position Define a record type before defining record values type date = { month: string; day: int; year: int } Construct a record { f1=e1; ; fn=en } : evaluates e1 to en, assigns results to the given fields Fields do not have to be written in order # let today = { day=16; year=2017; month= f ^ eb };; today : date = { day=16; year=2017; month= feb };; 28

Destructing Records type date = { month: string; day: int; year: int } let today = { day=16; year=2017; month= feb };; Access by field name or pattern matching print_string today.month;; (* prints feb *) (* patterns *) let { month=_; day=d } = today in let { year } = today in let _ = print_int d in (* prints 16 *) print_int year;; (* prints 2017 *) Notes: In record patterns, you can skip or reorder fields You can use the field name as the bound variable 29

Quiz 5: What does this evaluate to? let get (a,b) y = a+y in get 1 2 A. 3 B. type error C. 2 D. 1 30

Quiz 5: What does this evaluate to? let get (a,b) y = a+y in get 1 2 A. 3 B. type error get s first argument must be a pair C. 2 D. 1 31

Quiz 6: What does this evaluate to? let get (x,y) = match x with (a,b) -> a+y in get (1,2) 1 A. 3 B. type error C. 2 D. 1 32

Quiz 6: What does this evaluate to? let get (x,y) = match x with (a,b) -> a+y in get (1,2) 1 A. 3 B. type error get takes only one argument C. 2 D. 1 33

Quiz 7: What is the type of shift? type point = {x:int; y:int} let shift { x=px; y=py } = {x=px+1; y=py+1};; A. point -> bool list B. int list -> int list C. point -> point D. point -> int list 34

Quiz 7: What is the type of shift? type point = {x:int; y:int} let shift { x=px; y=py } = {x=px+1; y=py+1};; A. point -> bool list B. int list -> int list C. point -> point D. point -> int list 35