ML Declarations.1 Standard ML Declarations. ML Declarations.2  Area of a circle: - val pi = 3.14159; val pi = 3.14159 : real - fun area (r) = pi*r*r;

Slides:



Advertisements
Similar presentations
Modern Programming Languages, 2nd ed.
Advertisements

A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
ML Declarations.1 Standard ML Declarations. ML Declarations.2 Declarations o Re-declaration of names in ML o The reserved words "val rec" o Pattern Matching.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Programming Languages Section 1 1 Programming Languages Section 1. SML Fundamentals Xiaojuan Cai Spring 2015.
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 =
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.
Chapter 5 ( ) of Programming Languages by Ravi Sethi
Functional Design and Programming Lecture 1: Functional modeling, design and programming.
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.
Introduction to ML You will be responsible for learning ML on your own. Today I will cover some basics Read Robert Harper’s notes on “an introduction to.
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
1. Function prototype Function prototype is a declaration; indicates the function exists Should have function name, return type and parameter Placed before.
Patterns in OCaml 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.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
CPS120: Introduction to Computer Science Functions.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Perl Chapter 6 Functions. Subprograms In Perl, all subprograms are functions – returns 0 or 1 value – although may have “side-effects” optional function.
Computer Science By: Erica Ligons Compound Statement A compound statement- block A compound statement- is a unit of code consisting of zero or more statement.
L what are predefined functions? l what is? n function name n argument(s) n return value n function call n function invocation n nested function call l.
1 Bindings. 2 Outline Preliminaries Scope  Block structure  Visibility Static vs. dynamic binding Declarations and definitions More about blocks The.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
Chapter SevenModern Programming Languages1 A Second Look At ML.
CSE 341 : Programming Languages Lecture 4 Records, Datatypes, Case Expressions Zach Tatlock Spring 2014.
1 Objective Caml (Ocaml) Aaron Bloomfield CS 415 Fall 2005.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2013.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Principles of programming languages 12: Functional programming
C Functions -Continue…-.
ML: a quasi-functional language with strong typing
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Autumn 2017.
Predefined Functions Revisited
CSE 341 Lecture 5 efficiency issues; tail recursion; print
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2013.
slides created by Marty Stepp
Programming Fundamentals Lecture #7 Functions
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2017.
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.
FP Foundations, Scheme In Text: Chapter 14.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2018.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE 341 Section 5 Winter 2018.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
ML’s Type Inference and Polymorphism
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2016.
CSE 341 Lecture 11 b closures; scoping rules
ML’s Type Inference and Polymorphism
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Methods Scope How are names handled?
Presentation transcript:

ML Declarations.1 Standard ML Declarations

ML Declarations.2  Area of a circle: - val pi = ; val pi = : real - fun area (r) = pi*r*r; val area = fn : real -> real - area 2.0; val it = : real Example - Circle Area

ML Declarations.3 Identifiers in ML  val declaration binds a name to a value.  A name can not be used to change its value ! Actually a constant  A name can be reused for another purpose - val pi = "pi"; val pi = "pi" : string  If a name is declared again the new meaning is adopted afterwards - pi; val it = "pi" : string but does not affect existing uses of the name - area(1.0) val it = : real

ML Declarations.4 Is permanence of names a good feature?  LUCKY: redefining a function cannot damage the system or your program.  BUT: redefining a function called by your program may have no visible effect.  NOTE: when modifying a program, be sure to recompile the entire file

ML Declarations.5 'val' and 'val rec'  We can define function using val val sq = fn x => x*x;  What about recursive functions? fun f(n) = if n=0 then 1 else n * f(n-1); val f = fn (n) => if n=0 then 1 else n * ??; val rec f = fn (n) => if n=0 then 1 else n * f(n-1);  'val rec' stands for recursive definition and it is just like 'fun'

ML Declarations.6 Pattern Matching  Patterns can be used to simplify function definition - fun factorial 0 = 1 | factorial n = n * factorial(n-1); val factorial = fn : int -> int  When the function is called, the first pattern to match the actual parameter determines which expression on the right-hand-side will be evaluated.  Patterns can consist Constants - int, real, string, etc... Constructs - tuples, datatype constructs Variables - all the rest Underscore - a wildcard Later …

ML Declarations.7 Pattern Matching  When matching a pattern P to a value X, the matching is done recursively - "from outside to inside".  If matching succeeded, any variable in the pattern is binded with the corresponding value in X  There is no binding where the wildcard is used  Example - fun foo (x,1) = x | foo (1,_) = 0 | foo _ = ~1; val foo = fn : int * int -> int - foo(3,1); val it = 3 : int - foo(1,3); val it = 0 : int - foo(2,2); val it = ~1 : int foo(1,1) = 1 Since matching is done in the order of definition

ML Declarations.8 Patterns in Conditional Expression  Patterns can be used in a case conditional expression case E of P1 => E1 |... | Pn => En - case p-q of 0 => "zero" | 1 => "one" | 2 => "two" | n => if n<10 then "lots" else "lots &lots"; If Pi is the first to match then the result is the value of Ei Equivalent to an expression that defines a function by cases and applies it to E Scope of case : No symbol terminates the case expression! Enclose in parentheses to eliminate ambiguity

ML Declarations.9 Type Abbreviation  You can give new name to existing type: - type vec = real*real; type vec = real * real - infix ++; - fun (x1,y1) ++ (x2,y2) : vec = (x1+x2,y1+y2); val ++ = fn: (real * real) * (real * real) -> vec - (3.6,0.9) ++ (0.1,0.2) ++ (20.0,30.0); (23.7,31.1) : vec  The new name is only an alias - it is acceptable where ever the original name is acceptable and vice versa

ML Declarations.10 Local Declarations in Expressions  let D in E end - fun fraction(n,d)= (n div gcd(n,d), d div gcd(n,d)); val fraction = fn: int*int -> int*int - fun fraction(n,d)= let val com = gcd(n,d) in (n div com, d div com) end; val fraction = fn: int*int -> int*int D may be a compound declaration D1;D2;...;Dn The semicolons are optional  "let D in E end" Can be simulated using anonymous functions - fun fraction(n,d)= (fn com => (n div com, d div com))(gcd(n,d));

ML Declarations.11 Nested Local Declaration  ML allows nested function definitions - fun sqroot a = let val acc=1.0e~10 fun findroot x = let val nextx = (a/x + x)/2.0 in if abs (x-nextx)<acc*x then nextx else findroot nextx end in findroot 1.0 end; val sqroot = fn: real -> real

ML Declarations.12  local D1 in D2 end Behaves like the list D1;D2 in let D1 is visible only within D2  Used to hide a declaration - local fun itfib (n,prev,curr):int = if n=1 then curr else itfib (n-1,curr,prev+curr) in fun fib (n) = itfib(n,0,1) end; val fib = fn : int -> int  Why not simply nest the declaration of itfib within fib ? Local Declarations in Declarations

ML Declarations.13 Comparing let and local  - fun fraction(n,d)= let val com = gcd(n,d) in (n div com, d div com) end;  val fraction = fn: int*int -> real  - local fun itfib (p,prev,curr):int= if p=1 then curr else itfib (p-1,curr,prev+curr) in fun fib (n) = itfib(n,0,1) end; val fib = fn : int -> int

ML Declarations.14 Simultaneous Declarations (collateral)  val Id1 = E1 and... and Idn = En evaluates E1,...,En and only then declares the identifiers Id1,...,Idn  Example: Swapping the values of names val x = y and y = x val (x,y) = (y,x)  Note the last declaration. Actually the allowed format is Val P = E;  So it can be used to disassemble tuples - val a = (1,2,3); val a = (1,2,3) : int * int * int - val (_,x,_) = a; val x = 2 : int their order is immaterial

ML Declarations.15  Example: fun pos d = neg(d-2.0) + 1.0/d and neg d = if d>0.0 then pos(d-2.0)-1.0/d else 0.0; fun sum(d,one)= if d>0.0 then sum(d-2.0,~one)+one/d else 0.0; Mutually Recursive functions

ML Declarations.16 Translating an imperative code to mutually recursive functions  Emulating goto statements... - fun F(x,y,z)=G(x+1,y,z) = and G(x,y,z)=if y<z then F(x,y,z) else H(x,x+y,z) = and H(x,y,z)=if z>0 then F(x,y,z-x) else (x,y,z); val F = fn : int * int * int -> int * int * int val G = fn : int * int * int -> int * int * int val H = fn : int * int * int -> int * int * int - F(0,0,0); val it = (1,1,0) : int * int * int var x:=0; y:=0; z:=0; F: x:=x+1; goto G G: if y 0 then (z:=z-x; goto F) else stop