Programming Language 20060711 By ShuChun Weng scw scw
Programming Language 程式語言 2006/07/11 By Shu-Chun Weng (翁書鈞, scw) scw <at> csie <dot> org http: //scw. tw/works/CSCamp/ CS Camp 2006
為什麼要程式語言? n n n 因為電腦很機車,它說的話沒有人看得懂 因為電腦很機車,它說的話別台電腦也看不懂 i 386: 101100000011 11111110 11000000 11100000010 MIPS: 001000001000 00000011 00100001000 00000001 00001000 010000000 他們都在說 (3 + 1) * 4 ! 9/9/2020 CS Camp 2006 4
語言可讀性 (Readable) (cont. ) n 高階語言的目標通常包含可讀性 return ${${$_[0]}->{capture}} if defined ${${$_[0]}->{capture}}; return substr(${${$_[0]}->{str}}, $_[0]->from, $_[0]->to - $_[0]->from ); not exp log srand xor s qq qx xor s x x length uc ord and print chr ord for qw q join use sub tied qx xor eval xor print qq q q xor int eval lc q m cos and print chr ord for qw y abs ne open tied hex exp ref y m xor scalar srand print qq q q xor int eval lc qq y sqrt cos and print chr ord for qw x printf each return local x y or print qq s s and eval q s undef or oct xor time xor ref print chr int ord lc 9/9/2020 14 CS Camp 2006 foreach qw y hex alarm chdir kill exec return y s gt sin sort split
BEFOREHAND: close door, each window & exit; wait until time. open spellbook, study, read (scan, select, tell us); write it, print the hex while each watches, reverse its length, write again; kill spiders, pop them, chop, split, kill them. unlink arms, shift, wait & listen (listening, wait), sort the flock (then, warn the "goats" & kill the "sheep"); kill them, dump qualms, shift moralities, values aside, each one; die sheep! die to reverse the system you accept (reject, respect); next step, kill the next sacrifice, each sacrifice, wait, redo ritual until "all the spirits are pleased"; do it ("as they say"). do it(*everyone***must***participate***in***forbidden**s*e*x*). return last victim; package body; exit crypt (time, times & "half a time") & close it, select (quickly) & warn your next victim; AFTERWORDS: tell nobody. wait, wait until time; wait until next year, next decade; sleep, die yourself, die at last 9/9/2020 CS Camp 2006 # Larry Wall 15
程式執行方式 n 直譯 (interpret) • 邊讀邊執行程式 • BASIC, python, ruby, shell script, lisp n 編譯 (compile) • 至原生碼 (native code) n C, C++, FORTRAN • 至虛擬碼 (byte code) (JIT) n Java, C#, Smalltalk, (Perl) • 至其他語言 n 9/9/2020 Haskell CS Camp 2006 18
機械碼 (Machine Code) n n n i 386: 101100000011 11111110 11000000 11100000010 MIPS: 001000001000 00000011 00100001000 00000001 00001000 010000000 他們都在說 (3 + 1) * 4 ! 9/9/2020 CS Camp 2006 20
i 386 101100000011 X = 3 11111110 11000000 X = X + 1 11000000 11100000010 X = X * 4 X = (3 + 1) * 4 9/9/2020 CS Camp 2006 21
MIPS 001000001000 00000011 X = 3 00100001000 00000001 X = X + 1 00001000 010000000 X = X * 4 X = (3 + 1) * 4 9/9/2020 CS Camp 2006 22
組合語言 n n 用單字取代 0 和 1 給暫存器 (register) 名字 動作 (通常) 很單純 MOV, JMP, ADD …… • mov al, 3 inc al shl al, 2 • addi $t 0, $0, 3 addi $t 0, 1 sll $t 0, 2 9/9/2020 CS Camp 2006 23
組合語言 (cont. ) n 不允許一些危險行為 10110000 00000011 11111110 11000000 11100000010 11101011 11000000 11100000010 11000000 11110110 MOV ADD SHL JMP ADD ROL 9/9/2020 al, 3 al, 1 al, 2 -8 edi, esi al, e 0 h KUKUKU~ CS Camp 2006 24
高階語言 9/9/2020 CS Camp 2006 25
FORTRAN n n n 第一個高階語言! 還在用的老古董 Mathematical Formula Translating System IBM 1954 -57 完成 1977 年訂定 F 77 標準 • 結構化 • 多種內建資料型態 n 科學計算 • 效率 9/9/2020 CS Camp 2006 27
FORTRAN (cont. ) PROGRAM CMPLXD IMPLICIT COMPLEX(X) PARAMETER (PI = 3. 141592653589793, XJ = (0, 1)) DO 1, I = 0, 7 X = EXP(XJ * I * PI / 4) IF (AIMAG(X). LT. 0) THEN PRINT 2, 'e**(j*', I, '*pi/4) = ', REAL(X), ' - j', -AIMAG(X) ELSE PRINT 2, 'e**(j*', I, '*pi/4) = ', REAL(X), ' + j', AIMAG(X) END IF 2 FORMAT (A, I 1, A, F 10. 7, A, F 9. 7) 1 CONTINUE STOP END 9/9/2020 CS Camp 2006 28
Pascal (cont. ) program mine(output); var i : integer; procedure print(var j: integer); function next(k: integer): integer; begin next : = k + 1 end; begin writeln('The total is: ', j); j : = next(j) end; begin i : = 1; while i <= 10 do print(i) end. 9/9/2020 CS Camp 2006 type a = ^b; b = record a: integer; b: char; c: a end; var pointer_to_b: a; begin new(pointer_to_b); pointer_to_b^. a : = 10; pointer_to_b^. b : = 'A'; pointer_to_b^. c : = nil; end. 30
C (cont. ) void strcpy(char *s, char *t) { while (*s++ = *t++); } 9/9/2020 CS Camp 2006 32
Smalltalk (cont. ) 3 factorial + 4 factorial between: 10 and: 100 result : = a > b if. True: [ 'greater' ] if. False: [ 'less' ] 9/9/2020 CS Camp 2006 35
Eiffel n n n OO 考慮到讓眾多廠商的 程式碼合作 「依合約設計」 Design by Contract 9/9/2020 CS Camp 2006 38
Eiffel (cont. ) class interface ACCOUNT feature -- Access balance: INTEGER -- Current balance deposit_count: INTEGER -- Number of deposits made since opening feature -- Element change deposit (sum: INTEGER) -- Add sum to account. require non_negative: sum >= 0 ensure one_more_deposit: deposit_count = old deposit_count + 1 updated: balance = old balance + sum invariant consistent_balance: balance = all_deposits. total end -- class interface ACCOUNT 9/9/2020 CS Camp 2006 39
腳本語言 (Scripting Language) n n n n Shell script awk/sed Tk/Tcl Perl Python Ruby Javascript 9/9/2020 CS Camp 2006 40
Shell script + awk + sed f=`echo $1 | sed -e 's|(. *). [^/]*$|1|'` autob 5 -i utf 8 -o big 5 < $1 | bg 5 conv > $f. cjk && latex $f. cjk ps x | grep bbs | awk ‘{print $1}’ | xargs kill 9/9/2020 CS Camp 2006 41
Python n n n 「ABC programming language」 用縮排當語法! 高度可讀 龐大的標準函式庫 物件導向 Your challenge is powered by Python! 9/9/2020 CS Camp 2006 44
Python (cont. ) from itertools import count def generate_primes(stop_at=None): primes = [] for n in count(2): if stop_at is not None and n > stop_at: return composite = False for p in primes: if not n % p: composite = True break elif p**2 > n: break if not composite: primes. append(n) yield n 9/9/2020 CS Camp 2006 45
Ruby n 強調和其他語言很像 • • n Perl Python Smalltalk Eiffel OO ash = {: water => 'wet', : fire => 'hot'} puts hash[: fire] # Prints: hot hash. each_pair do |key, value| puts "#{key} is #{value}“ end 9/9/2020 CS Camp 2006 46
Javascript n n 控制網頁上的物件 AJAX! 9/9/2020 CS Camp 2006 47
lisp n n n 變成一種通稱 clisp, scheme 等「 方言」 一堆括號 允許 assignment 非常動態 (defun factorial (n) (if (<= n 1) 1 (* n (factorial (- n 1))))) 9/9/2020 CS Camp 2006 49
Prolog n n 邏輯程式語言 寫出邏輯,然後問問題 sibling(X, Y) : - parent(Z, X), parent(Z, Y). parent(X, Y) : - father(X, Y). parent(X, Y) : - mother(X, Y). mother(trude, sally). father(tom, erica). father(mike, tom). ? - sibling(sally, erica). yes. 9/9/2020 CS Camp 2006 51
PHP/ASP/JSP n 在 HTML 裡直接寫程 式 9/9/2020 CS Camp 2006 52
PS n n n 由印表機執行,描出 頁面 大幅減少傳輸 由程式產生的程式 %%Page: 2 2 Te. XDict begin 2 1 bop 880 1063 a Fj(Exercise)88 b(11. 5)j(13)880 1426 y Fi(Since)39 b(summation)h(of)f Fh(n)g Fi(indep)5 b(enden)-5 b(t)39 b(p)5 b(ossion)40 b(random)g(v)-9 b(ariables)39 b(is)h(a)e(p)5 b(ossion)41 b(random)880 1626 y(v)-9 b(ariable)5 9/9/2020 CS Camp 2006 53
型別 n n n 組語沒有「型別」 FORTRAN: Integer, Character, Real Pascal: 自訂型別 OO: 自訂型別中有函式 Functional Language: 函數型別 9/9/2020 CS Camp 2006 58
型別 (cont. ) n Assignable? • • • n Integer <- Character Real <- Integer <- Real ? Subtype • • 9/9/2020 Real < Integer < Character { a: Integer; b: Integer } < { a: Integer } (Integer -> Integer) < (Character -> Real) Q: { a: Int; b: Int; cmp: P -> Bool} < P: { a: Int; cmp: P -> Bool} CS Camp 2006 59
- Slides: 60