(define (map-bites make-bite bite-transf lst)
(map-bites-1 make-bite bite-transf lst 1 '()))
(define (map-bites-1 make-bite bite-transf lst i res-lst)
(cond ((null? lst) (apply append (reverse res-lst)))
(else (let ((bite (make-bite lst i)))
(if (null? bite) (laml-error "map-bites-1: Encountered an empty bite") 'do-nothing)
(map-bites-1 make-bite bite-transf (list-tail-flex lst (length bite)) (+ i 1) (cons (bite-transf bite) res-lst))))))
(define (map-n-bites make-bite bite-transf lst)
(map-n-bites-1 make-bite bite-transf lst 1 '()))
(define (map-n-bites-1 make-bite bite-transf lst i res-lst)
(cond ((null? lst) (apply append (reverse res-lst)))
(else (let ((bite (make-bite lst i)))
(if (null? bite) (laml-error "map-bites-1: Encountered an empty bite") 'do-nothing)
(map-n-bites-1 make-bite bite-transf (list-tail-flex lst (length bite)) (+ i 1) (cons (bite-transf bite i) res-lst) )))))
(define (filter-bites make-bite bite-pred lst)
(filter-map-bites-1 make-bite bite-pred id-1 lst 1 '()))
(define (filter-map-bites make-bite bite-pred bite-transf lst)
(filter-map-bites-1 make-bite bite-pred bite-transf lst 1 '()))
(define (filter-map-bites-1 make-bite bite-pred bite-transf lst i res-lst)
(cond ((null? lst) (apply append (reverse res-lst)))
(else (let ((bite (make-bite lst i)))
(if (null? bite) (laml-error "filter-map-bites-1: Encountered an empty bite") 'do-nothing)
(if (bite-pred bite)
(filter-map-bites-1 make-bite bite-pred bite-transf (list-tail-flex lst (length bite))
(+ i 1) (cons (bite-transf bite) res-lst))
(filter-map-bites-1 make-bite bite-pred bite-transf (list-tail-flex lst (length bite))
(+ i 1) res-lst))))))
(define (step-and-map-bites make-bite bite-pred bite-transf lst)
(step-and-map-bites-1 make-bite bite-pred bite-transf lst (length lst) 1 '()))
(define (step-and-map-bites-1 make-bite bite-pred bite-transf lst lst-lgt i res-lst)
(cond ((<= lst-lgt 0) (reverse res-lst))
((null? lst) (reverse res-lst))
(else (let* ((first-bite (make-bite lst i))
(selection-count (bite-pred first-bite))
)
(cond ((< selection-count 0)
(step-and-map-bites-1 make-bite bite-pred bite-transf (list-tail-flex lst (- selection-count)) (+ lst-lgt selection-count)
i (append (reverse (list-part 1 (- selection-count) lst)) res-lst)))
((> selection-count 0)
(let ((trans-res (bite-transf first-bite)))
(step-and-map-bites-1 make-bite bite-pred bite-transf (list-tail-flex lst selection-count) (- lst-lgt selection-count)
(+ i 1) (append (reverse trans-res) res-lst))))
(((= selection-count 0) (laml-error "step-and-map-bites-1: Illegal filter result."))))))))
(define (step-and-map-n-bites make-bite bite-pred bite-transf lst)
(step-and-map-n-bites-1 make-bite bite-pred bite-transf lst (length lst) 1 '()))
(define (step-and-map-n-bites-1 make-bite bite-pred bite-transf lst lst-lgt i res-lst)
(cond ((<= lst-lgt 0) (reverse res-lst))
((null? lst) (reverse res-lst))
(else (let* ((first-bite (make-bite lst i))
(selection-count (bite-pred first-bite))
)
(cond ((< selection-count 0)
(step-and-map-n-bites-1 make-bite bite-pred bite-transf (list-tail-flex lst (- selection-count)) (+ lst-lgt selection-count)
i (append (reverse (list-part 1 (- selection-count) lst)) res-lst)))
((> selection-count 0)
(let ((trans-res (bite-transf first-bite i)))
(step-and-map-n-bites-1 make-bite bite-pred bite-transf (list-tail-flex lst selection-count) (- lst-lgt selection-count)
(+ i 1) (append (reverse trans-res) res-lst))))
(((= selection-count 0) (laml-error "step-and-map-n-bites-1: Illegal filter result."))))))))
(define (bite-of-length n . optional-parameters)
(let ((noise-element (optional-parameter 1 optional-parameters (lambda (el) #f))))
(lambda (lst . optional-parameters)
(bite-of-length-1 n 0 noise-element lst '()))))
(define (bite-of-length-1 n i noise-element lst res-lst)
(cond ((null? lst) (reverse res-lst))
((= i n) (reverse res-lst))
((noise-element (car lst)) (bite-of-length-1 n i noise-element (cdr lst) (cons (car lst) res-lst)))
(else (bite-of-length-1 n (+ i 1) noise-element (cdr lst) (cons (car lst) res-lst)))))
(define (bite-of-varied-length f . optional-parameters)
(let ((noise-element (optional-parameter 1 optional-parameters (lambda (el) #f))))
(lambda (lst bite-number)
(bite-of-length-1 (f bite-number) 0 noise-element lst '()))))
(define (bite-of-varied-length-1 f bite-number i noise-element lst res-lst)
(cond ((null? lst) (reverse res-lst))
((= i (f bite-number)) (reverse res-lst))
((noise-element (car lst)) (bite-of-varied-length-1 f bite-number i noise-element (cdr lst) (cons (car lst) res-lst)))
(else (bite-of-varied-length-1 f bite-number (+ i 1) noise-element (cdr lst) (cons (car lst) res-lst)))))
(define (bite-while-element el-pred . attributes)
(let ((sentinel (as-symbol (defaulted-get-prop 'sentinel attributes "last"))))
(cond ((eq? sentinel 'last)
(lambda (lst . optional-parameters) (bite-while-element-sentinel-last el-pred lst '())))
((eq? sentinel 'first)
(lambda (lst . optional-parameters) (bite-while-element-sentinel-first el-pred lst '())))
((eq? sentinel 'alone)
(lambda (lst . optional-parameters) (bite-while-element-sentinel-alone el-pred lst '() 0)))
(else (laml-error "bite-while-element: Unknown attribute in trailing property list. Must be first or last. Is:" sentinel)))))
(define (bite-while-element-sentinel-last el-pred lst res-lst)
(cond ((null? lst) (reverse res-lst))
((el-pred (car lst)) (bite-while-element-sentinel-last el-pred (cdr lst) (cons (car lst) res-lst)))
(else (reverse (cons (car lst) res-lst)))))
(define (bite-while-element-sentinel-first el-pred lst res-lst)
(cond ((and (null? res-lst) (not (null? lst))) (bite-while-element-sentinel-first el-pred (cdr lst) (cons (car lst) res-lst)))
((null? lst) (reverse res-lst))
((el-pred (car lst)) (bite-while-element-sentinel-first el-pred (cdr lst) (cons (car lst) res-lst)))
(else (reverse res-lst))))
(define (bite-while-element-sentinel-alone el-pred lst res-lst level)
(cond ((null? lst) (reverse res-lst))
((and (= level 0) (not (el-pred (car lst)))) (list (car lst)))
((el-pred (car lst)) (bite-while-element-sentinel-alone el-pred (cdr lst) (cons (car lst) res-lst) (+ level 1)))
(else (reverse res-lst))))
(define (bite-while-element-with-accumulation pred accumulator init-acc-val . optional-parameters)
(let ((noise-element (optional-parameter 1 optional-parameters (lambda (el) #f))))
(lambda (lst . optional-parameters)
(cond ((null? lst) '())
(else (bite-while-element-with-accumulation-1 pred accumulator init-acc-val
(if (noise-element (car lst)) init-acc-val (accumulator init-acc-val (car lst)))
noise-element (cdr lst) (list (car lst))))))))
(define (bite-while-element-with-accumulation-1 pred accumulator init-acc-val cur-acc-val noise-element lst res-lst)
(cond ((null? lst) (reverse res-lst))
(else (let ((el (car lst))
(rest (cdr lst)))
(cond ((noise-element el)
(bite-while-element-with-accumulation-1 pred accumulator init-acc-val cur-acc-val noise-element rest (cons el res-lst)))
((pred el cur-acc-val)
(bite-while-element-with-accumulation-1 pred accumulator init-acc-val (accumulator cur-acc-val el) noise-element rest (cons el res-lst)))
(else (reverse res-lst)))))))
(define (bite-while-prefix bite-pred)
(lambda (lst . optional-parameters)
(let ((bite-number (optional-parameter 1 optional-parameters #f)))
(bite-while-prefix-1 bite-pred lst 2 bite-number (length lst)))
))
(define (bite-while-prefix-1 bite-pred lst i bite-number lst-lgt)
(cond ((> i lst-lgt) lst)
((bite-pred (front-sublist lst i) bite-number)
(bite-while-prefix-1 bite-pred lst (+ i 1) bite-number lst-lgt))
((= i 1) (laml-error "The bite predicate does not accept at least a bite of unity length"))
(else (front-sublist lst (- i 1)))))
(define (bite-while-accumulate bin-op init-val pred . optional-parameters)
(let ((noise-element (optional-parameter 1 optional-parameters (lambda (el) #f))))
(lambda (lst . optional-parameters)
(if (null? lst)
'()
(let ((el (car lst)))
(bite-while-accumulate-1 bin-op init-val pred (cdr lst) (if (noise-element el) init-val (bin-op init-val (car lst))) noise-element (list el)))))))
(define (bite-while-accumulate-1 bin-op init-val pred lst cur-val noise-element res-lst)
(cond ((null? lst) (reverse res-lst))
((pred cur-val) (reverse res-lst))
((noise-element (car lst)) (bite-while-accumulate-1 bin-op init-val pred (cdr lst) cur-val noise-element (cons (car lst) res-lst)))
(else (bite-while-accumulate-1 bin-op init-val pred (cdr lst) (bin-op cur-val (car lst)) noise-element (cons (car lst) res-lst)))))
(define (bite-while-compare el-relation . optional-parameters)
(let ((noise-element (optional-parameter 1 optional-parameters (lambda (el) #f))))
(lambda (lst . optional-parameters)
(let ((bite-number (optional-parameter 1 optional-parameters #f)))
(cond ((null? lst) '())
((null? (cdr lst)) lst)
(else (bite-while-compare-1 el-relation noise-element (car lst) (not (noise-element (car lst))) (car lst) (cdr lst) bite-number)))))))
(define (bite-while-compare-1 el-relation noise-element first remembered? remember-el non-empty-rest bite-number)
(cons first
(cond ((and (null? (cdr non-empty-rest)) remembered? (not (noise-element (car non-empty-rest))) (not (el-relation remember-el (car non-empty-rest))))
'())
((null? (cdr non-empty-rest))
non-empty-rest)
((and remembered? (not (noise-element (car non-empty-rest))) (el-relation remember-el (car non-empty-rest)))
(bite-while-compare-1 el-relation noise-element (car non-empty-rest)
#t (car non-empty-rest) (cdr non-empty-rest) bite-number))
((and remembered? (not (noise-element (car non-empty-rest))) )
'())
(else
(bite-while-compare-1 el-relation noise-element (car non-empty-rest)
(if remembered? remembered? (not (noise-element (car non-empty-rest))))
(if remembered? remember-el (car non-empty-rest)) (cdr non-empty-rest) bite-number)))))
(define (bite-while-monotone el-comparator . optional-parameters)
(let ((noise-element (optional-parameter 1 optional-parameters (lambda (el) #f))))
(lambda (lst . optional-parameters)
(let ((bite-number (optional-parameter 1 optional-parameters #f)))
(cond ((null? lst) '())
((null? (cdr lst)) lst)
(else (bite-while-monotone-1 el-comparator noise-element
(car lst) (not (noise-element (car lst))) (car lst) #f #f #f
(cdr lst) bite-number)))))))
(define (bite-while-monotone-1 el-comparator noise? first e1? e1 e2? e2 direction non-empty-rest bite-number)
(cons first
(cond
((and (null? (cdr non-empty-rest)) e2? (not (noise? (car non-empty-rest))) direction (not (= (el-comparator e2 (car non-empty-rest)) direction)))
'())
((null? (cdr non-empty-rest))
non-empty-rest)
(else (let ((e (car non-empty-rest))
(nr (cdr non-empty-rest)))
(cond
((and (not e1?) (not e2?) (noise? e))
(bite-while-monotone-1 el-comparator noise? e #f #f #f #f #f nr bite-number))
((and (not e1?) (not e2?) (not (noise? e)))
(bite-while-monotone-1 el-comparator noise? e #t e #f #f #f nr bite-number))
((and e1? (not e2?) (noise? e) )
(bite-while-monotone-1 el-comparator noise? e #t e1 #f #f #f nr bite-number))
((and e1? (not e2?) (not (noise? e)) (not direction))
(bite-while-monotone-1 el-comparator noise? e #t e2 #t e (el-comparator e1 e) nr bite-number))
((and e1? e2? (not (noise? e)) direction (not (= (el-comparator e2 e) direction)) )
'())
((and e1? e2? (not (noise? e)) direction (= (el-comparator e2 e) direction))
(bite-while-monotone-1 el-comparator noise? e #t e2 #t e direction nr bite-number))
((and e1? e2? (noise? e) direction)
(bite-while-monotone-1 el-comparator noise? e #t e1 #t e2 direction nr bite-number))
(else (laml-error "H"))
))))))
(define (laml-error . messages)
(error (laml-aggregate-messages messages)))
(define (laml-aggregate-messages message-list)
(string-merge
(map as-string message-list)
(make-list (- (length message-list) 1) " ")))
(define (list-tail-flex lst n)
(cond ((= n 0) lst)
((null? lst) '())
(else (list-tail-flex (cdr lst) (- n 1)))))
(define (id-1 x) x)
(define (optional-parameter n optional-parameter-list . optional-default-value)
(let ((optional-default-value-1 (if (null? optional-default-value) #f (car optional-default-value))))
(if (> n (length optional-parameter-list))
optional-default-value-1
(let ((candidate-value (list-ref optional-parameter-list (- n 1))))
(if (eq? candidate-value 'non-passed-value)
optional-default-value-1
candidate-value)))))
(define (as-symbol x)
(cond ((symbol? x) x)
((string? x) (string->symbol x))
((boolean? x)
(if x (as-symbol "true") (as-symbol "false")))
((char? x) (as-symbol (char->string x)))
(else #f)))
(define (as-string x)
(cond ((number? x) (number->string x))
((symbol? x) (symbol->string x))
((string? x) x)
((boolean? x)
(if x "true" "false"))
((char? x) (char->string x))
((list? x)
(string-append "("
(string-merge (map as-string x) (make-list (- (length x) 1) " "))
")"))
((vector? x)
(let ((lst (vector->list x)))
(string-append "#("
(string-merge (map as-string lst) (make-list (- (length lst) 1) " "))
")")))
((pair? x)
(string-append "("
(apply string-append
(map (lambda (y) (string-append (as-string y) " ")) (proper-part x))
)
" . " (as-string (first-improper-part x))
")"))
(else "??")))
(define (make-comparator lt gt)
(lambda (e1 e2)
(cond ((lt e1 e2) -1)
((gt e1 e2) 1)
(else 0))))
(define (defaulted-get-prop key p-list default)
(let ((res (find-in-property-list key p-list)))
(if res
(if (not (null? (cdr res)))
(cadr res)
(laml-error "Illformed property list:" (as-string p-list)))
default)))
(define (find-in-property-list key p-list)
(cond ((null? p-list) #f)
((eq? key (car p-list)) p-list)
((not (null? (cdr p-list))) (find-in-property-list key (cddr p-list)))
(else (laml-error "Illformed property list:" (as-string p-list)))))
(define (negate p)
(lambda (x)
(if (p x) #f #t)))
(define (sublist-until until-fn lst)
(sublist-until-1 until-fn lst '())
)
(define (sublist-until-1 until-fn lst res-lst)
(cond ((null? lst) (reverse res-lst))
((until-fn (car lst)) (reverse (cons (car lst) res-lst)))
(else (sublist-until-1 until-fn (cdr lst) (cons (car lst) res-lst)))))
(define (list-part a b lst)
(list-part-help a b lst 1 (length lst) '()))
(define (list-part-help a b lst i lgt res)
(cond ((> i lgt) (reverse res))
((> i b) (reverse res))
((and (>= i a) (<= i b) (not (null? lst))) (list-part-help a b (cdr lst) (+ i 1) lgt (cons (car lst) res)))
((and (<= i a) (not (null? lst))) (list-part-help a b (cdr lst) (+ i 1) lgt res))
((null? lst) (error (string-append "list-part error: " (as-string i))))))
(define (list-to-string lst separator)
(string-merge
(map as-string lst)
(make-list (- (length lst) 1) separator)))
(define (butlast lst)
(reverse (cdr (reverse lst))))
(define (last lst)
(car (reverse lst)))
(define (string-merge str-list-1 str-list-2)
(cond ((null? str-list-1) (apply string-append str-list-2))
((null? str-list-2) (apply string-append str-list-1))
(else (string-append
(car str-list-1) (car str-list-2)
(string-merge (cdr str-list-1) (cdr str-list-2))))))
(define (make-list n el)
(if (<= n 0) '() (cons el (make-list (- n 1) el))))
(define (filter pred lst)
(reverse (filter-help pred lst '())))
(define (filter-help pred lst res)
(cond ((null? lst) res)
((pred (car lst)) (filter-help pred (cdr lst) (cons (car lst) res)))
(else (filter-help pred (cdr lst) res))))