Presentation is loading. Please wait.

Presentation is loading. Please wait.

ML Programming Language Design and Implementation (4th Edition)

Similar presentations


Presentation on theme: "ML Programming Language Design and Implementation (4th Edition)"— Presentation transcript:

1 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 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 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) else (print(hd(V)); SumNext(tl(V))+digit(hd(V))); 6 fun SumValues(x:string):int= SumNext(explode(x)); 7 fun ProcessData() = (let val infile = open_in("data.sml"); val count = digit(input(infile,1)) in print(SumValues(input(infile,count))) end; print("\n")); Figure A.11 in text

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 means join list x and y fun reverse(L) = if L = nil then nil else [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 ML pattern matching May also be stated as:
fun reverse(x::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 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 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 "ML Programming Language Design and Implementation (4th Edition)"

Similar presentations


Ads by Google