;; ;; Collection.egi ;; (define $list (lambda [$a] (matcher {[,$val [] {[$tgt (match [val tgt] [(list a) (list a)] {[[ ] {[]}] [[ ] {[]}] [[_ _] {}]})]}] [ [] {[{} {[]}] [_ {}]}] [ [a (list a)] {[{$x @$xs} {[x xs]}] [_ {}]}] [ [a (list a)] {[{@$xs $x} {[x xs]}] [_ {}]}] [ [(list a)] {[$tgt (letrec {[$helper (lambda [$pxs $tgt] (match [pxs tgt] [(list a) (list a)] {[[ _] {tgt}] [[ ] (helper xs ys)] [[_ _] {}]}))]} (helper pxs tgt))]}] [ [(list a) (list a)] {[$tgt (letrec {[$helper (lambda [$xs $ys] (match ys (list a) {[ {[xs {}]}] [ {[xs ys] @(helper {@xs z} zs)}]}))]} (helper {} tgt))]}] [ [(list a)] {[$tgt (letrec {[$helper (lambda [$pxs $tgt] (match [pxs tgt] [(list a) (list a)] {[[ _] {tgt}] [[ ] (helper xs ys)] [[_ _] {}]}))]} (helper pxs tgt))]}] [ [(list a) (list a)] {[$tgt (letrec {[$helper (lambda [$xs $ys] (match ys (list a) {[ {[{} xs]}] [ {[ys xs] @(helper {@xs z} zs)}]}))]} (helper {} tgt))]}] [$ [something] {[$tgt {tgt}]}] }))) (define $map (lambda [$fn $ls] (match ls (list something) {[ {}] [ {(fn x) @(map fn xs)}]}))) (define $map-and-concat (lambda [$fn $ls] (match ls (list something) {[ {}] [ {@(fn x) @(map-and-concat fn xs)}]}))) (define $foldr (lambda [$fn $init $ls] (match ls (list something) {[ init] [ (fn x (foldr fn init xs))]}))) (define $foldl (lambda [$fn $init $ls] (match ls (list something) {[ init] [ (let {[$y (fn init x)]} (foldl fn y xs))]}))) (define $filter (lambda [$pred $ls] (match ls (list something) {[ {}] [ (if (pred x) {x @(filter pred xs)} (filter pred xs))]}))) (define $split (lambda [$pred $ls] (letrec {[$helper (lambda [$ls $xs $ys] (match ls (list something) {[ [xs ys]] [ (helper rs {l @xs} ys)] [ (helper rs xs {l @ys})]}))]} (helper ls {} {})))) (define $remove (lambda [$a] (lambda [$xs $x] (match xs (list a) {[ {}] [ rs] [ {y @((remove a) rs x)}]})))) (define $remove-all (lambda [$a] (lambda [$xs $x] (match xs (list a) {[ {}] [ ((remove-all a) rs x)] [ {y @((remove-all a) rs x)}]})))) (define $remove-collection (lambda [$a] (lambda [$xs $ys] (match ys (list a) {[ xs] [ ((remove-collection a) ((remove a) xs y) rs)]})))) (define $add (lambda [$a] (lambda [$xs $x] (if ((member? a) x xs) xs {@xs x})))) (define $union (lambda [$a] (lambda [$xs $ys] (match ys (list something) {[ xs] [ ((union a) ((add a) xs y) rs)]})))) (define $occurrence (lambda [$a] (lambda [$xs] (letrec {[$remove-and-count (lambda [$x $xs] (match xs (list a) {[ [{} 1]] [ (let {[[$r $i] (remove-and-count x ys)]} [r (+ i 1)])] [ (let {[[$r $i] (remove-and-count x ys)]} [{y @r} i])]}))]} (match xs (list something) {[ {}] [ (let {[[$rs $i] (remove-and-count x ys)]} {[x i] @((occurrence a) rs)})]}))))) #| (define $subcollections (lambda [$xs] (foldr (lambda [$x $rs] (let {[[$y $i] x]} (map-and-concat (lambda [$sub] (match-all (index-loop $l $j (between 1 i) {y @l} {}) (list something) [ {@ys @sub}])) rs))) {{}} ((occurrence a) xs)))) |# (define $one-of (lambda [$pred $xs] (match xs (list something) {[ #f] [ (if (pred x) #t (one-of pred rs))]}))) (define $size (lambda [$xs] (match xs (list something) {[ 0] [ (+ 1 (size rs))]}))) (define $car (lambda [$xs] (match xs (list something) {[ x]}))) (define $cdr (lambda [$xs] (match xs (list something) {[ ys]}))) (define $rac (lambda [$xs] (match xs (list something) {[ x]}))) (define $rdc (lambda [$xs] (match xs (list something) {[ ys]}))) (define $nth (lambda [$i $l] (match l (list something) {[ (if (eq? i 0) x (nth (- i 1) xs))]}))) (define $take (lambda [$n $xs] (if (eq? n 0) {} (match xs (list something) {[ {x @(take (- n 1) xs)}] [ {}]})))) (define $drop (lambda [$n $xs] (if (eq? n 0) xs (match xs (list something) {[ (drop (- n 1) xs)] [ {}]})))) (define $reverse (lambda [$xs] (match xs (list something) {[ {}] [ {@(reverse rs) x}]}))) (define $member? (lambda [$a] (lambda [$x $ys] (match ys (list a) {[ #f] [ #t] [ ((member? a) x ys)]})))) (define $unique (lambda [$a] (lambda [$xs] (letrec {[$loop-fn (lambda [$xs $ys] (match xs (list something) {[ ys] [ (if ((member? a) x ys) (loop-fn rs ys) (loop-fn rs {@ys x}))]}))]} (loop-fn xs {}))))) (define $subcollection? (lambda [$a] (lambda [$xs $ys] (match xs (list something) {[ #t] [ (if ((member? a) x ys) ((subcollection? a) rest ys) #f)]})))) (define $concat (lambda [$xs] (match xs (list something) {[ {}] [ {@x @(concat rs)}]}))) (define $empty? (match-lambda (list something) {[ #t] [ #f]})) (define $multiset (lambda [$a] (matcher {[,$val [] {[$tgt (match [val tgt] [(list a) (multiset a)] {[[ ] {[]}] [[ ] {[]}] [[_ _] {}]})]}] [ [] {[{} {[]}] [_ {}]}] [ [(multiset a)] {[$tgt (match tgt (list a) {[> {{@hs @ts}}] [_ {}]})]}] [ [a (multiset a)] {[$tgt (match-all tgt (list a) [> [x {@hs @ts}]])]}] [ [(multiset a)] {[$tgt {((remove-collection a) tgt pxs)}]}] [$ [something] {[$tgt {tgt}]}] }))) (define $set (lambda [$a] (matcher { [ [] {[{} {[]}] [_ {}]}] [ [(set a)] {[$tgt (match tgt (list a) {[> {tgt}] [_ {}]})]}] [ [a (set a)] {[$tgt (match-all tgt (list a) [> [x tgt]])]}] [$ [something] {[$tgt {tgt}]}] })))