Top Banner
scheme 核心概念() StorySense
40

Scheme 核心概念(一)

Jan 15, 2015

Download

Technology

Scheme 核心概念 for StorySense
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Scheme 核心概念(一)

scheme 核心概念(一)StorySense

Page 2: Scheme 核心概念(一)

1. Functional Language ?2. LISP ?3. Data type4. Forms & Eval5. Recursion6. Local Variables

scheme 核心概念(一)

Page 3: Scheme 核心概念(一)

Functional Language ? ● 函數式語言

● 特性:○ First-class and higher-order functions○ Pure functions○ Recursion○ Strict versus non-strict evaluation○ Type systems

Page 4: Scheme 核心概念(一)

LISP ?● LISt Processor 列表處理語言● John McCarthy 約翰·麥卡錫 1958年於MIT基於Lambda calculus所設計的程

式語言● 最初LISP語言的概念上的設計,後續有很多不同實現與版本,統稱為LISP家

族● Common Lisp● Clojure● Scheme

○ 極簡主義○ 目前版本R7S7○ 主流的scheme的直譯器或是編譯器

■ Racket■ chicken■ guile

Page 5: Scheme 核心概念(一)

Data Type

● booleans ○ (not #t) => #f

● numbers○ 1○ 3.14○ 1/2○ 1+i

Page 6: Scheme 核心概念(一)

Data Type

● characters○ #\a

● strings○ "hello scheme"

● symbol○ a ,abc ,this-is-symbol○ 字母或是符號的組合○ 用於關聯值或是函數或是其他元素

Page 7: Scheme 核心概念(一)

Data Type

● Dotted pairs & lists○ (1 . 2) => pair○ (1 2) => list○ pair make list

■ cons■ car■ cdr

Page 8: Scheme 核心概念(一)

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

Page 9: Scheme 核心概念(一)

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

Page 10: Scheme 核心概念(一)

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)

Page 11: Scheme 核心概念(一)

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

Page 12: Scheme 核心概念(一)

Eval

● self-eval● symbol-eval● form-eval● special form-eval

Page 13: Scheme 核心概念(一)

self-eval

數字 or 字串 or 真假值

● 自我求值● 直接回傳值● ex :

○ 7 => 7○ “hello” => “hello”

Page 14: Scheme 核心概念(一)

symbol-eval

找出跟這個符號關聯的元素

● ex :○ (define a 10)○ a => 10

Page 15: Scheme 核心概念(一)

form-eval● (f a b c)

○ f 為函數位置

○ a b c 為參數位置○ 先將所有參數依序求值○ 再將函數求值,確定函數的過程○ 將參數帶入函數中求值○ 回傳值

Page 16: Scheme 核心概念(一)

form-eval

● ex :○ (+ 1 2) => 3○ (+ 1 (+ 1 1)) => 3○ (+ 1 (+ 1 2) 4 (/ 1 0)) => error

Page 17: Scheme 核心概念(一)

special form-eval

● define● lambda● if● set!● begin

Page 18: Scheme 核心概念(一)

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

Page 19: Scheme 核心概念(一)

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)) !!!

Page 20: Scheme 核心概念(一)

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”

Page 21: Scheme 核心概念(一)

set!

● side effect !!!● not pure function● (set! symbol new-value)● ex:

○ (define a 10)○ (set! a 20)○ a => 20

Page 22: Scheme 核心概念(一)

begin

● (begin form1 form2 form3 …. formN)● ex:

○ (if #t (begin (+ 1 1) (set! a "in begin") a) "false") => 100

Page 23: Scheme 核心概念(一)

Recursion

● scheme沒有 for ???● tail recursion ● tail-call-optimization● need example

Page 24: Scheme 核心概念(一)

list-sum (define (list-sum lis) (if (null? lis) 0 (+ (car lis) (list-sum (cdr lis)))))

Page 25: Scheme 核心概念(一)

tail-recursive

(define (lsum lis acc) (if (null? lis) acc (lsum (cdr lis) (+ acc (car lis)))))(define (list-sum lis) (lsum lis 0))

Page 26: Scheme 核心概念(一)

fibonacci number

(define (fib n) (if (<= n 2) 1 (+ (fib (- n 1))

(fib (- n 2)))))

Page 27: Scheme 核心概念(一)

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)))

Page 28: Scheme 核心概念(一)

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))))))

Page 29: Scheme 核心概念(一)

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 '()))

Page 30: Scheme 核心概念(一)

Let It Be Lambda

● (let ((n1 v1) (n2 v2)

(n.. v…)) body)= ( (lambda (n1 n2 n…) (body)) v1 v2 v…)

Page 31: Scheme 核心概念(一)

Local Variables

● (define x 20)

● (let ((x 10)) (+ x 1))

● (let ((x 5)) (set! x 6) x)

Page 32: Scheme 核心概念(一)

Lexical Scope(define scope 5)(define (print-scope)

(display scope))

(let ((scope 100))(print-scope)) => (display 5)

Page 33: Scheme 核心概念(一)

state

● state(define (make-state init-value)

(let ((init init-value))init))

(define a (make-state 5))(define b (make-state 6))(+ a b)

Page 34: Scheme 核心概念(一)

Let Over Lambda & Closure(define inc-counter

(let ((counter 0))(lambda ()

(set! counter (+ 1 counter)) counter)))

> (inc-counter) => 1> (inc-counter) => 2

Page 35: Scheme 核心概念(一)

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

Page 36: Scheme 核心概念(一)

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")))))

Page 37: Scheme 核心概念(一)

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))

Page 38: Scheme 核心概念(一)

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)

Page 39: Scheme 核心概念(一)

Learn Lisp in Scheme

● 請原諒投影片很爛的code hightlight

● Question & Feedback make me more perfect

contact me : [email protected] (odie)

Page 40: Scheme 核心概念(一)

Thanks for your listeningnext time - scheme 核心概念(二)