用ML編寫正確的程序MLProgrammingCorrectly_第1頁
用ML編寫正確的程序MLProgrammingCorrectly_第2頁
用ML編寫正確的程序MLProgrammingCorrectly_第3頁
用ML編寫正確的程序MLProgrammingCorrectly_第4頁
用ML編寫正確的程序MLProgrammingCorrectly_第5頁
已閱讀5頁,還剩20頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、用ML編寫正確的程序ML Programming Correctly汪旻51103790361函數(shù)式程序設計過程式語言面向命令,函數(shù)式語言面向表達式函數(shù)式語言避免繁瑣的內存管理函數(shù)式語言要求寫出可靠正確的程序函數(shù)式語言的幾個概念函數(shù),遞歸,模式匹配,多態(tài)類型檢測,高階函數(shù),無窮數(shù)據結構,惰性求值2ML與純函數(shù)式編程語言的區(qū)別ML是非純函數(shù)式語言熱情求值沒有無窮數(shù)據結構模塊多態(tài)類型檢測異常處理簡單的IO指令3ML的誕生ML(Meta-Language) 是一個通用的函數(shù)式編程語言,它是由愛丁堡大學的Robin Milner在二十世紀七十年代晚期開發(fā)的,現(xiàn)在流行的方言有Standard ML和Ca

2、ml.啟發(fā)語言:ISWIM影響語言:Miranda, Haskell, Cyclone, C+, F#, Clojure, Felix, Opa, Erlang 設計初衷:LCF定理證明機中尋找證明策略ML大多被用于語言設計和操作(編譯器、分析器、定理證明機), 但是它作為通用語言也被用于生化,金融系統(tǒng),和宗譜數(shù)據庫,一個P2P的客戶服務器程序等等。4Level 0Hello world“Hello world!”; (*make a string*)print it; (*output it*)階乘函數(shù)fun fac 0 = 1 | fac n = n * fac (n - 1);LV.0M

3、L Programmer猿LV.1ML ProgrammerLevel Up!5Level 1值的聲明命名常量val pi = 3.14159;聲明函數(shù)fun circle_area (r) = pi*r*r;標識符字母開頭,允許數(shù)字、下劃線、撇號!%&$#+_*/:?|6Level 1基本類型數(shù)int, real + - * div mod 負號類型約束字符串連接符#”0”和“0”真值orelse, andalso, notif E then E1 else E2- fun square x = x*x;- fun square x = x*x :real;7Level 1序偶、元組、記錄序

4、偶向量(2.5 , 1.2) : real*realtype vec = real * real;記錄val ZS = name=“Zhang San”, age=20, major=“CS”;#age ZS; 20 : intLV.1ML Programmer猿LV.2ML ProgrammerLevel Up!8Level 2表達式中綴操作符(infix operator)及其優(yōu)先級、結合方式傳值調用(add.)局部聲明let . in . endlocal . in . end支持嵌套9(* Example for infix operator *)infix xor;fun (p xo

5、r q) = (p orelse q) andalso not (p andalso q);(* 中綴優(yōu)先級 *)infix 7 times;(* 傳值調用 *)sqr(sqr(sqr(2)=sqr(sqr(2*2)=sqr(sqr(4)=sqr(4*4)=sqr(16)=16*16=256 .zero(sqr(sqr(sqr(2)=zero(sqr(sqr(2*2)=zero(sqr(sqr(4)=zero(sqr(4*4)=zero(sqr(16)=zero(16*16)=zero(256)=010(* Example for let . in . end *)fun fraction (

6、n,d) = let val com = gcd(n,d) in (n div com, d div com) end; val fraction = fn : int * int - int * int(* Example for local . in . end *)local fun itfib (n, prev, curr) : int = if n=1 then curr else itfib (n-1, curr, prev+curr)in fun fib (n) = itfib(n, 0, 1)end; val fib = fn ; int - intLV.2ML Program

7、mer猿LV.3ML ProgrammerLevel Up!11 Level 3模塊系統(tǒng)結構通過Conplex.zero這樣的形式來訪問不同結構里的標識符可以相同簽名12structure Complex : ARITH = struct type t = real*real; val zero = (0.0, 0.0); fun sum (x,y), (x,y) = (x+x, y+y) : t; fun diff (x,y), (x,y) = (x-x, y-y) : t; fun prod (x,y), (x,y) = (x*x - y*y, x*y + x*y) : t; fun re

8、cip (x,y) = let val t:real = x*x + y*y in (x/t, y/t) end fun quo (z,z) = prod(z, recip z); end;signature ARITH = sig type t val zero : t val sum : t * t - t val diff : t * t - t val prod : t * t - t val quo : t * t - t end;LV.3ML Programmer猿LV.4ML ProgrammerLevel Up!13Level 4多態(tài)類型檢測多態(tài)類型的聲明關于算法(Damas(

9、1985)的博士論文)- fun pairself x = (x,x); val pairself = fn : a - a * afun fst (x,y) = x; val fst = fn : a * b - afun snd (x,y) = y; val snd = fn : a * b - bLV.4ML Programmer猿LV.5ML ProgrammerLevel Up!14Level 5List順序 3, 4可重復 3, 4, 3元素類型任意 (1,”one”),(2,”two”),(3,”three”) : (int*string) list: 右結合中綴操作符cons

10、在表前加入一個元素_通配符LV.5ML Programmer猿LV.6ML ProgrammerLevel Up!15Level 6數(shù)據類型聲明datatype person = King | Peer of string*string*int | Knight of string | Peasant of string;fun title King = His Majesty the King“ | title (Peer(deg,terr,_) = The deg of terr | title (Knight name) = Sir name | title (Peasant name)

11、 = name;16Level 6樹節(jié)點=節(jié)點+子樹or葉子datatype a tree = Lf | Br of a tree * a tree;size(tree)應用17val tree2 = Br(2, Br(1,Lf,Lf), Br(3,Lf,Lf);val tree5 = Br(5, Br(6,Lf,Lf), Br(7,Lf,Lf);val tree4 = Br(4, tree2, tree5);421356718(* make a tree *)datatype a tree = Lf | Br of a * a tree * a tree;structure Tree = s

12、tructfun size Lf = 0 | size (Br(v,t1,t2) = 1 + size t1 + size t2;fun size Lf = 0 | size (Br(v,t1,t2) = 1 + size t1 + size t2;fun depth Lf = 0 | depth (Br(v,t1,t2) = 1 + Int.max (depth t1, depth t2); fun reflect Lf = Lf | reflect (Br(v,t1,t2) = Br(v, reflect t2, reflect t1);.end;19(* 二叉查找樹 *)(* Dicti

13、onaries as Binary search trees *)signature DICTIONARY = sig type key(*type of keys*) type a t(*type of tables*) exception E of key(*errors in lookup, insert*) val empty: a t(*the empty dictionary*) val lookup: a t * key - a val insert: a t * key * a - a t val update: a t * key * a - a t end;(*Struct

14、ure Order can vary; Tree avoids referring to a free structure. *)structure Dict : DICTIONARY = struct type key = string; type a t = (key * a) tree; exception E of key; val empty = Lf; 20fun lookup (Lf, b) = raise E b | lookup (Br (a,x),t1,t2), b) =(case Spare(a,b) of GREATER = lookup(t1, b) | EQUAL

15、= x | LESS = lookup(t2, b); fun insert (Lf, b, y) = Br(b,y), Lf, Lf) | insert (Br(a,x),t1,t2), b, y) =(case Spare(a,b) of GREATER = Br (a,x), insert(t1,b,y), t2) | EQUAL = raise E b | LESS = Br (a,x), t1, insert(t2,b,y); fun update (Lf, b, y) = Br(b,y), Lf, Lf) | update (Br(a,x),t1,t2), b, y) =(case

16、 Spare(a,b) of GREATER = Br (a,x), update(t1,b,y), t2) | EQUAL = Br (a,y), t1, t2) | LESS = Br (a,x), t1, update(t2,b,y); end;LV.6ML Programmer猿LV.7ML ProgrammerLevel Up!21后期升級攻略函數(shù)與算子實現(xiàn)惰性求值實現(xiàn)搜索策略函數(shù)式程序正確性的證明抽象類型(用于建立大型系統(tǒng))ML中的命令式程序設計應用:LAMBDA演算解釋器22總結用ML可以很容易寫出清晰、可靠的程序。值得作為函數(shù)式編程的入門語言。在犧牲部分效率的情況下,提升程序的安全性是十分必要的。因為在函數(shù)式編程里,正確性第一,清晰性次之,效率排在第三位。ML在運行前會檢查所有的類型和接口以防止出錯,讓程序員將更多的精力放在豐富語言上,而不是冗長又無用的調試和測試。ML遠沒有這次所展示的這么少,它的強大之處還待繼續(xù)去深入地了解。23參考資料ML for the Working

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論