Constructors Construct a value of a given type.

Slides:



Advertisements
Similar presentations
More ML Compiling Techniques David Walker. Today More data structures lists More functions More modules.
Advertisements

Modern Programming Languages, 2nd ed.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists A list is a finite sequence of elements. [3,5,9] ["a", "list" ] [] Elements may appear more than once [3,4]
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
A First Look at ML.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function F can return.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Chapter ElevenModern Programming Languages1 A Fourth Look At ML.
A Fourth Look At ML Chapter ElevenModern Programming Languages, 2nd ed.1.
A First Look at ML Chapter FiveModern Programming Languages, 2nd ed.1.
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
5/11/2015IT 3271 Types in ML (Ch 11) datatype bool = true | false; datatype 'element list = nil | :: of 'element * 'element list n Predefined, but not.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
CSE341: Programming Languages Lecture 4 Records (“each of”), Datatypes (“one of”), Case Expressions Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 7 Functions Taking/Returning Functions Dan Grossman Fall 2011.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
Introduction and Chapter 8 of Programming Languages by Ravi Sethi Elements of Functional Programming.
Functional Programming Element of Functional Programming.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Introduction to Functional Programming and ML CS 331 Principles of Programming Languages.
Chapter 9: Functional Programming in a Typed Language.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
F28PL1 Programming Languages Lecture 13: Standard ML 3.
PZ03EX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03EX - ML Programming Language Design and Implementation.
Chapter SevenModern Programming Languages1 A Second Look At ML.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
CSE 341 : Programming Languages Lecture 8 First Class Functions Zach Tatlock Spring 2014.
COMP 412, FALL Type Systems II C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Cs776(Prasad)L6sml971 SML-97 Specifics SML/NJ 110.
Principles of programming languages 12: Functional programming
CSE341: Programming Languages Lecture 7 First-Class Functions
ML: a quasi-functional language with strong typing
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Autumn 2017.
ML Again ( Chapter 7) Patterns Local variable definitions
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2013.
ML Programming Language Design and Implementation (4th Edition)
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2017.
CSE 341 Lecture 3 let expressions; pattern matching Ullman
CSE341: Programming Languages Lecture 7 First-Class Functions
CMSC 202 Lesson 22 Templates I.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2018.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Functions, Patterns and Datatypes
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 7 First-Class Functions
Functions, Patterns and Datatypes
Templates I CMSC 202.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 7 First-Class Functions
Modern Programming Languages
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2019.
PROGRAMMING IN HASKELL
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 7 First-Class Functions
Presentation transcript:

Constructors Construct a value of a given type. Most languages provide kinds: Atomic: 1, 3.14159, “hello”, etc. Compound: apply a function to one or more components: 3::nil (1, 2, 3) {a = “x”, y = 239.0}

Patterns A pattern is one of: + a constant + a variable + a wildcard _ + a compound constructor applied to one or more (sub)patterns

Informal matching algorithm fun matches(left, right) = if left is a constant then if left = right then true else false else if left is a variable then true else if left is a wildcard then true else if left is a compound constructor then if right has same top-level constructor then if (each component of left matches each component of right) then true else false else (* left may not appear in a pattern! *) raise some kind of error

val 3 = 3; val 3 = 4; val _ = “walrus” val (3+5, x) = (8, 9); val ({baz=“hi”, foo=seal}, fish::nil) = ({foo=“bar”, baz=“hi”}, [827]); val [2,3,x] @ nil = 2::3::4::nil;

Patterns and functions - fun silly(0, y) = y | silly(x, y) = 1 + silly(x-1, y); val silly = fn : int * int -> int - val my_tuple = (3,4); val my_tuple = (3,4) : int * int - silly(my_tuple); val it = 7 : int

Example: reverse fun reverse(x) = if null(x) then nil else reverse(tl(x)) @ [hd(x)]; fun reverse(nil) = nil | reverse(x::xs) = reverse(xs) @ [x];

Tail-recursive transformation Create a nested helper function Use parameter(s) in the helper that mean(s) “the work done so far”: fun reverse(x) = let fun rev(nil, rev_so_far) = done | rev(x::xs, rev_so_far) = rev(xs, x::rev_so_far); in rev(x, nil) end;

C++ templates template <class T> class ListNode { private: T* hd; ListNode* tl; public: ListNode(T * head, ListNode * tail) : hd(head), tl(tail) {} T* head() { return hd; } ListNode* tail() { return tl; } };

Template instantiation class ListNode__int__ { private: int* hd; ListNode__int__* tl; public: ListNode__int__(int * head, ListNode__int__ * tail) : hd(head), tl(tail) {} int* head() { return hd; } ListNode__int__* tail() { return tl; } };

Template functions template <class List> int length(List * list) { if (list == NULL) { return 0; } else { return 1 + length(list->tail()); } cout << length< ListNode<int> >(nums); // USAGE

Weird template tricks // Overload length() int length(double x) { return int(x - 10); } class A { // Define strange type public: double tail() { return 10.0; } }; A my_a; // USE: What happens? int result = length<A>(&my_a); cout << result << endl;

Summary: C++ templates C++ templates are more permissive than ML polymorphism: more programs can be written using templates Expressiveness/permissiveness comes at a cost: type checking is less restrictive. Textual substitution incorporates little “semantic” information.