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.
 

61 lines
1.3 KiB

#!/usr/bin/guile -s
!#
(use-modules (ice-9 rdelim))
(define (read-lines)
(let loop ((line (read-line)))
(if
(eof-object? line)
'()
(cons line (loop (read-line))))))
(define (map list mapper)
(if
(null? list)
'()
(cons (mapper (car list)) (map (cdr list) mapper))))
(define (first-last-combine current rest)
(if
(null? current)
rest
(if
(null? rest)
current
(cons (car current) (cdr rest)))))
(define (first-last list predicate)
(if
(null? list)
'()
(first-last-combine
(if
(predicate (car list))
(cons (car list) (cons (car list) '()))
'())
(first-last (cdr list) predicate))))
(define (sum numbers)
(if
(null? numbers)
0
(+ (car numbers) (sum (cdr numbers)))
))
(define (solve-line line)
(
#!(lambda (first-last-result) (sum (map first-last-result (lambda (char) (string->number (string char))))))!#
(lambda (first-last-result) (string->number (list->string first-last-result)))
(first-last
(string->list line)
(lambda (char) (if (char-numeric? char) #t #f)))))
(define (solve-all lines)
(if
(null? lines)
0
(+ (solve-line (car lines)) (solve-all (cdr lines)))
))
(display (solve-all (read-lines)))