fork(1) download
  1. ; file reversal
  2.  
  3. (define (read-line . port)
  4. (define (eat p c)
  5. (if (and (not (eof-object? (peek-char p)))
  6. (char=? (peek-char p) c))
  7. (read-char p)))
  8. (let ((p (if (null? port) (current-input-port) (car port))))
  9. (let loop ((c (read-char p)) (line '()))
  10. (cond ((eof-object? c) (if (null? line) c (list->string (reverse line))))
  11. ((char=? #\newline c) (eat p #\return) (list->string (reverse line)))
  12. ((char=? #\return c) (eat p #\newline) (list->string (reverse line)))
  13. (else (loop (read-char p) (cons c line)))))))
  14.  
  15. ; read up to max-lines from current input, return lines
  16. (define (read-lines max-lines)
  17. (let loop ((x (read-line)) (xs (list)) (k max-lines))
  18. (if (or (zero? k) (eof-object? x))
  19. (reverse xs)
  20. (loop (read-line) (cons x xs) (- k 1)))))
  21.  
  22. (define (temp num)
  23. (string-append
  24. "./file-reversal."
  25. (number->string (get-process-id))
  26. "."
  27. (number->string num)))
  28.  
  29. (define max-lines 10000)
  30.  
  31. (define (read-into-temps)
  32. (let loop ((file-num 1))
  33. (let ((lines (read-lines max-lines)))
  34. (if (null? lines) (- file-num 1)
  35. (let ((temp-file-name (temp file-num)))
  36. (with-output-to-file temp-file-name
  37. (lambda ()
  38. (do ((lines lines (cdr lines)))
  39. ((null? lines))
  40. (display (car lines)) (newline))))
  41. (loop (+ file-num 1)))))))
  42.  
  43. (define (write-output num-files)
  44. (let loop ((num num-files))
  45. (when (positive? num)
  46. (with-input-from-file (temp num)
  47. (lambda ()
  48. (do ((lines (reverse (read-lines -1)) (cdr lines)))
  49. ((null? lines))
  50. (display (car lines)) (newline))))
  51. (loop (- num 1)))))
  52.  
  53. (define (reverse-file in-file out-file)
  54. (with-input-from-file in-file (lambda ()
  55. (with-output-to-file out-file (lambda ()
  56. (write-output (read-into-temps)))))))
  57.  
  58. (reverse-file "bible.txt" "txt.elbib")
Runtime error #stdin #stdout #stderr 0s 7264KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: (open-input-file) cannot open file - No such file or directory: "bible.txt"

	Call history:

	<syntax>	  (##core#set! reverse-file (##core#lambda (in-file out-file) (with-input-from-file in-file (lambda ()...
	<syntax>	  (##core#lambda (in-file out-file) (with-input-from-file in-file (lambda () (with-output-to-file out-......
	<syntax>	  [reverse-file] (##core#begin (with-input-from-file in-file (lambda () (with-output-to-file out-file (lambda () (wri......
	<syntax>	  [reverse-file] (with-input-from-file in-file (lambda () (with-output-to-file out-file (lambda () (write-output (rea......
	<syntax>	  [reverse-file] (lambda () (with-output-to-file out-file (lambda () (write-output (read-into-temps)))))
	<syntax>	  [reverse-file] (##core#lambda () (with-output-to-file out-file (lambda () (write-output (read-into-temps)))))
	<syntax>	  [reverse-file] (##core#begin (with-output-to-file out-file (lambda () (write-output (read-into-temps)))))
	<syntax>	  [reverse-file] (with-output-to-file out-file (lambda () (write-output (read-into-temps))))
	<syntax>	  [reverse-file] (lambda () (write-output (read-into-temps)))
	<syntax>	  [reverse-file] (##core#lambda () (write-output (read-into-temps)))
	<syntax>	  [reverse-file] (##core#begin (write-output (read-into-temps)))
	<syntax>	  [reverse-file] (write-output (read-into-temps))
	<syntax>	  [reverse-file] (read-into-temps)
	<syntax>	  (reverse-file "bible.txt" "txt.elbib")
	<eval>	  (reverse-file "bible.txt" "txt.elbib")
	<eval>	  [reverse-file] (with-input-from-file in-file (lambda () (with-output-to-file out-file (lambda () (write-output (rea......	<--