#!/usr/bin/env racket
#lang racket/base
viz assoc
assoc 1)
(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