ML Programming Language Design and Implementation (4th Edition)

Slides:



Advertisements
Similar presentations
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Advertisements

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.
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.
RECURSIVE FUNCTIONS. A recursive function is a function that calls itself. It will normally have two parts: 1. A basis for sufficiently small arguments.
PZ05A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ05A - Abstract data types Programming Language Design.
PZ10CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10CX - LISP Programming Language Design and Implementation.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
PZ06BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06BX - Introduction to Smalltalk Programming Language.
Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML.
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Functional Programming Element of Functional Programming.
 The design of the imperative languages is based directly on the von Neumann architecture  Efficiency is the primary concern  Low-level specifications:
CSE S. Tanimoto Introduction to ML 1 Introduction to ML History Special features Interacting with ML ML’s basic types ML’s composite types Math.
Chapter 9: Functional Programming in a Typed Language.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
F28PL1 Programming Languages Lecture 13: Standard ML 3.
Name the United States Coins Count the Pennies 10 ¢
PZ03EX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03EX - ML Programming Language Design and Implementation.
PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06C - Polymorphism Programming Language Design and.
Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3.
COMP313A Functional Programming (1)
1 Alex Proctor and Brian Lee for CSCI 431 at UNCA, Fall 2002 ML (Meta Language) a brief introduction Alex Proctor and Brian Lee For CSCI 431 at the University.
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03BX - Recursive descent parsing Programming Language.
Chapter SevenModern Programming Languages1 A Second Look At ML.
PZ02CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ02CX - Perl Programming Language Design and Implementation.
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03CX - Language semantics Programming Language Design.
Abstract data types Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Functional Programming
Programming Languages and Compilers (CS 421)
Principles of programming languages 12: Functional programming
Types CSCE 314 Spring 2016.
README FILE Programming Languages Design and Implementation
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
ML: a quasi-functional language with strong typing
Dime.
Perl Programming Language Design and Implementation (4th Edition)
Functions and patterns
ML Again ( Chapter 7) Patterns Local variable definitions
A lightening tour in 45 minutes
PZ10CX - LISP Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section Appendix A.6.
PZ06C - Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3 PZ06C.
PZ05A - Abstract data types
Subprograms and Programmer Defined Data Type
FP Foundations, Scheme In Text: Chapter 14.
Name the United States Coins
CSE 341 Section 5 Winter 2018.
CSE S. Tanimoto Introduction to ML
PZ09A - Activation records
PZ04A - Scalar and composite data
PROGRAMMING IN HASKELL
Abstract data types Programming Language Design and Implementation
CSE S. Tanimoto Introduction to ML
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Polymorphism Programming Language Design and Implementation
Functions and patterns
CSE S. Tanimoto Introduction to ML
Functions and patterns
Polymorphism Programming Language Design and Implementation
Abstract data types Programming Language Design and Implementation
CSE S. Tanimoto Introduction to ML
Introduction to Smalltalk
Polymorphism Programming Language Design and Implementation
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Drew Wyborski Programming Languages
Polymorphism Programming Language Design and Implementation
PZ03BX - Recursive descent parsing
Presentation transcript:

ML Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 4.2.3 Appendix A.7

ML overview ML (MetaLanguage) is an applicative language with programs written in the style of C or Pascal. ML was developed by Robin Milner as a mechanism for machine-assisted formal proofs in the Edinburgh Logic for Computable Functions system developed in the mid-1970s ML was also found useful as a general symbol-manipulation language. In 1983, the language was redesigned and extended with concepts like modules to become Standard ML. ML is a language with static types, strong typing and applicative execution of programs, but types need not be specified by the programmer. ML programs consist of several function definitions. Each function is statically typed and may return values of any type. Because it is applicative, variable storage is handled differently from languages like C or FORTRAN. ML supports polymorphism and, through its type system, supports data abstractions.

ML example Figure A.11 in text 1 fun digit(c:string):int = ord(c)-ord("0"); 2 (* Store values as a list of characters *) 3 fun SumNext(V) = if V=[ ] then (print("\n Sum="); 0) 4 else (print(hd(V)); 5 SumNext(tl(V))+digit(hd(V))); 6 fun SumValues(x:string):int= SumNext(explode(x)); 7 fun ProcessData() = 8 (let val infile = open_in("data.sml"); 9 val count = digit(input(infile,1)) 10 in 11 print(SumValues(input(infile,count))) 12 end; 13 print("\n")); Figure A.11 in text

Sample ML reverse a list function datatype list [a, b, c, d, e] hd(x) is head of list or first element tl(x) is tail of list or list without first element x::y means a list with x as head of list, y tail x@y means join list x and y fun reverse(L) = if L = nil then nil else reverse(tl(L)) @ [hd(L)]; Goal: Reduce (reverse list L) to a simpler function: Either argument is nil or operate on head of L and tail of L. Apply reverse function to tail of L and add that to the single list consisting of the head of L

ML pattern matching May also be stated as: fun reverse(x::y) = reverse(y) @ [x] | reverse([ ]) = [ ]; In this format, do a “pattern match” to find which function definition to apply: argument L is not nil so match to x::y; argument L is nil so match to [ ].

ML types Lists: [a, b, c, d, e]  All elements the same type Tuples: (``a'', 1, 2.3)  Shorthand for static records Records: {1=``a'', 2=1, 3=2.3}  Selectors are named How to build dynamic arbitrary structures: datatype money = penny | nickel | dime; val x = penny;  set constant with value Want record of change: datatype change = coins of money * int; coins(penny,3)  object of type change Create functions to operate on change: fun NumPennies(coins(penny,x)) = x; val x = coins(penny, 5); NumPennies(x); val it = 5: int;

ML structures Bags of coins: datatype coinbag = bag of coinbag * coinbag | item of change | empty; fun Valuechange(coins(penny,X)) = X | Valuechange(coins(nickel,X)) = 5* X | Valuechange(coins(dime,X)) = 10* X; fun Countchange(bag(X,Y)) = Countchange(X)+Countchange(Y) | Countchange(item(X)) = Valuechange(X) | Countchange(Empty) = 0; Use of function: val x=bag(item(coins(dime,3)),item(coins(nickel,7))); Countchange(x); val it = 65 : int