F28PL1 Programming Languages. Lecture 12: Standard ML 2

Similar documents
F28PL1 Programming Languages. Lecture 14: Standard ML 4

CS 457/557: Functional Languages

Mini-ML. CS 502 Lecture 2 8/28/08

Fall Lecture 3 September 4. Stephen Brookes

Lecture #23: Conversion and Type Inference

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

F28PL1 Programming Languages. Lecture 11: Standard ML 1

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

The type checker will complain that the two branches have different types, one is string and the other is int

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

Products and Records

CISC 1600 Lecture 3.1 Introduction to Processing

Higher-Order Functions

Lecture 19: Functions, Types and Data Structures in Haskell

Australian researchers develop typeface they say can boost memory, that could help students cramming for exams.

SML A F unctional Functional Language Language Lecture 19

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

Inductive Data Types

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

CS558 Programming Languages

CS558 Programming Languages

Functional Programming in Haskell Part I : Basics

CS558 Programming Languages

Currying fun f x y = expression;

Lecture 3: Recursion; Structural Induction

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

15 150: Principles of Functional Programming. More about Higher-Order Functions

Redefinition of an identifier is OK, but this is redefinition not assignment; Thus

Recap: Functions as first-class values

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

Introduction to ML. Based on materials by Vitaly Shmatikov. General-purpose, non-c-like, non-oo language. Related languages: Haskell, Ocaml, F#,

OBJECTIVE QUESTIONS: Choose the correct alternative:

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

Lecture 2: The Basis of SML

Standard ML. Curried Functions. ML Curried Functions.1

Fall 2018 Stephen Brookes. LECTURE 2 Thursday, August 30

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

How to Design Programs

Begin at the beginning

An introduction introduction to functional functional programming programming using usin Haskell

Lists. Michael P. Fourman. February 2, 2010

Exercises on ML. Programming Languages. Chanseok Oh

Introduction to SML Getting Started

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

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

Programming Languages

CSCI-GA Scripting Languages

CS1 Recitation. Week 2

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

ML Built-in Functions

Lecture Notes on Induction and Recursion

AMCAT Procedure functions and scope Sample Questions

Variables and Bindings

Chapter 3 Linear Structures: Lists

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Module 8: Local and functional abstraction

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

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

Haskell 98 in short! CPSC 449 Principles of Programming Languages

A general introduction to Functional Programming using Haskell

Part I. Language Core

Programming Paradigms Languages F28PL, Lecture 1

CSCI-GA Final Exam

CSE341, Fall 2011, Midterm Examination October 31, 2011

Introduction to OCaml

Chapter 3 Linear Structures: Lists

CIS24 Project #3. Student Name: Chun Chung Cheung Course Section: SA Date: 4/28/2003 Professor: Kopec. Subject: Functional Programming Language (ML)

A Fourth Look At ML. Chapter Eleven Modern Programming Languages, 2nd ed. 1

Programming Paradigms

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

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

Handout 2 August 25, 2008

Names and Functions. Chapter 2

Polymorphism and Type Inference

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

Announcements. CSCI 334: Principles of Programming Languages. Lecture 5: Fundamentals III & ML

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

Introduction to Scientific Programming with C++

CIS 120 Midterm I February 16, 2015 SOLUTIONS

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Functional Programming

Functional Programming Languages (FPL)

SMURF Language Reference Manual Serial MUsic Represented as Functions

Wellesley College CS251 Programming Languages Spring, 2000 FINAL EXAM REVIEW PROBLEM SOLUTIONS

Introduction to Functional Programming: Lecture 7 2 Functions as innite data structures Ordinary ML data structures are always nite. We can, however,

The reader is referred to the Objective Caml reference manual for a more detailed description of these features.

G Programming Languages - Fall 2012

Datatype declarations

VARIABLE, OPERATOR AND EXPRESSION [SET 1]

Notes on the 2008 Exam

CS153: Compilers Lecture 15: Local Optimization

Crayon (.cry) Language Reference Manual. Naman Agrawal (na2603) Vaidehi Dalmia (vd2302) Ganesh Ravichandran (gr2483) David Smart (ds3361)

ITT8060: Advanced Programming (in F#)

All the operations used in the expression are over integers. a takes a pair as argument. (a pair is also a tuple, or more specifically, a 2-tuple)

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

Lecture 2: SML Basics

Tutorial 7. Number Colour Black Brown Red Orange Yellow Green Blue Violet Gray White

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

Transcription:

F28PL1 Programming Languages Lecture 12: Standard ML 2

Declaration introduce a variable associate identifier with value - val identifier = expression; > val identifier = value : type identifier - any sequence of letters, digits and _ starting with a letter use identifier in subsequent expressions to return value

Declaration - val topound = 0.66; > val topound = 0.66 : real - 27.45*toPound; > 18.117 : real - val employee = ( Duncan, Thaw,19000.0); > val employee = ( Duncan, Thaw,19000.0): string * string * real - val paypounds = #3 employee*topound; > val paypounds = 12540.0 : real

Declaration can t assign new value to declared variable can redeclare variable by entering new definition - val topound = 0.68; > val topound = 0.68 : real creates a new instance of the variable does not assign to existing variable

Function function mapping from a domain to a range domain - formal parameters range - result function type displayed as fn : domain type -> range type

Function can display types of prefix system functions - size; > fn : string -> int i.e. size takes a string and returns an int - not; > fn : bool -> bool i.e. not takes a bool and returns an int

Function definition fun identifier pattern = expression; identifier name associated with function pattern formal parameters to begin with, an identifier expression function body

Function definition system shows function type can t display function value - fun ident pattern = expression; > val ident = fn : type1 -> type2 type1 - type for pattern - domain type2 - type for expression - range

Function definition system uses polymorphic type checking to deduce types from use of operators/values of known type e.g. increment an integer - fun inc x = x+1; > val inc = fn : int -> int 1 is int so... + must be int addition so... x must be int

Function definition e.g. put s on end of a string - fun plural w = w^ s ; > val plural = fn : string -> string ^ takes 2 strings so... w must be string ^ returns a string so... w must be string and s must be string

Function definition e.g. given string, return it with its length - fun ssize s = (s,size s); > val ssize = fn : string -> string * int size takes a string so... s must be string size returns int

Explicit formal parameter types sometimes can t deduce type in presence of overloaded operators e.g. square an integer fun sq x = x*x can t tell if * is int or real use explicitly typed pattern: (pattern:type) - fun sq (x:int) = x*x; > val sq = fn : int -> int

Function call must call a type1 -> type2 function with a type1 actual parameter to return a type2 result - ident expression1; > value2 : type2 system: 1. evaluates expression1 to give value1 of type type1 2. finds type1 -> type2 function associated with ident 3. replaces s idents formal parameter with value1 in idents body 4. evaluates idents body to return value2 of type2

Function call substitution model of function call - inc 3; > 4 : int inc 3 ==> 3+1 ==> 4 - plural banana ; > bananas : string plural banana ==> banana ^ s ==> bananas

Function call - ssize fish ; > ( fish,4) : string * int ssize fish ==> ( fish,size fish ) ==> ( fish,4) - sq 7; > 49 : int sq 7 ==> 7*7 ==> 49

Multiple arguments fun ident patt1 patt2... pattn = exp; if: patt1 : type1... pattn : typen exp : typen+1 then function type is: type1 ->... -> typen -> typen+1

Multiple arguments e.g. sum of squares - fun sumsq x y = sq x+sq y; > val sumsq = fn : int -> int -> int sq is int -> int so... x must be int sq is int -> int so... y must be int sq x is int and sq y is int so... + must be int addition

Multiple arguments - sumsq 3 4; > 25 : int sumsq 3 4 ==> sq 3+sq 4 ==> 9+16 ==> 25

Multiple arguments e.g. does a string have more than n letters? - fun ismore s n = size s > n; > val ismore : fn string -> int -> bool size takes a string so... s is string size s returns an int so... > is int comparison so... n is int > returns a bool

Multiple arguments ismore hello 6; > false : bool ismore hello 6 ==> size hello > 6 ==> 5 > 6 ==> false

Multiple cases can define functions with multiple cases fun ident patterns1 = expression1 ident patterns2 = expression2... patterns is one or more pattern pattern may be constant identifier patterns must be of same types expressions must be of same type

Multiple cases for function call: actual parameters matched against each patterns in turn when all match succeeds, value of corresponding expression returned match: constant - must be same as actual parameter value identifier - identifier in body expression replaced with actual parameter should have a case for all possible combinations of patterns values

Multiple cases e.g. is integer 0? - fun iszero 0 = true iszero x = false; > val iszero = fn : int -> bool 0 is int so... x must be int true and false both bool so... result is bool

Multiple cases - fun iszero 0 = true iszero x = false; > val iszero = fn : int -> bool - iszero 0; > true : bool 0 matches 0 ==> true - iszero 3; > false : bool 3 doesn t match 0 3 matches x ==> false

Multiple cases e.g. rainbow colour sequence - fun next red = orange next orange = yellow next yellow = green next green = blue next blue = indigo next indigo = violet next s = s^ : not a valid colour ; > val next = fn : string -> string red, orange... all string so s must be string all results are string

Multiple cases - next blue ; > indigo : string blue doesn t match red blue doesn t match orange... blue matches blue ==> indigo

Multiple cases - next banana ; > banana not a valid colour : string banana doesn t match red... banana matches s ==> banana ^ not a valid colour ==> banana: not a valid colour

Wildcard pattern _ - matches anything and ignores it e.g. don t use x on right hand side of 2nd case of iszero - fun iszero 0 = true iszero _ = false; > val iszero = fn : int -> bool - iszero 22; > false : bool 22 doesn t match 0 22 matches _ ==> false

Non-sequential conjunction - fun AND false false = false AND false true = false AND true false = false AND true true = true; > val AND = fn : bool -> bool -> bool - AND true false; > false : bool true false doesn t match false false true false doesn t match false true true false matches true false ==> false

Non-sequential conjunction if 1st actual parameter is false then result is false if 1st actual parameter is true then result is same as second actual parameter simplify: - fun AND false _ = false AND true y = y; > val it AND = fn : bool -> bool -> bool

Recursion 1 recursion is functional programming equivalent of iteration in imperative language usually recurse across a range base case at end of range function returns a value recursion case function combines result of processing next in range with result of processing rest of range i.e. recursion case makes progress towards base case

Recursion 1 for integers, range is often from some positive value, say n, to 0 base case: n=0 ==> return value recursion case: n>0 ==> process n and recurse with n-1 e.g. sum integers from 0 to n base case: n=0 ==> sum is 0 recursion case: n>0 ==> add n to summing from 0 to n-1

Recursion 1: integer - fun sum 0 = 0 sum n = n+sum (n-1); > val sum = fn : int -> int 0 is int so n must be int 0 is int so... n+sum (n-1) must be int so... + must be int addition so... n must be int (!) and sum (n-1) must be int

Recursion 1: integer - sum 3; > 6 : int sum 3 ==> 3+sum 2 ==> 3+(2+sum 1) ==> 3 + (2+(1+sum 0)) ==> 3+(2+(1+0)) ==> 6

Recursion 1: integer e.g. find 2 to the power n base case: n=0 ==> 1 recursion case: n>0 ==> multiply 2 by 2n-1 - fun pow2 0 = 1 pow2 n = 2*pow2 (n-1); > val pow2 = fn : int -> int 0 is int so n must be int 0 is int so * must be int multiplication so 2 must be int and pow2 (n-1) must be int

Recursion 1: integer - pow2 3; > 8 : int pow2 3 ==> 2*pow2 2 ==> 2*(2*pow2 1) ==> 2*(2*(2*pow2 0)) ==> 2*(2*(2*1)) ==> 8

Recursion 1: integer e.g. find x to the power n base case: n=0 ==> 1 recursion case: n>0 ==> mutiply x by xn-1 - fun power _ 0 = 1 power x n = x*power x (n-1); > val power = fn : int -> int -> int 1 is int so x*power x (n-1) must be int so * must be int multiplication so x must be int 0 is int so n must be int

Recursion 1: integer - power 3 3; > 27 : in power 3 3 ==> 3*power 3 2 ==> 3*(3*power 3 1) ==> 3*(3*(3*power 3 0)) ==> 3*(3*(3*1)) ==> 27

Recursion 1 e.g. make a string with n. s base case: n=0 ==> recursion case: n>0 ==> join. to n-1. s - fun stops 0 = stops n =. ^stops (n-1); > val stops = fn : int -> string 0 is int so n must be int is string so. ^stops (n-1) must be string so ^ must return string (!) so. must be string (!) so stops (n-1) must be string

Recursion 1: integer - stops 3; >... : string stops 3 ==>. ^stops 2 ==>. ^(. ^stops 1) ==>. ^(. ^(. ^stops 0)) ==>. ^(. ^(. ^ )) ==>...

Recursion 1: integer e.g. make a string with n lots of string s base case: n=0 ==> recursion case: n>0 ==> join s to n-1 s s - fun sjoin _ 0 = sjoin s n = s^sjoin s (n-1); > val sjoin = fn : string -> int -> string

Recursion 1: integer - sjoin 3 run ; > runrunrun : int sjoin 3 run ==> run ^sjoin 2 run ==> run ^( run ^sjoin 1 run ) ==> run ^( run ^( run ^sjoin 0 run )) ==> run ^( run ^( run ^ )) ==> runrunrun

Partial application 1 apply a function of n formal parameters to m<n-1 actual parameters forms a new function of n-m formal parameters like original function with formal parameters 1..m frozen to actual parameters 1..m

Partial application 1 - fun f a b c d =... > val f = fn : type1 -> type2 ->... typen - val aval =...; > val aval =...: type1 - val f = f aval; > val f : type2 ->... typen f is like f with a frozen to the value of aval

Partial application 1 e.g. powers - fun power _ 0 = 1 power x n = x*power x (n-1); > val power = fn : int -> int -> int - val pow2 = power 2; > val pow2 = fn : int -> int pow2 is like power with x frozen to 2 - pow2 3; > 8 : int pow2 3 ==> power 2 3 ==>... 8

Partial application 1 e.g. sjoin - fun sjoin _ 0 = sjoin s n = s^sjoin s (n-1); > val sjoin = fn : string -> int -> string - val stops = sjoin. ; > val stops = fn : int -> string stops is like sjoin with s frozen to. - stops 3; >... : string stops 3 ==> sjoin. 3 ==>......

Conditional expression pattern matching with constant can detect presence or absence of value can t check properties of value conditional expression if expression1 then expression2 else expression3 expression1 must return bool expression2 and expression3 must return same type

Conditional expression e.g. find absolute value of integer - fun abs x = if x>=0 then x else ~x; > val abs = fn : int -> int

Conditional expression e.g. find bigger of two integers - fun max (x:int) y = if x>y then x else y; > val max = fn : int -> int -> int NB > is overloaded so must make type of one of x or y explicit