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

Similar documents
Introduction to SML Getting Started

Scope and Introduction to Functional Languages. Review and Finish Scoping. Announcements. Assignment 3 due Thu at 11:55pm. Website has SML resources

A quick introduction to SML

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

Functional Programming. Big Picture. Design of Programming Languages

CSCI-GA Scripting Languages

Chapter 15. Functional Programming Languages

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

So what does studying PL buy me?

CPS 506 Comparative Programming Languages. Programming Language Paradigm

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

Functional Programming Languages (FPL)

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

Concepts of Programming Languages

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

CS 314 Principles of Programming Languages

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

CS 314 Principles of Programming Languages

15. Functional Programming

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

Functional Programming

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

Example Scheme Function: equal

The Algol family and ML

Functional Programming

The Algol family and ML

Functional Programming Lecture 1: Introduction

Recap: Functions as first-class values

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Variables and Bindings

Introduction to OCaml

Functional Programming and Haskell

Basic concepts. Chapter Toplevel loop

Chapter 15 Functional Programming Languages

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

OCaml. History, Variants. ML s holy trinity. Interacting with ML. Base type: Integers. Base type: Strings. *Notes from Sorin Lerner at UCSD*

02157 Functional Programming Lecture 1: Introduction and Getting Started

Functional Languages. Hwansoo Han

Chapter 15. Functional Programming Languages

Programming Languages

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Introduc)on To Standard ML

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

Chapter 11 :: Functional Languages

LECTURE 16. Functional Programming

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

Polymorphism and Type Inference

CS 242. Fundamentals. Reading: See last slide

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

If we have a call. Now consider fastmap, a version of map that uses futures: Now look at the call. That is, instead of

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Language Sequence. The Algol Family and ML. Algol 60 Sample. Algol 60. Some trouble spots in Algol 60. Algol Joke. John Mitchell

Topic IV. Block-structured procedural languages Algol and Pascal. References:

Functional Programming Language Haskell

Functional Programming

Introduction. chapter Functions

CMSC 331 Final Exam Section 0201 December 18, 2000

The design of a programming language for provably correct programs: success and failure

Functional Programming. Pure Functional Languages

Lists. Michael P. Fourman. February 2, 2010

Functional Programming

Note that pcall can be implemented using futures. That is, instead of. we can use

Types and Type Inference

Any questions. Say hello to OCaml. Say hello to OCaml. Why readability matters. History, Variants. Plan (next 4 weeks)

Functional Programming. Pure Functional Programming

Programming Languages and Compilers (CS 421)

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Types, Type Inference and Unification

ITT8060 Advanced Programming

Some Advanced ML Features

Functional programming Primer I

F28PL1 Programming Languages. Lecture 11: Standard ML 1

! Broaden your language horizons! Different programming languages! Different language features and tradeoffs. ! Study how languages are implemented

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

Imperative languages

Types II. Hwansoo Han

Semester Review CSC 301

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)

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides.

4/19/2018. Chapter 11 :: Functional Languages

Functional Programming. Pure Functional Languages

Programming Languages Third Edition. Chapter 7 Basic Semantics

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

A Brief Introduction to Scheme (I)

Begin at the beginning

CS558 Programming Languages

CSE3322 Programming Languages and Implementation

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Polymorphism and Type Inference

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

Lambda Calculus and Type Inference

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Lambda Calculus. Variables and Functions. cs3723 1

Continuations provide a novel way to suspend and reexecute

G Programming Languages - Fall 2012

Transcription:

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

Introduction ML Programming Language Functional programming language is one of important programming language paradigms that have been designed for software development, because the program mappings are achieved more directly from input values to output values. Most of functional programs are mainly composed by the expressions, mathematical function or groups of functions. Even through an imperative and object oriented programming languages also have expressions and functions, functional programming language is concentrated on mathematical programming. ML is standard for Meta Language, which is a language that used to describe another languages. History The founder of ML programming language is Robin Milner. He graduated from Cambridge University and has held position at the University of Edinburgh, Stanford University and City University, London. He is a Professor of Theoretical Computer Science and Head of the Department at Cambridge. Milner has made fundamental contributions of information science and particularly to computer programming theory. His Logic for Computable Functions (LCF) is considered a milestone in the history of computer assisted logic, and the instruments is used in the formulation of assertions on programs, programming languages and computing systems. From 1971 to 1980, at Stanford and Edinburgh, Milner developed ML, a special programming language for the formulation of fundamental concepts in computer-assisted logic. In 1983, Luca Cardelli designed an efficient complied ML called Standard ML. Gradually ML was being widely used and become one of the first industry-scale languages which semantic definition is fully formal. ML s dialect and base set of features were standardized as Standard ML (SML) in 1990. And then SML standard was revised in 1997 and language systems that comply with the new standard are called SML'97 implementations. Some of the most popular ML s dialects released, they include SML/NJ, CAML, and Moscow-ML. Features and examples ML is a static scoped functional programming language, like Scheme. The basic types of ML are integer, real, string, char, and Booleans. Programmer can construct objects using tuples, lists, functions and 2

records to create their own structure types of data. A tuple is a sequence of objects with different types. There are some simple ML codes to show different types and how tuple, list and record look like. Example1. -val x= 4; val x= 4 : int -val y= x+3; val y= 7 : int -x*x; val it= 16 : int Example 2. - "Hello World"; val it = "Hello World" : string -(if (2=3) then 5 else 6) +1; val it =7 : int From the example one, the - is called the (primary) prompt, and indicates that the compiler is ready to process input. The val statement binds a name to a value, but the name cannot be later rebound to a new value. The semicolon signifies the end of input. Then compiler can return value of the expression and the type of value. Example two show the string type of Hello World and a simple conditional expression, programmers don t have to declare variables as Pascal and C. you can type expressions and then get return values. When an expression is evaluated at toplevel and not explicitely bound to some identifier, it is automatically bound to the identifier it. Therefore, the value of the last evaluated expression can always be accessed. Tuple: -val x= (2, hi ); val x= (2, hi ); int * string -#1 x; val it = 2 : int -#2 x; val it = "hi" : string Record: - val x= {one = 2, two = "hi"}; val x = {one=2,two="hi"} : {one:int, two:string} - #one x; val it = 2 : int List : - val x = [2,3] ; val x = [2,3] : int list - val y = 1 :: x ; (* this is called consing an element on to a list *) val it = [ 1, 2, 3] : int list - x @ y; ( * appending list *) val it = [ 2, 3, 1, 2, 3] : int list 3

Functional languages treat function as first class values; they can be passed parameters, stored in data structures and returned function results. This is a major impact on programming style. Functions in ML, which can take functions as arguments and/or produce functions as values, are called higher-order functions. To define new operations, function declarations must be used. The declaration form of function is shown below. fun function_name ( formal_parameters ) = function_ body_ expression; eg, fun square( x: int ) = x * x ; Functions can be recursive, which means that is the function can call itself and return values to the original level of function call. The following recursive version of the power function can compute x y given the result of x ˆ y: Function: - fun power (x:int,y:int):int = if (y=0) then 1 else x * power (x,y- 1); val power = fn : int * int -> int - power (3,5); val it = 243 : int - 3 * 3 * 3 * 3 * 3; val it = 243 : int Function: -let val a=1 in = let fun f(x) = x+a in = let val a = 3 in = f( 4 ) = end = end =end; val it = 5 : int Print Print( hi \ n ); hi val it = (); unit note: SML only allows you to print strings. ML is not considered a pure functional language, because it permits side effects of functional evaluation. However, ML supports many of the advanced capabilities expected of a good functional language: recursion, type inference, a good variety of built-in data types, facilities to construct aggregate and functional types, module systems, polymorphism, higher-order functions, and exception handling. ML is a strongly typed programming language. The syntax of ML is more similar to Pascal than LISP. It does not use the parenthesized functional syntax that originated with lambda expression. ML 4

supports polymorphic functions and data types. Data-type polymorphism allows a single type declaration (such as "list") to describe lists of integers, lists of strings, lists of characters, and so on. It means that the types need not to be fixed and is beneficial to write generic functions. ML also contains features of garbage collection or memory management; an automatic deallocation of unreachable data makes programs simpler, cleaner, and more reliable. ML has exception handling and a module facility for implementing abstract data types. Moreover, ML has a powerful compiler, programmers need not write down the type of every variable and function-parameter, because the compiler can usually calculate the type from context. This makes programs more concise and easier to write. Conclusion Finally, ML is a fully functional programming language. It is also widely used as a teaching language and as an advanced programming language for complex non-numerical problems. SML /NJ is a major and latest dialects of ML, it can support different platforms, such as Microsoft Windows, and Unix systems. The SML/NJ system is also maintained for all of the major processor/os combinations, including machines with the MIPS, SPARC, DEC Alpha, and Intel x86 processors. The type system of SML is designed specifically to limit allowable programs to eliminate common errors like buffer overflows and dangling pointers. ML has a reputation for inefficiency when compares with imperative one; however, it becomes more powerful and efficient in mathematical programs after programming language experts developed and enhanced. References: Watt, David A. Programming Language Concepts and Paradigms.Ed. C.A.R Hoare and series Editors Prentice Hall International (UK) Ltd, 1990 Robert W. Sebesta. Concepts of Programming Languages. 3rd ed. CA: Addison-Wesley Publishing Company, Inc.1996. Pucella, Riccardo.Notes on Programming Standard ML of New Jersey version (110.0.6). Department of Computer Science, Cornell University, 2001 The SML/NJ Fellowship. Standard ML of New Jersey. 2003 http://www.smlnj.org/index.html 5