Solutions of some puzzles in Scheme (Lisp), my first experience with it.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.1 KiB

12 months ago
#!/usr/bin/guile -s
!#
(use-modules (ice-9 rdelim))
(define (read-lines)
((lambda (line)
(if (eof-object? line) '() (cons line (read-lines))))
(read-line)))
12 months ago
(define (id value) value)
12 months ago
(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))))))))
12 months ago
(define (map mapper) (reduce-right
(lambda (current accumulator) (cons (mapper current) accumulator))
'()))
12 months ago
(define (compose-two f g) (lambda (x) (f (g x))))
(define compose (reduce-right compose-two id))
12 months ago
(define (combine combiner)
(lambda (a b) (if (null? a) b (if (null? b) a (combiner a b)))))
12 months ago
(define sum (reduce-right + 0))
12 months ago
(define (repeat value) (Y
(lambda (f) (lambda (n)
(if (= n 0) '() (cons value (f (- n 1))))))))
12 months ago
12 months ago
(define solve-line (compose (list
string->number
list->string
(reduce-right
(combine (lambda (left right) (cons (car left) (cdr right))))
'())
12 months ago
(map (lambda (char) (if (char-numeric? char) ((repeat char) 2) '())))
string->list)))
12 months ago
(define solve-all (compose (list
sum
(map solve-line))))
12 months ago
(display (solve-all (read-lines)))