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 656
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 656
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 656
======assoc.scm======
#!/usr/bin/env racket
#lang racket/base
viz [[racket>assoc]]
assoc ((k1 . v1) (k2 . v2) ...)
operace:
(cons-assoc key val assoc) nastav nebo pridej [[PAPR1:L6#Asociativni_pole|assoc pole]]
(assoc-key assoc key) hledej; racket: (assoc key alist)
(assoc-del assoc key) zrus klic
v rnrs je alist jako ((k v) (k v) ...)
racket: (assoc key alist) -> (key ...) ;equal?
racket: (assv key alist) -> (key ...) ;eqv?
racket: (assq key alist) -> (key ...) ;eq?
(exists pred list)
(forall pred list)
'(exists pomoci akumulace, foldl:)
(define (exists p l)
(foldl
(lambda (e t)
(if t ;uz nasli, vracej porad #t
t
(p e)))
#f
l))
(exists (lambda (x) (= x 1)) '(1 2 3))
(exists even? '(1 2 3))
(exists even? '(1 5 3))
pomoci iterace lze jen and a or, viz [[PAPR1:L9]]
'(exists pomoci iterace:)
(define (exists-iter p l)
(cond
((null? l) #f) ;prazdny seznam, nebo jsme na konci
((p (car l)) #t);nasli
(else (exists-iter p (cdr l)))))
(exists-iter (lambda (x) (= x 1)) '(1 2 3))
(exists-iter even? '(1 2 3))
(exists-iter even? '(1 5 3))
pouzit exists str159, existuje? premapovat, jinak pridat
(define (cons-assoc key val assoc) assoc) ;todo pomoci fold, iterace?
sel by exist-iter inline jako letrec?
NOTE: Lekce 6 jeste nezná akumulace, fold
(define (cons-assoc key val assoc)
(if (exists (lambda (x) (equal? key (car x))) assoc)
;existuje, najdeme a pre-map-ujeme
(map
(lambda (x)
(if (equal? key (car x))
(cons key val)
x))
assoc)
;nenasli, novy par
(cons (cons key val) assoc)))
(define al (cons-assoc 'a 1 '()))
(cons-assoc 'a 2 al)
(cons-assoc 'b 3 al);porad dva prvky!!! nemuze modifikovat al!!!
(cons-assoc 'c 4 al);takze nabalovat se to muze rekurzi, fold-rem apod!
(cons-assoc 'c 4 al)
(cons-assoc 'd 5 '((d . 5)))
(collate: '(1 1 2 2 3 3 3) => ((1 . 1) (2 . 2) (3 . 3))
(define (collate l) ;vlastne upravene cons-assoc, reseni na urovni lekce 6
(foldl
(lambda (e t)
(if (exists (lambda (x) (equal? e (car x))) t)
;existuje, najdeme a inkrementujeme
(map
(lambda (x)
(if (equal? e (car x))
(cons e (+ 1 (cdr x)));inkrementujeme
x))
t)
;nenasli, novy par
(cons (cons e 1) t)))
'();term/kontext bude alist
l))
(collate '(1 2 2 3 3 3)) ;=>((1 . 1) (2 . 2) (3 . 3))
(exit)
(sort lst
less-than?
[ #:key extract-key
#:cache-keys? cache-keys?]) → list?
lst : list?
less-than? : (any/c any/c . -> . any/c)
extract-key : (any/c . -> . any/c) = (lambda (x) x)
cache-keys? : boolean? = #f
(sort '(1 2 4 3) <)
(sort '(1 2 4 3) >)
(sort '((b . 2) (a . 1) (c . 3)) (lambda (x y) (< (cdr x) (cdr y))))
prida nebo aktualizuje key val
(define (cons-assoc-iter key val assoc)
(if (null? assoc)
(list (cons key val))
(foldl ;'rychlejsi' nez foldr, ale dela reverse!
(lambda (e t)
(if (equal? key (car e))
(cons (cons key val) t) ;#vymenime
(cons-assoc-iter key val (cons e t))));#kopirujeme a hledame dal
'()
assoc)))
'(cons-assoc-iter k v al)
(cons-assoc-iter 1 1 '())
(cons-assoc-iter 1 2 '((1 . 1)))
(cons-assoc-iter 2 2 '((1 . 1)))
; vim: syntax=racket