-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiinsertcollect.rkt
20 lines (17 loc) · 1.01 KB
/
multiinsertcollect.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#lang racket
(require "atom.rkt" "equal.rkt")
(define (multi-insert-left-right-collect new left right things collect)
(cond
[(null? things) (collect (list) 0 0)]
[(equal? (car things) left)
(multi-insert-left-right-collect new left right (cdr things)
(lambda (new-things num-left num-right)
(collect (cons new (cons (car things) new-things)) (add1 num-left) num-right)))]
[(equal? (car things) right)
(multi-insert-left-right-collect new left right (cdr things)
(lambda (new-things num-left num-right)
(collect (cons (car things) (cons new new-things)) num-left (add1 num-right))))]
[else (multi-insert-left-right-collect new left right (cdr things)
(lambda (new-things num-left num-right)
(collect (cons (car things) new-things) num-left num-right)))]))
(multi-insert-left-right-collect 5 1 2 (list 1 2 3 1 2 3 1 2 3) (lambda (new-things num-left num-right) (cons new-things (cons num-left (cons num-right (list))))))