Efraim Flashner <efr...@flashner.co.il> writes: > +(define wc-command-implementation > + (lambda files > + (let ((files (filter (lambda (file) > + (catch 'system-error > + (lambda () > + (stat file)) > + (lambda args > + (let ((errno (system-error-errno args))) > + (format (current-error-port) "~a: ~a~%" > + file (strerror errno)) > + #f)))) > + files))) > + (for-each > + (lambda (file) > + (let-values (((lines chars) > + (call-with-input-file file lines+chars))) > + (format #t "~a ~a ~a~%" lines chars file))) > + files)))) > + > +(define wc-l-command-implementation > + (lambda files > + (let ((files (filter (lambda (file) > + (catch 'system-error > + (lambda () > + (stat file)) > + (lambda args > + (let ((errno (system-error-errno args))) > + (format (current-error-port) "~a: ~a~%" > + file (strerror errno)) > + #f)))) > + files))) > + (for-each > + (lambda (file) > + (let-values (((lines chars) > + (call-with-input-file file lines+chars))) > + (format #t "~a ~a~%" lines file))) > + files)))) > + > +(define wc-c-command-implementation > + (lambda files > + (let ((files (filter (lambda (file) > + (catch 'system-error > + (lambda () > + (stat file)) > + (lambda args > + (let ((errno (system-error-errno args))) > + (format (current-error-port) "~a: ~a~%" > + file (strerror errno)) > + #f)))) > + files))) > + (for-each > + (lambda (file) > + (let-values (((lines chars) > + (call-with-input-file file lines+chars))) > + (format #t "~a ~a~%" chars file))) > + files))))
It looks to me that the filter function is the same in all of these procedures. Even the actual implementation, i.e. the for-each over the resulting files is almost exactly the same. This could be simplified. If only the format expression differs then you could abstract this difference away. You could still have three different procedures, but they can be the result of evaluating a higher-order function. It also seems to me that you could use syntactic sugar to simplify “(define something (lambda ...))” to “(define (something ...))”. ~~ Ricardo