Contenu connexe Similaire à Scheme 核心概念(一) (20) Scheme 核心概念(一)3. Functional Language ?
● 函數式語言
● 特性:
○
○
○
○
○
First-class and higher-order functions
Pure functions
Recursion
Strict versus non-strict evaluation
Type systems
4. LISP ?
●
●
●
●
●
●
LISt Processor 列表處理語言
John McCarthy 約翰·麥卡錫 1958年於MIT基於Lambda calculus所設計的程
式語言
最初LISP語言的概念上的設計,後續有很多不同實現與版本,統稱為LISP家
族
Common Lisp
Clojure
Scheme
○
○
○
極簡主義
目前版本R7S7
主流的scheme的直譯器或是編譯器
■
■
■
Racket
chicken
guile
6. Data Type
● characters
○ #a
● strings
○ "hello scheme"
● symbol
○ a ,abc ,this-is-symbol
○ 字母或是符號的組合
○ 用於關聯值或是函數或是其他元素
7. Data Type
● Dotted pairs & lists
○ (1 . 2) => pair
○ (1 2) => list
○ pair make list
■ cons
■ car
■ cdr
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. 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. 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. 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>
15. form-eval
● (f a b c)
○ f 為函數位置
○ a b c 為參數位置
○ 先將所有參數依序求值
○ 再將函數求值,確定函數的過程
○ 將參數帶入函數中求值
○ 回傳值
16. form-eval
● ex :
○ (+ 1 2) => 3
○ (+ 1 (+ 1 1)) => 3
○ (+ 1 (+ 1 2) 4 (/ 1 0)) => error
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. 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”
22. begin
● (begin form1 form2 form3 …. formN)
● ex:
○ (if #t (begin
(+ 1 1)
(set! a "in begin")
a)
"false") => 100
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. 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. Let It Be Lambda
● (let ((n1 v1)
(n2 v2)
(n.. v…))
body)
= ( (lambda (n1 n2 n…) (body))
v1 v2 v…)
34. Let Over Lambda & Closure
(define inc-counter
(let ((counter 0))
(lambda ()
(set! counter (+ 1 counter))
counter)))
> (inc-counter) => 1
> (inc-counter) => 2
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. 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. 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. 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. Learn Lisp in Scheme
● 請原諒投影片很爛的code hightlight
● Question & Feedback make me
more perfect
contact me : ts771164@gmail.com (odie)