Created
September 18, 2010 13:16
-
-
Save mnbi/585659 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/opt/local/bin/gosh | |
;; -*- coding: utf-8 -*- | |
;; wordcount.scm: a simple filter to count lines/words/characters. | |
(define (apply-filter proc port) | |
(let ((line (read-line port))) | |
(if (not (eof-object? line)) | |
[begin | |
(proc line) | |
(apply-filter proc port)]))) | |
;; BEGIN: initialize global variable(s) | |
(define lines 0) | |
(define words 0) | |
(define chars 0) | |
;; MAIN | |
(apply-filter | |
(lambda (line) | |
[begin | |
(set! lines (+ lines 1)) | |
(set! words | |
(+ words | |
(let ((lst (string-split line char-whitespace?))) | |
(if (= (string-length (car lst)) 0) ; empty line | |
0 | |
(length lst))))) | |
(set! chars | |
(+ chars (string-length line) 1)) | |
]) | |
(standard-input-port)) | |
;; END: output results in global variable(s) | |
(print "\t" lines "\t" words "\t" chars) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment