Skip to content

Instantly share code, notes, and snippets.

@mnbi
Created September 18, 2010 13:16
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 mnbi/585659 to your computer and use it in GitHub Desktop.
Save mnbi/585659 to your computer and use it in GitHub Desktop.
#!/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