BuK/IIm15/KA-VL4
Aus ProgrammingWiki
Wiki-Quelltext dieser Seite.
<language>SchemeJS</language>
Streamprozeduren:
<code id="57179d76eb114">
(require racket/engine)
(define stream-car car)
(define stream-cdr
(lambda (stm)
(force (cdr stm))))
(define-syntax stream-cons
(syntax-rules ()
((_ head tail) (cons head (delay tail)))))
(define make-stream
(lambda (seed proc)
(letrec
((stream-builder
(lambda (n)
(stream-cons n (stream-builder (proc n))))))
(stream-builder seed))))
(define stream-item
(lambda (str k)
(if (= k 0)
(stream-car str)
(stream-item (stream-cdr str) (- k 1)))))
(define stream-map
(lambda (f str)
(stream-cons
(f (stream-car str))
(stream-map f (stream-cdr str)))))
(define print-stream
(lambda (stm n)
(cond
((<= n 0) (printf "...~n"))
(else
(printf "~a, " (stream-car stm))
(print-stream (stream-cdr stm) (- n 1))))))
(define nat_zahlen (make-stream 0 (lambda (n) (+ n 1))))
(define generiere_aufzaehlbare_menge
(lambda (f)
(stream-map f nat_zahlen )))
</code>
Aufzählprozedur für Wortmenge A* mit A = {a,b,c}
<code id="57179d76eb182">
(define g
(lambda (n)
(cond
([= n 0] "")
([= n 1] "a")
([= n 2] "b")
([= n 3] "c")
([= (modulo n 3) 0] (string-append (g (- (/ n 3) 1)) "c"))
([= (modulo n 3) 1] (string-append (g (- (/ (+ n 2) 3) 1)) "a"))
([= (modulo n 3) 2] (string-append (g (- (/ (+ n 1) 3) 1)) "b")))))
</code>
<run id="57179d76eb328">(g 0)</run>
Cantorsche Paarungsfunktion
<code id="57179d76eb1e5">
(define cantor
(lambda (x y)
(+ (/ (* (+ y x) (+ y x 1)) 2) y)))
(define getCantorX
(lambda (z)
(- (getCantorW z)(getCantorY z))))
(define getCantorW
(lambda (z)
(floor (/ (- (sqrt (+ (* 8 z) 1)) 1) 2) )))
(define getCantorT
(lambda (w)
(/ (* (+ w 1) w) 2)))
(define getCantorY
(lambda (z)
(- z (getCantorT (getCantorW z)))))
</code>
$\Chi '_M$ mit Hilfsprozeduren
<code id="57179d76eb246">
(define fib
(lambda (n)
(cond
([= n 1] 1)
([= n 2] 1)
(else (+ (fib (- n 1)) (fib (- n 2)))))))
(define countB_helper
(lambda (ls i)
(cond
([empty? ls] i)
([equal? (car ls) #\b] (countB_helper (cdr ls) (+ i 1)))
(else (countB_helper (cdr ls) i)))))
(define countB
(lambda (str)
(countB_helper (string->list str) 0)))
(define chiStrichFib_helper
(lambda (w i)
(cond
([= (countB w) (fib i)] #t)
(else (chiStrichFib_helper w (+ i 1))))))
(define chiStrichFib
(lambda (w t)
(chiStrichFib_helper w 1)))
</code>
<run id="57179d76eb389">(chiStrichFib "ab" #t)
</run>
Aufzählfunktion für M
<code id="57179d76eb2b2">
(define aufzaehlKA
(lambda (z)
(cond
([equal? #t (engine-run (getCantorY z) (engine (binary->unary chiStrichFib (g(getCantorX z)))))] (g(getCantorX z)))
(else "abb"))))
</code>
<run id="57179d76eb3e7">
(aufzaehlKA 5)
</run>