Scheme 核心概念(一)

696 views

Published on

Scheme 核心概念 for StorySense

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
696
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Scheme 核心概念(一)

  1. 1. scheme 核心概念(一) StorySense
  2. 2. scheme 核心概念(一) 1. 2. 3. 4. 5. 6. Functional Language ? LISP ? Data type Forms & Eval Recursion Local Variables
  3. 3. Functional Language ? ● 函數式語言 ● 特性: ○ ○ ○ ○ ○ First-class and higher-order functions Pure functions Recursion Strict versus non-strict evaluation Type systems
  4. 4. LISP ? ● ● ● ● ● ● LISt Processor 列表處理語言 John McCarthy 約翰·麥卡錫 1958年於MIT基於Lambda calculus所設計的程 式語言 最初LISP語言的概念上的設計,後續有很多不同實現與版本,統稱為LISP家 族 Common Lisp Clojure Scheme ○ ○ ○ 極簡主義 目前版本R7S7 主流的scheme的直譯器或是編譯器 ■ ■ ■ Racket chicken guile
  5. 5. Data Type ● booleans ○ (not #t) => #f ● numbers ○ 1 ○ 3.14 ○ 1/2 ○ 1+i
  6. 6. Data Type ● characters ○ #a ● strings ○ "hello scheme" ● symbol ○ a ,abc ,this-is-symbol ○ 字母或是符號的組合 ○ 用於關聯值或是函數或是其他元素
  7. 7. Data Type ● Dotted pairs & lists ○ (1 . 2) => pair ○ (1 2) => list ○ pair make list ■ cons ■ car ■ cdr
  8. 8. Dotted pairs & lists ● (cons 1 2) => (1 . 2) ● (car (cons 1 2)) => 1 ● (cdr (cons 1 2)) => 2 ○ (quote ()) or ‘() => () ○ (cons (cons 1 2) 3) => ((1 . 2) . 3) ○ (cons 1 (cons 2 3)) => (1 2 . 3) cdr 的部分是pair,就不會顯示dot ○ (cons 4 ‘()) => (4) *這是pair ○ (cons 5 (cons 4 ‘())) => (5 4) *這是list
  9. 9. Dotted pairs & lists ● ● ● ● (pair? (cons 1 2)) => #t (list? (cons 1 2)) => #f (pair? (cons 1 (cons 2 '()))) => #t (list? (cons 1 (cons 2 '()))) => #t
  10. 10. Pair & List operation ● car ● cdr ● ex: ○ ○ ○ ○ ○ l => ((1 2) 3 ((5 6) 7)) (car l) => (1 2) (cdr l) => (3 ((5 6) 7)) (car (cdr l)) => 3 *(cadr l) => 3 (car (car (cdr (cdr l)))) => (5 6) *(caaddr l) => (5 6)
  11. 11. Forms & Eval ● express ● ● ● ● ● eval value 7 => 7 (+ 1 2) => 3 (if #t 1 2) => 1 (define a (+ 1 (* 3 4))) => a (lambda (x) (+ 1 x)) => #<procedure>
  12. 12. Eval ● ● ● ● self-eval symbol-eval form-eval special form-eval
  13. 13. self-eval 數字 or 字串 or 真假值 ● 自我求值 ● 直接回傳值 ● ex : ○ 7 => 7 ○ “hello” => “hello”
  14. 14. symbol-eval 找出跟這個符號關聯的元素 ● ex : ○ (define a 10) ○ a => 10
  15. 15. form-eval ● (f a b c) ○ f 為函數位置 ○ a b c 為參數位置 ○ 先將所有參數依序求值 ○ 再將函數求值,確定函數的過程 ○ 將參數帶入函數中求值 ○ 回傳值
  16. 16. form-eval ● ex : ○ (+ 1 2) => 3 ○ (+ 1 (+ 1 1)) => 3 ○ (+ 1 (+ 1 2) 4 (/ 1 0)) => error
  17. 17. special form-eval ● ● ● ● ● define lambda if set! begin
  18. 18. define ● ● ● ● 在環境中把symbol與value關聯起來 先對value部分求值 (define symbol value) ex: ○ ○ ○ ○ (define a 10) (define b (+ 1 2)) (define a’ a) (define copy-car car) ■ (copy-car (cons 1 2)) => 1
  19. 19. lambda ● lambda calculation !!! ● (lambda args body) ● ex: ○ ○ ○ ○ ○ ○ ○ (lambda (x) (+ 1 x)) ((lambda (x) (+ 1 x)) 5) => 6 (define inc (lambda (x) (+ 1 x))) (inc 5) => 6 (define (inc x) (+ 1 x)) (inc 5) => 6 TRY (lambda (x) (/ x 0)) !!!
  20. 20. if ● control structure ● (if test true-block false-block) ● ex: ○ ○ ○ ○ (if #t 1 2) => 1 (if (> 1 2) “y” “n”) => “n” (if (= 1 1) “y” “n”) => “y” (if (> 2 1) “y” (/ 1 0)) => “y”
  21. 21. set! ● ● ● ● side effect !!! not pure function (set! symbol new-value) ex: ○ (define a 10) ○ (set! a 20) ○ a => 20
  22. 22. begin ● (begin form1 form2 form3 …. formN) ● ex: ○ (if #t (begin (+ 1 1) (set! a "in begin") a) "false") => 100
  23. 23. Recursion ● ● ● ● scheme沒有 for ??? tail recursion tail-call-optimization need example
  24. 24. list-sum (define (list-sum lis) (if (null? lis) 0 (+ (car lis) (list-sum (cdr lis)))))
  25. 25. tail-recursive (define (lsum lis acc) (if (null? lis) acc (lsum (cdr lis) (+ acc (car lis))))) (define (list-sum lis) (lsum lis 0))
  26. 26. fibonacci number (define (fib n) (if (<= n 2) 1 (+ (fib (- n 1)) (fib (- n 2)))))
  27. 27. fibonacci number tail-recursive (define (fib-tail-helper a b n) (if (= n 0) b (fib-tail-helper b (+ a b) (- n 1)))) (define (fib-tail n) (fib-tail-helper 1 1 (- n 2)))
  28. 28. remove (define (remove x ls) (if (null? ls) '() (if (eq? x (car ls)) (append '() (remove x (cdr ls))) (append (list (car ls)) (remove x (cdr ls))))))
  29. 29. remove tail-recursive (define (remove-tail-helper x ls acc) (if (null? ls) acc (if (eq? x (car ls)) (remove-tail-helper x (cdr ls) acc) (remove-tail-helper x (cdr ls) (append acc (list (car ls))))))) (define (remove-tail x ls) (remove-tail-helper x ls '()))
  30. 30. Let It Be Lambda ● (let ((n1 v1) (n2 v2) (n.. v…)) body) = ( (lambda (n1 n2 n…) (body)) v1 v2 v…)
  31. 31. Local Variables ● (define x 20) ● (let ((x 10)) (+ x 1)) ● (let ((x 5)) (set! x 6) x)
  32. 32. Lexical Scope (define scope 5) (define (print-scope) (display scope)) (let ((scope 100)) (print-scope)) => (display 5)
  33. 33. state ● state (define (make-state init-value) (let ((init init-value)) init)) (define a (make-state 5)) (define b (make-state 6)) (+ a b)
  34. 34. Let Over Lambda & Closure (define inc-counter (let ((counter 0)) (lambda () (set! counter (+ 1 counter)) counter))) > (inc-counter) => 1 > (inc-counter) => 2
  35. 35. Let Over Lmabda Over Let Over Lmabda (define (make-adder) (let ((n 2)) (define add2 (lambda (x) (+ x n))) add2)) >(define my-add (make-adder)) > (my-add 2) => 4 > (my-add 8) => 10
  36. 36. Example: Stack class (define (make-stack) (let ((stack '())) (define (push val) (set! stack (cons val stack)) stack) (define (pop) (let ((tmp (car stack))) (set! stack (cdr stack)) tmp)) (define (top) (car stack)) (define (is-empty?) (null? stack)) (lambda (op) (cond ((eq? op 'push) push) ((eq? op 'pop) pop) ((eq? op 'top) top) ((eq? op 'empty?) is-empty?) (else "error")))))
  37. 37. Example: Stack class (define stack (make-stack)) (define top (stack 'top)) (define empty? (stack 'empty?)) (define push-stack (stack 'push)) (define pop-stack (stack 'pop))
  38. 38. Example: Stack class > (push-stack 5) => (5) > (top) => 5 > (push-stack 6) => (6 5) > (pop-stack) => 6 > (top) => 5 > (push-stack 1) => (1 5) > (push-stack 2) =>(2 1 5) > (push-stack 3) =>(3 2 1 5)
  39. 39. Learn Lisp in Scheme ● 請原諒投影片很爛的code hightlight ● Question & Feedback make me more perfect contact me : ts771164@gmail.com (odie)
  40. 40. Thanks for your listening next time - scheme 核心概念(二)

×