fork download
  1. ; sum embedded numbers
  2.  
  3. (define (next-number xs)
  4. (let loop ((xs xs))
  5. (if (null? xs) (values #f #f)
  6. (if (not (char-numeric? (car xs)))
  7. (loop (cdr xs))
  8. (let loop ((xs xs) (n 0))
  9. (if (null? xs) (values n xs)
  10. (if (char-numeric? (car xs))
  11. (loop (cdr xs)
  12. (+ (* n 10)
  13. (- (char->integer (car xs))
  14. (char->integer #\0))))
  15. (values n xs))))))))
  16.  
  17. (define (sum-embedded-numbers str)
  18. (let loop ((xs (string->list str)) (sum 0))
  19. (call-with-values
  20. (lambda () (next-number xs))
  21. (lambda (n xs)
  22. (if n (loop xs (+ sum n)) sum)))))
  23.  
  24. (display (sum-embedded-numbers "11aa22bb33cc44")) (newline)
Success #stdin #stdout 0.03s 8020KB
stdin
Standard input is empty
stdout
110