======K4b.scm======
<code shell>#!/usr/bin/env racket
#lang racket/base
</code>
====== fac ======
<code scheme>
(define ! (lambda (n)
(if (= n 0) 1
(* n (! (- n 1))))))
</code>
====== fac-iter ======
<code scheme>
(define !iter (lambda (a i)
(if (= i 0) a
(!iter (* a i) (- i 1)))))
(define !* (lambda(n) (!iter 1 n)))
(define !iter (lambda (a i)
(display "a: ")
(display a)
(newline)
(display "i: ")
(display i)
(newline)
(newline)
(if (= i 0) a
(!iter (* a i) (- i 1)))))
</code>
====== merge ======
<code scheme>
(define merge
(lambda (l1 l2 order)
(cond ((null? l1) l2)
((null? l2) l1)
((order (car l1)
(car l2))
(cons (car l1) (merge (cdr l1)
l2 order)))
(else (cons (car l2) (merge (cdr l2)
l1 order))))))
</code>
====== split ======
<code scheme>
(define split
(lambda (l)
(split-pomocna l () ())))
</code>
====== split-pomocna ======
<code scheme>
(define split-pomocna
(lambda (l l1 l2)
(if (null? l) (cons l1 l2)
(split-pomocna (cdr l)
(cons (car l) l2)
l1))))
</code>
====== mergesort ======
<code scheme>
(define mergesort
(lambda (l order)
(if (or (null? l) (null? (cdr l))) l
(let* ((l1l2 (split l))
(l1 (car l1l2))
(l2 (cdr l1l2)))
(merge (mergesort l1 order)
(mergesort l2 order) order)))))
</code>
====== abc-string ======
<code scheme>
(define abc-string "aábcčdďeéěfghiíjklmnňoópqrřsštťuůúvwxyýzž")
</code>
====== abc-assoc1 ======
<code scheme>
(define abc-assoc1
(let* ((lst (string->list abc-string))
(len (length lst)))
(map
cons
lst
(build-list len +))))
</code>
====== my-assoc2 ======
<code scheme>
(define my-assoc2
(lambda (e l)
(cond ((null? l) #f)
((equal? e (caar l)) (car l))
(else (my-assoc e (cdr l))))))
</code>
====== my-assoc ======
<code scheme>
(define my-assoc
(lambda (e l)
(if (null? l) #f
(if (equal? e (caar l)) (car l)
(my-assoc e (cdr l))))))
</code>
====== index-of ======
<code scheme>
(define index-of (lambda (c)
(cdr (my-assoc c abc-assoc))))
</code>
====== compare-strings-cz ======
<code scheme>
(define compare-strings-cz
(lambda (s1 s2)
(compare-strings-cz-aux
(string->list s1)
(string->list s2)
)))
</code>
====== compare-strings-cz-aux ======
<code scheme>
(define compare-strings-cz-aux
(lambda (l1 l2)
(cond ((null? l1) #t)
((null? l2) #f)
((< (index-of (car l1))
(index-of (car l2))) #t)
((> (index-of (car l1))
(index-of (car l2))) #f)
(else (compare-strings-cz-aux
(cdr l1)
(cdr l2))))))
(compare-strings-cz "svete" "áhoj" )
</code>
====== merge-sort ======
<code scheme>
(mergesort '("ahoj" "člověče" "xxx" "ššš")
compare-strings-cz)
; vim: syntax=racket
</code>