Warning: Declaration of action_plugin_subjectindex_indexer::register(&$controller) should be compatible with DokuWiki_Action_Plugin::register(Doku_Event_Handler $controller) in /data/web/virtuals/28604/virtual/www/subdom/bo/lib/plugins/subjectindex/action/indexer.php on line 15

Warning: Declaration of action_plugin_mathjax_enable::register(Doku_Event_Handler &$controller) should be compatible with DokuWiki_Action_Plugin::register(Doku_Event_Handler $controller) in /data/web/virtuals/28604/virtual/www/subdom/bo/lib/plugins/mathjax/action/enable.php on line 62

Warning: Declaration of action_plugin_googleanalytics::register(&$controller) should be compatible with DokuWiki_Action_Plugin::register(Doku_Event_Handler $controller) in /data/web/virtuals/28604/virtual/www/subdom/bo/lib/plugins/googleanalytics/action.php on line 40

Warning: Declaration of action_plugin_folded::register(&$controller) should be compatible with DokuWiki_Action_Plugin::register(Doku_Event_Handler $controller) in /data/web/virtuals/28604/virtual/www/subdom/bo/lib/plugins/folded/action.php on line 40

Warning: Declaration of action_plugin_hidden::register(&$controller) should be compatible with DokuWiki_Action_Plugin::register(Doku_Event_Handler $controller) in /data/web/virtuals/28604/virtual/www/subdom/bo/lib/plugins/hidden/action.php on line 28

Warning: Declaration of action_plugin_include::register(&$controller) should be compatible with DokuWiki_Action_Plugin::register(Doku_Event_Handler $controller) in /data/web/virtuals/28604/virtual/www/subdom/bo/lib/plugins/include/action.php on line 354

Warning: Declaration of action_plugin_tag::register(&$contr) should be compatible with DokuWiki_Action_Plugin::register(Doku_Event_Handler $controller) in /data/web/virtuals/28604/virtual/www/subdom/bo/lib/plugins/tag/action.php on line 175

Warning: Cannot modify header information - headers already sent by (output started at /data/web/virtuals/28604/virtual/www/subdom/bo/lib/plugins/subjectindex/action/indexer.php:15) in /data/web/virtuals/28604/virtual/www/subdom/bo/inc/auth.php on line 532

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /data/web/virtuals/28604/virtual/www/subdom/bo/inc/auth.php on line 818

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /data/web/virtuals/28604/virtual/www/subdom/bo/inc/parser/handler.php on line 1376

Warning: Declaration of SI_EntryDefault::match($text) should be compatible with SI_Entry::match() in /data/web/virtuals/28604/virtual/www/subdom/bo/lib/plugins/subjectindex/plugins/EntryDefault.php on line 68

Warning: Declaration of SI_EntryTag::match($text) should be compatible with SI_Entry::match() in /data/web/virtuals/28604/virtual/www/subdom/bo/lib/plugins/subjectindex/plugins/EntryTag.php on line 42

Warning: Declaration of SI_EntryVerse::match($text) should be compatible with SI_Entry::match() in /data/web/virtuals/28604/virtual/www/subdom/bo/lib/plugins/subjectindex/plugins/EntryVerse.php on line 1280

Warning: preg_match(): Compilation failed: invalid range in character class at offset 4360 in /data/web/virtuals/28604/virtual/www/subdom/bo/inc/parser/lexer.php on line 118
A PCRE internal error occured. This might be caused by a faulty plugin

Warning: Cannot modify header information - headers already sent by (output started at /data/web/virtuals/28604/virtual/www/subdom/bo/lib/plugins/subjectindex/action/indexer.php:15) in /data/web/virtuals/28604/virtual/www/subdom/bo/inc/actions.php on line 215
YPP1:P10.scm [Bo.bule]

======P10.scm====== <code shell>#!/usr/bin/env racket #lang racket/base</code> přiklady, programy, kód z [[PAPR1:L10|Lekce 10]] * n-ární a binární stromy 10.1, 10.3 * množiny jako uspořádané seznamy 10.2, nebo BST: 10.3 * kombinatorika 10.4 ======= 10.1 n-ární stromy ======= a /|\ b c d |\ e f /|\ g h i | j <code scheme> (define tree-aj '(a (b) (c (e) (f (g) (h) (i (j)))) (d)) ) </code> <hidden tree konstruktory a accessory> <code scheme> (define tree (lambda (val . subtrees) (cons val subtrees))) (define get-val car) (define (get-branches tree) (if (null? tree) '() (cdr tree));safe-cdr ) </code> </hidden> (a (b c) (...)) <hidden je to seznam, mimo prvního seznamy?> <code scheme> (define (forall p? l) (or (null? l);prazdna mnozina splnuje (and (p? (car l)) (forall p? (cdr l))))) (define (tree? t) (and (list? t) (forall tree? (cdr t)))) ; ;stejne jako tree?, ale forall inline named let: (define (tree2? t) (and (list? t) (let forall ((p? tree?) (l (cdr t))) (or (null? l) (and (p? (car l)) (forall p? (cdr l)))));inline forall, named-let iterace )) </code> </hidden> <code scheme> (map tree? (list tree-aj 1 '(1 . 2) '(1 2) '(1 (2 3)) '(1 (2 (3))) )) (map tree2? (list tree-aj 1 '(1 . 2) '(1 2) '(1 (2 3)) '(1 (2 (3))) )) </code> <hidden levá a pravá větev> <code scheme> (define (left-branches tree k) (if (or (< k 0) (null? tree)) '() (let iter ((k k) (branches (cdr tree))) (cond ((null? branches) '()) ((= k 0) (car branches)) (else (iter (- k 1) (cdr branches))))))) (define (right-branches tree k) (left-branches tree (- (length tree) k -2))) </code> </hidden> <code scheme> (map (lambda (k) (left-branches tree-aj k)) '(0 1 2 3 4)) (map (lambda (k) (left-branches '(r (a) (b) (c) (d)) k)) '(0 1 2 3 4)) </code> <hidden deep-first-search> <code scheme> (define (dfs tree) (if (null? tree) '() (cons (get-val tree) ;( dfs (get-branches tree)) ;mapuj dfs na branches => apply append (apply append (map dfs (get-branches tree))) ))) </code> </hidden> <code scheme> (dfs tree-aj) (equal? (dfs tree-aj) '(a b c e f g h i j d)) </code> do sirky: iterace po patrech: <code> (a (b) (c (f))) (d ...) (e ...)) </code> <hidden breath-firts-search> <code scheme> (define (bfs tree) (let patro ((tree (list tree)));"pridame" patro navic (display tree) (newline) (if (null? tree) '() (append (map get-val tree);hodnoty tohoto patra ;rekurze do dalsiho patra (patro (apply append (map get-branches tree))))))) </code> </hidden> <code scheme> '(bfs testy) tree-aj (map (lambda (x) (display `(x: ,x)) (newline)) tree-aj) </code> (map get-val tree-aj) ;nefunguje!!! <code scheme> (map get-val (list tree-aj)) (map get-val (get-branches tree-aj)) `(bfs: ,(bfs tree-aj)) `(dfs: ,(dfs tree-aj)) </code> ======= 10.3 BST, reprezentace množin ======= BST: prázdný nebo strom s dvěma podtromy <hidden BST konstruktory a selektory> <code scheme> (define (make-bst val left right) (list val left right)) (define (make-leaf val) (make-bst val '() '())) (define (bst-val tree) (car tree)) (define (bst-left tree) (cadr tree)) (define (bst-right tree) (caddr tree)) </code> /<hidden> <code scheme> (define bstree (make-bst 4 (make-bst 2 (make-leaf 1) (make-leaf 3)) (make-bst 6 (make-leaf 5) (make-leaf 7)))) bstree </code> * union a inter: sjednocení a průnik predikát in * prazdný => #f * hodnota rovna => #t * jinak left/right podle <= <hidden predikát in? pro BST> <code scheme> (define (bst-in? x tree) (cond ((null? tree) #f) ((= x (bst-val tree)) #t) ((< x (bst-val tree)) (bst-in? x (bst-left tree))) (else (bst-in? x (bst-right tree))))) </code> </hidden> <code scheme> (let ((cisla '(1 2 3 4 5 6 7 99))) (map cons cisla (map (lambda (x) (bst-in? x bstree)) cisla))) </code> <hidden konstruktor cons-bst: přidá prvek do množiny> <code scheme> (define (cons-bst x tree) </code> (display `(x: ,x tree: ,tree)) (newline) <code scheme> (cond ((null? tree) (make-leaf x));vratime novy leaf ((= x (bst-val tree)) tree) ;roven/nasli, vratime ((< x (bst-val tree)) (make-bst (bst-val tree) (cons-bst x (bst-left tree)) (bst-right tree))) (else (make-bst (bst-val tree) (bst-left tree) (cons-bst x (bst-right tree)))))) </code> </hidden> <code scheme> (cons-bst 1 '()) (cons-bst 2 (cons-bst 1 '())) (let iter ((i 1) (tree '())) (if (> i 10) tree (iter (+ i 1) (cons-bst i tree)))) ;nebude balancovany!? </code> Program 10.x <hidden xxx> </hidden> <code scheme> ; vim: syntax=racket </code>


Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /data/web/virtuals/28604/virtual/www/subdom/bo/inc/auth.php on line 818
YPP1/P10.scm.txt · Last modified: 2015/01/15 20:45 (external edit)
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0