#!/usr/bin/guile \ -e main -s !# ;;; Copyright 2005 Daniel Cer (daniel.cer@cs.colorado.edu) ;;; ;;; This work is licensed under the Creative Commons Attribution-NonCommercial- ;;; ShareAlike License. To view a copy of this license, visit ;;; http://creativecommons.org/licenses/by-nc-sa/2.5/ or send a letter to ;;; Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, ;;; 94105, USA. ; read-entries - read in the list of new-line seperated numbers from an ; input-port. (define (read-entries port) (let ((in-line (read-line port))) (if (eof-object? in-line) '() (append (list (string->number in-line)) (read-entries port))))) ; bubble-sort - returns a list with all of the items in the list 'entries' ; but with the items in ascending order. (define (bubble-sort entries) (do ((entries-vec (list->vector entries)) (i (- (length entries) 1) (if (equal? (+ j 1) i) (- i 1) i)) (j 0 (if (equal? (+ j 1) i) 0 (+ j 1)))) ((equal? i 0) (vector->list entries-vec)) (if (< (vector-ref entries-vec (+ j 1)) (vector-ref entries-vec j)) (let ((swap-tmp (vector-ref entries-vec j))) (vector-set! entries-vec j (vector-ref entries-vec (+ j 1))) (vector-set! entries-vec (+ j 1) swap-tmp))))) (define (main args) (if (not (equal? (length args) 2)) (display (string-append "Usage:\n\t" (car args) " (file with numbers to sort)\n")) (map (lambda (entry) (display entry) (newline)) ; display the sorted list (bubble-sort ; sort everything (call-with-input-file (car (cdr args)) read-entries)) ; read in the items )))