#lang scheme/base
(require "base.ss" scheme/list)
(define (assoc/cdr key alist (default #f))
(if-it (assoc key alist)
(cdr it)
default))
(define (assoc/s key alist (default '()))
(let ((it (filter (lambda (kv)
(equal? (car kv) key))
alist)))
(if (null? it) default it)))
(define (assoc* key lst (default #f))
(define (helper rest)
(cond ((null? rest) default)
((and (pair? (car rest))
(equal? key (caar rest)))
(car rest))
((and (not (pair? (car rest)))
(equal? key (car rest)))
rest)
(else
(helper (cdr rest)))))
(helper lst))
(define (assoc*/cdr key lst (default #f))
(if-it (assoc* key lst)
(cdr it)
default))
(define (group alist)
(foldl (lambda (kv interim)
(if-it (assoc (car kv) interim) (cons (cons (car it) (cons (cdr kv) (cdr it)))
(filter (lambda (kv)
(not (equal? it kv))) interim))
(cons (list (car kv) (cdr kv)) interim)))
'()
alist))
(define (list->unique lst (equal? equal?))
(reverse (foldl (lambda (item interim)
(if (memf (lambda (item1)
(equal? item item1))
interim)
interim
(cons item interim)))
'()
lst)))
(provide/contract
(assoc/cdr (->* (any/c list?)
(any/c)
any))
(assoc/s (->* (any/c list?)
(any/c)
any))
(assoc* (->* (any/c list?)
(any/c)
any))
(assoc*/cdr (->* (any/c list?)
(any/c)
any))
(group (-> (or/c null? pair?) any))
(list->unique (->* (pair?)
(procedure?)
any))
)
(provide (all-from-out scheme/list))