Download presentation
Presentation is loading. Please wait.
Published byΛυσάνδρα Παπανικολάου Modified over 5 years ago
1
list data list 만들기 list 사용하기 nil : list link : * list -> list
empty? : list -> bool first : list -> rest : list -> list
2
map over list (define (map f lst) (if (empty? lst) nil
(link (f (first lst)) (map f (rest lst))) )
3
(define (inc n) (+ n 1)) (map inc ‘(1 2 3)) (define (square n) (* n n)) (map square ‘(1 2 3))
4
fold over list (define (accumulate op init lst) (if (empty? lst) init
(op (first lst) (accumulate op init (rest lst))) )
5
binary tree data tree 만들기 tree 사용하기 leaf : -> tree
node : tree * tree -> tree tree 사용하기 leaf-val : tree -> is-leaf? : tree -> bool left-subtree : tree -> tree right-subtree : tree -> tree
6
map over binary tree (define (map f tr)
(if (is-leaf? tr) (leaf (f (leaf-val tr))) (node (map f (left-subtree tr)) (map f (right-subtree tr)) )) )
7
fold over binary tree (define (accumulate op tr)
(if (is-leaf? tr) (leaf-val tr) (op (accumulate op (left-subtree tr)) (accumulate op (right-subtree tr)) )
8
boolean circuit data boolean circuit 만들기 boolean circuit 사용하기
one: circuit zero: circuit not : circuit -> circuit and : circuit * circuit -> circuit or : circuit * circuit -> circuit boolean circuit 사용하기 is-one? : circuit -> bool is-zero? : circuit -> bool is-not? : circuit -> bool is-and? : circuit -> bool is-or? : circuit -> bool nth-circuit : circuit * nat -> circuit
9
map over boolean circuit
(define (map f circuit) (cond ((is-one? circuit) (f one)) ((is-zero? circuit) (f zero)) ((is-not? circuit) (not (map f (nth-child circuit 0)))) ((is-and? circuit) (and (map f (nth-child circuit 0)) (map f (nth-child circuit 1)))) ((is-or? circuit) )
10
fold over boolean circuit
(define (eval c) (cond ((is-one? c) 1) ((is-zero? c) 0) ((is-not? c) (bool-not (eval (nth-circuit c 0)))) ((is-and? c) (bool-and (eval (nth-circuit c 0)) (eval (nth-circuit c 1)))) ((is-or? c) (bool-or (eval (nth-circuit c 0)) )
11
cascading over list (define (sum-odd lst) (accumulate +
(filter odd? lst) ) (define (sum-odd-square lst) (map square (filter odd? lst))
12
cascading over tree (define (sum-odd-squares tr) (accumulate +
(map square (filter odd? (enlist-leaves tr) )
13
symbolic expression data
식 만들기 const : int -> expr var : string -> expr sum : expr * expr -> expr product : expr * expr -> expr 식 사용하기 is-const? : expr -> bool is-var? : expr -> bool is-sum? : expr -> bool is-product? : expr -> bool const-val: expr -> int var-name: expr -> string addend: expr * nat -> expr augend: expr -> expr multiplier: expr -> expr multiplicand: expr -> expr
14
example: symbolic differentiation
(define (diff e x) (cond ((is-const? e) …) ((is-var? e) …) ((is-sum? e) …) ((is-product? e) ...) (else (error “diff expects expr”)) )
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.