CUNY CISC 3160 - Functional Programming Language

Unformatted text preview:

ML Programming LanguageIntroductionPrintConclusionCIS24 Project #3 Student Name: Chun Chung Cheung Course Section: SA Date: 4/28/2003 Professor: Kopec Subject: Functional Programming Language (ML) 1ML Programming Language Introduction 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 2records 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 3Functional 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 4supports 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


View Full Document

CUNY CISC 3160 - Functional Programming Language

Download Functional Programming Language
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Functional Programming Language and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Functional Programming Language 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?