Skip to content

Instantly share code, notes, and snippets.

@indraniel
Created May 12, 2018 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indraniel/d9d8f2a1d2b856b8a0887340f9f594ee to your computer and use it in GitHub Desktop.
Save indraniel/d9d8f2a1d2b856b8a0887340f9f594ee to your computer and use it in GitHub Desktop.
An example file reading script with chicken scheme
#!/usr/bin/env csi -ss
;; An example file reading script with chicken scheme
(use extras)
(define (read-it file)
(let ((fh (open-input-file file)))
(let loop((c (read-line fh)))
(if (eof-object? c)
(close-input-port fh)
(begin
(print c)
(loop (read-line fh)))))))
(define (main args)
(define file (if (null? args) '() (car args)))
(if (null? file)
(display "Please supply a file name to read!\n")
(read-it file)))
@indraniel
Copy link
Author

Another alternative can be:

(define path "/path/to/file")

(with-input-from-file path 
  (lambda () 
    (let loop ((line (read-line)) 
               (count 0)) 
      (if (eof-object? line) 
        count 
        (begin 
          (display line) 
          (newline) 
          (loop (read-line) (+ count 1)))))))

@indraniel
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment