diff --git a/day05-hard/main.scm b/day05-hard/main.scm index 3ed6fae..2c0c0b3 100755 --- a/day05-hard/main.scm +++ b/day05-hard/main.scm @@ -25,13 +25,6 @@ (define (is-not value) (lambda (x) (not (equal? x value)))) (define (is-not-empty value) (not (null? value))) -;=============== prepend ====================== -(define (prepend first-value) (lambda (rest) (cons first-value rest))) - -(assert-eq "prepend test failed" - '(1 2 3 4) - ((prepend 1) '(2 3 4))) - ;=============== combinators ====================== (define (Y f) (f (lambda (x) ((Y f) x)))) (define (Y2 f) (f (lambda (x y) ((Y2 f) x y)))) @@ -72,6 +65,20 @@ (lambda (current accumulator) (cons (+ 1 (car accumulator)) (cons current accumulator)))) '(101 102 103 104))) +;=============== prepend ====================== +(define (prepend first-value) (lambda (rest) (cons first-value rest))) + +(assert-eq "prepend test failed" + '(1 2 3 4) + ((prepend 1) '(2 3 4))) + +;=============== append ====================== +(define (append first-value) (reduce-right (list first-value) cons)) + +(assert-eq "append test failed" + '(1 2 3 4) + ((append 4) '(1 2 3))) + ;=============== reverse ====================== (define reverse (reduce-left '() cons)) @@ -433,25 +440,22 @@ (get-range-start state-value) (get-range-end state-value))) -(define (seed-maps-processor seed-maps) +(define seed-maps-processor (compose (list - ;; for state - #!(tee-with-comment "state after applying mapset")!# - ((compose (list - ;; for list of maps (within a single map set); returns state-transforming lambda - compose - (map (lambda (seed-map) - ;; for seed map within the list: returns state-transforming lambda - (compose (list - flat - (map (lambda (state-entry) - ;; for seed map _and_ single range from state, returns list of new ranges - (if - (is-state-value-current state-entry) - (list state-entry) - (apply-map-to-previous-state-value seed-map (get-state-value state-entry))))))))))) - seed-maps) - swap-state))) + ;; for list of maps (within a single map set); returns state-transforming lambda + compose + #!(prepend (tee-with-comment "state after applying mapset"))!# + (append swap-state) + (map (lambda (seed-map) + ;; for seed map within the list: returns state-transforming lambda + (compose (list + flat + (map (lambda (state-entry) + ;; for seed map _and_ single range from state, returns list of new ranges + (if + (is-state-value-current state-entry) + (list state-entry) + (apply-map-to-previous-state-value seed-map (get-state-value state-entry)))))))))))) (define parse-seed-maps (compose (list