|
|
@ -10,22 +10,22 @@ |
|
|
|
'() |
|
|
|
'() |
|
|
|
(cons line (loop (read-line)))))) |
|
|
|
(cons line (loop (read-line)))))) |
|
|
|
|
|
|
|
|
|
|
|
(define (map list mapper) |
|
|
|
(define (reduce-right reducer initial list) |
|
|
|
(if |
|
|
|
(if |
|
|
|
(null? list) |
|
|
|
(null? list) |
|
|
|
'() |
|
|
|
initial |
|
|
|
(cons (mapper (car list)) (map (cdr list) mapper)))) |
|
|
|
(reducer (car list) (reduce-right reducer initial (cdr list))))) |
|
|
|
|
|
|
|
|
|
|
|
(define (reduce list reducer current) |
|
|
|
(define (map mapper list) |
|
|
|
(if |
|
|
|
(reduce-right |
|
|
|
(null? list) |
|
|
|
(lambda (current accumulator) (cons (mapper current) accumulator)) |
|
|
|
current |
|
|
|
'() |
|
|
|
(reduce (cdr list) reducer (reducer current (car list))))) |
|
|
|
list)) |
|
|
|
|
|
|
|
|
|
|
|
(define (combine a b combiner) |
|
|
|
(define (combine combiner a b) |
|
|
|
(if (null? a) b (if (null? b) a (combiner a b)))) |
|
|
|
(if (null? a) b (if (null? b) a (combiner a b)))) |
|
|
|
|
|
|
|
|
|
|
|
(define (sum numbers) (reduce numbers + 0)) |
|
|
|
(define (sum numbers) (reduce-right + 0 numbers)) |
|
|
|
|
|
|
|
|
|
|
|
(define |
|
|
|
(define |
|
|
|
(repeat value number) |
|
|
|
(repeat value number) |
|
|
@ -36,23 +36,25 @@ |
|
|
|
|
|
|
|
|
|
|
|
(define (is-numeric-char char) (if (char-numeric? char) #t #f)) |
|
|
|
(define (is-numeric-char char) (if (char-numeric? char) #t #f)) |
|
|
|
|
|
|
|
|
|
|
|
(define (first-last list predicate) |
|
|
|
(define (first-last predicate list) |
|
|
|
(reduce |
|
|
|
(reduce-right |
|
|
|
(map list (lambda (entry) |
|
|
|
|
|
|
|
(if (predicate entry) (repeat entry 2) '()))) |
|
|
|
|
|
|
|
(lambda (current accumulator) |
|
|
|
(lambda (current accumulator) |
|
|
|
(combine |
|
|
|
(combine |
|
|
|
|
|
|
|
(lambda (left right) (cons (car left) (cdr right))) |
|
|
|
current |
|
|
|
current |
|
|
|
accumulator |
|
|
|
accumulator)) |
|
|
|
(lambda (current rest) (cons (car current) (cdr rest))))) |
|
|
|
'() |
|
|
|
'())) |
|
|
|
(map |
|
|
|
|
|
|
|
(lambda (entry) |
|
|
|
|
|
|
|
(if (predicate entry) (repeat entry 2) '())) |
|
|
|
|
|
|
|
list))) |
|
|
|
|
|
|
|
|
|
|
|
(define (solve-line line) |
|
|
|
(define (solve-line line) |
|
|
|
((lambda (first-last-result) |
|
|
|
((lambda (first-last-result) |
|
|
|
(string->number (list->string first-last-result))) |
|
|
|
(string->number (list->string first-last-result))) |
|
|
|
(first-last (string->list line) is-numeric-char))) |
|
|
|
(first-last is-numeric-char (string->list line)))) |
|
|
|
|
|
|
|
|
|
|
|
(define (solve-all lines) |
|
|
|
(define (solve-all lines) |
|
|
|
(sum (map lines solve-line))) |
|
|
|
(sum (map solve-line lines))) |
|
|
|
|
|
|
|
|
|
|
|
(display (solve-all (read-lines))) |
|
|
|
(display (solve-all (read-lines))) |
|
|
|