solution-01.scm 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. (use-modules
  2. ;; for textual reading and writing procedures
  3. (ice-9 textual-ports)
  4. ;; not sure if needed
  5. (ice-9 binary-ports)
  6. ;; for `eof-object?`
  7. (ice-9 rdelim)
  8. ;; for keyword arguments
  9. (ice-9 optargs)
  10. ;; procedures for lists
  11. (srfi srfi-1))
  12. ;; =================
  13. ;; HELPER PROCEDURES
  14. ;; =================
  15. ;; FILE HANDLING
  16. (define* (get-lines-from-file file-path #:key (encoding "UTF-8"))
  17. ;; another common encoding is: "ISO-8859-1"
  18. ;; see http://www.iana.org/assignments/character-sets for more
  19. (define (get-lines-from-port port)
  20. (let ([line (get-line port)])
  21. (cond [(eof-object? line) '()]
  22. [else
  23. (cons line
  24. (get-lines-from-port port))])))
  25. (call-with-input-file file-path
  26. (lambda (port)
  27. (set-port-encoding! port encoding)
  28. (get-lines-from-port port))))
  29. (define (println output)
  30. (display (simple-format #f "~s\n" output)))
  31. ;; ============
  32. ;; ACTUAL LOGIC
  33. ;; ============
  34. (define (main args)
  35. (let* ([lines (get-lines-from-file (cadr args))]
  36. [frequency-changes (map string->number lines)])
  37. (println (apply + frequency-changes))))
  38. (main (program-arguments))