Upload
-
View
398
Download
0
Embed Size (px)
DESCRIPTION
Scheme 核心概念 for StorySense
Citation preview
scheme 核心概念(一)StorySense
1. Functional Language ?2. LISP ?3. Data type4. Forms & Eval5. Recursion6. Local Variables
scheme 核心概念(一)
Functional Language ? ● 函數式語言
● 特性:○ First-class and higher-order functions○ Pure functions○ Recursion○ Strict versus non-strict evaluation○ Type systems
LISP ?● LISt Processor 列表處理語言● John McCarthy 約翰·麥卡錫 1958年於MIT基於Lambda calculus所設計的程
式語言● 最初LISP語言的概念上的設計,後續有很多不同實現與版本,統稱為LISP家
族● Common Lisp● Clojure● Scheme
○ 極簡主義○ 目前版本R7S7○ 主流的scheme的直譯器或是編譯器
■ Racket■ chicken■ guile
Data Type
● booleans ○ (not #t) => #f
● numbers○ 1○ 3.14○ 1/2○ 1+i
Data Type
● characters○ #\a
● strings○ "hello scheme"
● symbol○ a ,abc ,this-is-symbol○ 字母或是符號的組合○ 用於關聯值或是函數或是其他元素
Data Type
● Dotted pairs & lists○ (1 . 2) => pair○ (1 2) => list○ pair make list
■ cons■ car■ cdr
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
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
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)
Forms & Eval
● express value
● 7 => 7● (+ 1 2) => 3● (if #t 1 2) => 1 ● (define a (+ 1 (* 3 4))) => a● (lambda (x) (+ 1 x)) => #<procedure>
eval
Eval
● self-eval● symbol-eval● form-eval● special form-eval
self-eval
數字 or 字串 or 真假值
● 自我求值● 直接回傳值● ex :
○ 7 => 7○ “hello” => “hello”
symbol-eval
找出跟這個符號關聯的元素
● ex :○ (define a 10)○ a => 10
form-eval● (f a b c)
○ f 為函數位置
○ a b c 為參數位置○ 先將所有參數依序求值○ 再將函數求值,確定函數的過程○ 將參數帶入函數中求值○ 回傳值
form-eval
● ex :○ (+ 1 2) => 3○ (+ 1 (+ 1 1)) => 3○ (+ 1 (+ 1 2) 4 (/ 1 0)) => error
special form-eval
● define● lambda● if● set!● begin
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
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)) !!!
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”
set!
● side effect !!!● not pure function● (set! symbol new-value)● ex:
○ (define a 10)○ (set! a 20)○ a => 20
begin
● (begin form1 form2 form3 …. formN)● ex:
○ (if #t (begin (+ 1 1) (set! a "in begin") a) "false") => 100
Recursion
● scheme沒有 for ???● tail recursion ● tail-call-optimization● need example
list-sum (define (list-sum lis) (if (null? lis) 0 (+ (car lis) (list-sum (cdr lis)))))
tail-recursive
(define (lsum lis acc) (if (null? lis) acc (lsum (cdr lis) (+ acc (car lis)))))(define (list-sum lis) (lsum lis 0))
fibonacci number
(define (fib n) (if (<= n 2) 1 (+ (fib (- n 1))
(fib (- n 2)))))
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)))
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))))))
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 '()))
Let It Be Lambda
● (let ((n1 v1) (n2 v2)
(n.. v…)) body)= ( (lambda (n1 n2 n…) (body)) v1 v2 v…)
Local Variables
● (define x 20)
● (let ((x 10)) (+ x 1))
● (let ((x 5)) (set! x 6) x)
Lexical Scope(define scope 5)(define (print-scope)
(display scope))
(let ((scope 100))(print-scope)) => (display 5)
state
● state(define (make-state init-value)
(let ((init init-value))init))
(define a (make-state 5))(define b (make-state 6))(+ a b)
Let Over Lambda & Closure(define inc-counter
(let ((counter 0))(lambda ()
(set! counter (+ 1 counter)) counter)))
> (inc-counter) => 1> (inc-counter) => 2
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
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")))))
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))
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)
Learn Lisp in Scheme
● 請原諒投影片很爛的code hightlight
● Question & Feedback make me more perfect
contact me : [email protected] (odie)
Thanks for your listeningnext time - scheme 核心概念(二)