Functions, Patterns and Datatypes

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.
A Fourth Look At ML 1. Type Definitions Predefined, but not primitive in ML: Type constructor for lists: Defined for ML in ML datatype bool = true | false;
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
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.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
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.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Data  Consists of constructions that can be inspected, taken apart, or joined to form.
ML: A Strongly Typed Functional Language Interpreted (compilers available): -bash-3.00$ poly Poly/ML 5.4 Release > 1+3; val it = 4 : int >o ^D -bash-3.00.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
String is a synonym for the type [Char].
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.
Introduction to ML – Part 1 Frances Spalding. Assignment 1 chive/fall05/cos441/assignments/a1.ht m
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.
Introduction to ML – Part 1 Kenny Zhu. Assignment 2 chive/fall07/cos441/assignments/a2.ht m
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.
CSE 341 Lecture 6 exceptions; higher order functions; map, filter, reduce Ullman 5.2, 5.4 slides created by Marty Stepp
Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML.
Crash course on SML Wojciech Moczydłowski SML – functional programming language Complete formal semantics Extremely convenient for writing compilers, interpreters,
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
CSE 341 Lecture 10 more about data types; nullable types; option Ullman ; slides created by Marty Stepp
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
CS235 Languages and Automata Department of Computer Science Wellesley College Introduction to Standard ML Wednesday, September 23, 2009 Reading: Beginning.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 13, 2006 박성우.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
Chapter 9: Functional Programming in a Typed Language.
Cs7120 (prasad)L7-TDEF1 Type Definitions. cs7120 (prasad)L7-TDEF2 Concrete Types Primitive types ( int, bool, char, string, etc ) Type constructors (
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
F28PL1 Programming Languages Lecture 13: Standard ML 3.
Overview of the Haskell 98 Programming Language
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
Chapter SevenModern Programming Languages1 A Second Look At ML.
CSED101 INTRODUCTION TO COMPUTING SUM TYPE 유환조 Hwanjo Yu.
A FIRST LOOK AT ML Chapter Five Modern Programming Languages 1.
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.
More Data Types CSCE 314 Spring CSCE 314 – Programming Studio Defining New Data Types Three ways to define types: 1.type – Define a synonym for.
1.SML Docs Standard Basis 2.First-Class Functions Anonymous Style Points Higher-Order 3.Examples Agenda.
String is a synonym for the type [Char].
Principles of programming languages 12: Functional programming
ML: a quasi-functional language with strong typing
ML Again ( Chapter 7) Patterns Local variable definitions
Type Definitions cs776 (prasad) L8tdef.
CMSC 330: Organization of Programming Languages
PROGRAMMING IN HASKELL
CSE 341 Lecture 3 let expressions; pattern matching Ullman
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
Nicholas Shahan Spring 2016
Agenda SML Docs First-Class Functions Examples Standard Basis
Agenda SML Docs First-Class Functions Examples Standard Basis
Functions, Patterns and Datatypes
CSE 341 Section 5 Winter 2018.
CSE S. Tanimoto Introduction to ML
Types and Classes in Haskell
PROGRAMMING IN HASKELL
Functions, Patterns and Datatypes
CSE 341 Section 3 Nick Mooney Spring 2017.
CSE-321 Programming Languages Introduction to Functional Programming
Functions, Patterns and Datatypes
Functions, Patterns and Datatypes
Presentation transcript:

Functions, Patterns and Datatypes Creating New Functions Composition Currying Recursive Functions Patterns Programmer-defined Datatypes CSE 341 -- S. Tanimoto Functions, Patterns , and Types

Composition and Currying in ML Combining two functions to create a new function by composition. h(x) = g(f(x)) ML provides the built in operator “o” Combining a function and one value to create a new function by currying. h(x) = f(a, x) ML does it implicitly. CSE 341 -- S. Tanimoto Functions, Patterns , and Types

CSE 341 -- S. Tanimoto Functions, Patterns , and Types Composition in ML fun f x = x+1; val f = fn : int -> int fun g y = 2*y; val g = fn : int -> int val h = g o f; val h = fn : int -> int h 5; val it = 12 CSE 341 -- S. Tanimoto Functions, Patterns , and Types

CSE 341 -- S. Tanimoto Functions, Patterns , and Types Currying in ML fun add (x,y) = x+y; val add = fn : int * int -> int add (7,2); val it = 9 fun plus x y = x+y; val plus = fn : int -> int -> int plus 7 2; val it = 9; val anotherWeek = plus 7; val anotherWeek = fn : int -> int anotherWeek 2; CSE 341 -- S. Tanimoto Functions, Patterns , and Types

Recursive Functions in ML fun fact n = if n = 0 then 1 else n * fact (n-1); val fact = fn : int -> int fact 5; val it = 120 fact 100; uncaught exception overflow raised at <file stdin> CSE 341 -- S. Tanimoto Functions, Patterns , and Types

Recursive Functions Using Patterns fun fact 0 = 1 | fact n = n * fact(n-1); val fact = fn : int -> int fact 5; val it = 120 CSE 341 -- S. Tanimoto Functions, Patterns , and Types

Reversing a List: Using if fun rev lst = if lst = nil then nil else rev(tl(lst))@[hd(lst)]; val rev : fn ''a list -> ''a list rev [1, 2, 3, 4]; val it = [4, 3, 2, 1] : int list rev [“nice”, “feature”, “recursion”]; val it = [“recursion”, “feature”, “nice”] : string list rev [1.0, 2.0, 3.0]; Error: operator and operand don’t agree (equality type required) CSE 341 -- S. Tanimoto Functions, Patterns , and Types

Reversing a List: Using Patterns fun rev2(nil) = nil | rev2(a::b) = rev2(b)@[a]; val rev2 : fn 'a list -> 'a list rev2 [1, 2, 3, 4]; val it = [4, 3, 2, 1] : int list rev2 [1.0, 1.2, 1.3]; val it = [1.3, 1.2, 1.0] : real list CSE 341 -- S. Tanimoto Functions, Patterns , and Types

Programmer-Defined “Types” Use type to create a synonym or abbreviation for an existing type type piles = int list list; type piles = int list list val somepiles : piles = [[1,2,3],[4,5],[6,7]]; val sompiles = [[1,2,3],[4,5],[6,7]] : piles CSE 341 -- S. Tanimoto Functions, Patterns , and Types

Programmer-Defined “Datatypes” Use datatype to create an entirely new type -- one which could not be expressed using only the basic types, tuples, and lists. datatype color = Red | Yellow | Green; datatype color Green | Red | Yellow val mycolor = Red; val mycolor = Red : color fun okToGo c = if c = Green then true else false; val okToGo fn : color -> bool okToGo(Red); val it = false : bool CSE 341 -- S. Tanimoto Functions, Patterns , and Types

Datatypes with Constructors datatype tnode = L of int | I of int * tnode * tnode | NULL; (A tree node is either a Leaf containing an integer, an interior node containing an int and two tree nodes, or it’s NULL -- representing an empty tree.) CSE 341 -- S. Tanimoto Functions, Patterns , and Types

Working with Datatypes (* tree.ml *) (* Create a binary tree, test whether an element occurs in the tree, and insert an element *) val mytree = I(50,I(25,L(10),L(40)),I(75,L(60),L(90))); fun member (i, NULL) = false | member (i, L(j)) = (i = j) | member (i, I(j, ls, rs)) = if i = j then true else if i < j then member (i, ls) else member (i, rs); CSE 341 -- S. Tanimoto Functions, Patterns , and Types

Working with Datatypes (Cont) fun insert (i, NULL) = L(i) | insert (i, L(j)) = if i = j then L(i) else if i < j then I(j,L(i),NULL) else I(j,NULL,L(i)) | insert (i, I(j, ls, rs)) = if i = j then I(i, ls, rs) else if i < j then I(j, insert(i,ls), rs) else I(j, ls, insert(i, rs)); CSE 341 -- S. Tanimoto Functions, Patterns , and Types