3. Functional Language ?
● 函數式語言
● 特性:
○
○
○
○
○
First-class and higher-order functions
Pure functions
Recursion
Strict versus non-strict evaluation
Type systems
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. 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…)