Presentation is loading. Please wait.

Presentation is loading. Please wait.

PZ03EX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 1 PZ03EX - ML Programming Language Design and Implementation.

Similar presentations


Presentation on theme: "PZ03EX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 1 PZ03EX - ML Programming Language Design and Implementation."— Presentation transcript:

1 PZ03EX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 1 PZ03EX - ML Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 4.2.3 Appendix A.7

2 PZ03EX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 2 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.

3 PZ03EX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 3 ML example 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

4 PZ03EX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 4 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

5 PZ03EX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 5 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 [ ].

6 PZ03EX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 6 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;

7 PZ03EX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 7 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


Download ppt "PZ03EX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 1 PZ03EX - ML Programming Language Design and Implementation."

Similar presentations


Ads by Google