From 46167adb2c0f79b2027e9d5cd656fdc289a2d849 Mon Sep 17 00:00:00 2001 From: Inga Date: Mon, 4 Dec 2023 12:05:04 +0000 Subject: [PATCH] simplified compose --- day01-easy/main.scm | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/day01-easy/main.scm b/day01-easy/main.scm index 83b2d07..da2a592 100755 --- a/day01-easy/main.scm +++ b/day01-easy/main.scm @@ -12,20 +12,19 @@ (define (id value) value) -(define (compose result-transformer f argument-transformer) - (lambda (value) - (result-transformer (f (argument-transformer value))))) - (define Y (lambda (f) (f (lambda (x) ((Y f) x))))) (define (reduce-right reducer initial) (Y (lambda (f) (lambda (list) (if (null? list) initial (reducer (car list) (f (cdr list)))))))) -(define (map mapper) - (reduce-right - (lambda (current accumulator) (cons (mapper current) accumulator)) - '())) +(define (map mapper) (reduce-right + (lambda (current accumulator) (cons (mapper current) accumulator)) + '())) + +(define compose (reduce-right + (lambda (current accumulator) (lambda (value) (current (accumulator value)))) + id)) (define (combine combiner) (lambda (a b) (if (null? a) b (if (null? b) a (combiner a b))))) @@ -36,20 +35,17 @@ (lambda (f) (lambda (n) (if (= n 0) '() (cons value (f (- n 1)))))))) -(define (is-numeric-char char) (if (char-numeric? char) #t #f)) - -(define (first-last predicate) (compose - id +(define solve-line (compose (list + string->number + list->string (reduce-right (combine (lambda (left right) (cons (car left) (cdr right)))) '()) - (map (lambda (entry) (if (predicate entry) ((repeat entry) 2) '()))))) - -(define solve-line (compose - (lambda (first-last-result) (string->number (list->string first-last-result))) - (first-last is-numeric-char) - string->list)) + (map (lambda (char) (if (char-numeric? char) ((repeat char) 2) '()))) + string->list))) -(define (solve-all lines) (sum ((map solve-line) lines))) +(define solve-all (compose (list + sum + (map solve-line)))) (display (solve-all (read-lines)))