Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT, www.cs.njit.edu/~elsa/635 www.cs.njit.edu/~elsa/635.

Slides:



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

A First Look at ML.
Chapter 3A Review boolean fun = true; if(fun) System.out.print(“yeah!”);
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.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
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.
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 First Look at ML Chapter FiveModern Programming Languages, 2nd ed.1.
CS 320: Compiling Techniques David Walker. People David Walker (Professor) 412 Computer Science Building office hours: after each.
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 =
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 2: Primitive Data and Definite Loops.
CS 320: Compiling Techniques David Walker. People David Walker (Professor) 412 Computer Science Building office hours: after each.
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.
CSE 341 Lecture 6 exceptions; higher order functions; map, filter, reduce Ullman 5.2, 5.4 slides created by Marty Stepp
Lecture #2, Jan. 10, 2007 Evaluating expressions Defining functions
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CS 320: Compiling Techniques David Walker. People David Walker (Professor) 412 Computer Science Building office hours: after each.
ML Introduction.1 Standard ML Introduction. ML Introduction.2 Includes: Most of the examples Most of the questions Most of the answers... Recommended.
CS235 Languages and Automata Department of Computer Science Wellesley College Introduction to Standard ML Wednesday, September 23, 2009 Reading: Beginning.
Standard ML1 Standard ML: A Quick Tutorial Hossein Hojjat University of Tehran Formal Methods Laboratory.
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 13, 2006 박성우.
10/15/20151 Programming Languages and Compilers (CS 421) William Mansky Based in part on slides by Mattox.
Introduction to Functional Programming and ML CS 331 Principles of Programming Languages.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Introduction to ML CS 331 Principles of Programming Languages revised Spring 2003.
Building Java Programs Primitive Data and Definite Loops.
05/31/101 Programming Languages and Compilers (CS 421)‏ Munawar Hafiz 2219 SC, UIUC Based in part on slides by.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
F28PL1 Programming Languages Lecture 13: Standard ML 3.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
A FIRST LOOK AT ML Chapter Five Modern Programming Languages 1.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
Cs776(Prasad)L6sml971 SML-97 Specifics SML/NJ 110.
7/7/20161 Programming Languages and Compilers (CS 421) Dennis Griffith 0207 SC, UIUC Based in part on slides by Mattox.
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Principles of programming languages 12: Functional programming
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
ML: a quasi-functional language with strong typing
Programming Languages and Compilers (CS 421)
CSE 341: Programming Languages Section 1
CSE 341: Programming Langs
CSE341: Programming Languages Section 1
Functions, Patterns and Datatypes
CSE 341 Section 5 Winter 2018.
Building Java Programs
CSE-321 Programming Languages Introduction to Functional Programming
Functions, Patterns and Datatypes
Building Java Programs Chapter 2
Functions, Patterns and Datatypes
CSE-321 Programming Languages Introduction to Functional Programming
Programming Languages and Compilers (CS 421) #3: Closures, evaluation of function applications, order of evaluation #4: Evaluation and Application.
Madhusudan Parthasarathy (madhu) 3112 Siebel Center
Nate Brunelle Today: Conditional Decision Statements
Functions, Patterns and Datatypes
Building Java Programs Chapter 2
Functions, Patterns and Datatypes
Presentation transcript:

Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,

Copyright 2002 Elsa L. Gunter SML Compiler is on the AFS system at /usr/local/sml/bin/sml A (possibly better, non-PowerPoint) text version of this lecture can be found at For the SML code for today’s lecture see

Copyright 2002 Elsa L. Gunter WWW Addresses for SML ftp://ftp.research.bell- labs.com/dist/smlnj/release/110/110-smlnj.exeftp://ftp.research.bell- labs.com/dist/smlnj/release/110/110-smlnj.exe labs.com/cm/cs/what/smlnj/doc/basis/pages/sml-std- basis.htmlhttp://cm.bell- labs.com/cm/cs/what/smlnj/doc/basis/pages/sml-std- basis.html

Copyright 2002 Elsa L. Gunter Books on SML Supplemental texts (not required) –Elements of ML Programming, by Jeffrey D. Ullman, on Prentice Hall –ML for the Working Programmer, by Lawrence C. Paulson, on Cambridge University Press

Copyright 2002 Elsa L. Gunter Session in SML Standard ML of New Jersey, Version , September 28, 2000 [CM&CMB] - (* Read-eval-print loop; expressions and declarations *) ; val it = 5 : int - val test = 3 < 2; val test = false : bool

Copyright 2002 Elsa L. Gunter SML Expressions and Declarations - (* = At top-level, an expression = ; = is treated as an abbreviation for the declaration = val it = ; =*) - "Hi there"; val it = "Hi there" : string

Copyright 2002 Elsa L. Gunter SML Top-level Expressions and Declarations - (* ^ is string concatenation *) - it ^ " my good friend"; val it = "Hi there my good friend" : string - (* () pronounced "unit" is the result for functions that don’t have a result *) - print "Hello world\n"; Hello world val it = () : unit

Copyright 2002 Elsa L. Gunter Overloading for Basic Arithmetic - val x = 5 + 7; val x = 12 : int - val y = x * 2; val y = 24 : int - val z = ; val z = 1.58 : real

Copyright 2002 Elsa L. Gunter Overloading but No Coercion - val w = y + z; stdIn: Error: operator and operand don't agree [tycon mismatch] operator domain: int * int operand: int * real in expression: y + z

Copyright 2002 Elsa L. Gunter Using SML Code From a File File named test.sml contains 3 + 2; val x = 5 + 7; val y = x * 2; val z = ; val w = y + z;

Copyright 2002 Elsa L. Gunter - use "A:\\SML_Lect\\test.sml"; [opening A:\SML_Lect\test.sml] val it = 5 : int val x = 12 : int val y = 24 : int val z = 1.58 : real Compiler Output

Copyright 2002 Elsa L. Gunter A:\SML_Lect\test.sml: Error: operator and operand don't agree [tycon mismatch] operator domain: int * int operand: int * real in expression: y + z Compiler Output

Copyright 2002 Elsa L. Gunter Booleans (aka Truth Values) - true; val it = true : bool - false; val it = false : bool - if y > x then 25 else 0; val it = 25 : int

Copyright 2002 Elsa L. Gunter Booleans - 3 > 1 andalso 4 > 6; val it = false : bool - 3 > 1 orelse 4 > 6; val it = true : bool - not (4 > 6); val it = true : bool

Copyright 2002 Elsa L. Gunter Functions - fun plus_two n = n + 2; val plus_two = fn : int -> int - plus_two 17; val it = 19 : int - val plus_two = fn n => n + 2; val plus_two = fn : int -> int - plus_two 14; val it = 16 : int

Copyright 2002 Elsa L. Gunter Values fixed at declaration time - val x = 12; val x = 12 : int - fun plus_x y = y + x; val plus_x = fn : int -> int - plus_x 3; val it = 15 : int

Copyright 2002 Elsa L. Gunter Values fixed at declaration time - val x = 7; val x = 7 : int - plus_x 3; val it = 15 : int

Copyright 2002 Elsa L. Gunter Functions with more than one argument - fun add_three (x:int) y z = x + y + z; val add_three = fn : int -> int -> int -> int - val t = add_three 6 3 2; val t = 11 : int

Copyright 2002 Elsa L. Gunter Partial application of functions - val h = add_three 5 4; val h = fn : int -> int - h 3; val it = 12 : int - h 7; val it = 16 : int

Copyright 2002 Elsa L. Gunter Functions as arguments - fun thrice f x = f (f (f x)); val thrice = fn : ('a -> 'a) -> 'a -> 'a - thrice plus_two; val it = fn : int -> int - it 4; val it = 10 : int - thrice (fn s => "Hi! " ^ s) "Good-bye!"; val it = "Hi! Hi! Hi! Good-bye!" : string

Copyright 2002 Elsa L. Gunter Recursive Functions - fun factorial 0 = 1 = | factorial n = n * factorial (n - 1); val factorial = fn : int -> int - factorial 5; val it = 120 : int - - (* fun is needed for recursion function declarations *)

Copyright 2002 Elsa L. Gunter Tuples - val s = (5,"hi",3.2); val s = (5,"hi",3.2) : int * string * real - val (a,b,c) = s; val a = 5 : int val b = "hi" : string val c = 3.2 : real

Copyright 2002 Elsa L. Gunter Tuples - val d = ((1,4,62),("bye",15),73.95); val d = ((1,4,62),("bye",15),73.95) : (int * int * int) * (string * int) * real - val (p,(st,_),_) = d; val p = (1,4,6) : int * int * int val st = "bye" : string

Copyright 2002 Elsa L. Gunter Tuples - fun fst_of_3 (x,_,_) = x; val fst_of_3 = fn : 'a * 'b * 'c -> 'a - s; val it = (5,"hi",3.2) : int * string * real - fst_of_3 s; val it = 5 : int - fst_of_3 d; val it = (1,4,62) : int * int * int

Copyright 2002 Elsa L. Gunter Records - val teacher = {Name = "Elsa L. Gunter", ss = (119,73,6244), age = 102}; val teacher = {Name="Elsa L. Gunter",age=102,ss=(119,73,6244)} : {Name:string, age:int, ss:int * int * int}

Copyright 2002 Elsa L. Gunter Records - val {ss = (s1,s2,s3), Name = elsa, age = years} = teacher; val elsa = "Elsa L. Gunter" : string val years = 102 : int val s1 = 119 : int val s2 = 73 : int val s3 = 6244 : int

Copyright 2002 Elsa L. Gunter Records and Tuples - val q = (280,{student = {Name = "Joseph Martins", = ss = (325,40,1276), = age = 19}, = instructor = teacher});

Copyright 2002 Elsa L. Gunter Records and Tuples val q = (280, {instructor={Name="Elsa L. Gunter",age=102,ss=(119,73,6244)}, student={Name="Joseph Martins",age=19,ss=(325,40,1276)}}) : int * {instructor:{Name:string, age:int, ss:int * int * int}, student:{Name:string, age:int, ss:int * int * int}}

Copyright 2002 Elsa L. Gunter Tuples are Records - val strange = (1,"f",2) = {3 = 2, 2 = "f", 1 = 1};; val strange = true : bool