fork download
  1. ; baseball
  2.  
  3. (define (tempname)
  4. (let loop ((i 0))
  5. (let ((f (string-append "temp" (number->string i))))
  6. (if (file-exists? f) (loop (+ i 1)) f))))
  7.  
  8. (define (with-input-from-url url thunk)
  9. (let ((f (tempname)))
  10. (if (zero? (system (string-append wget " " f " " url)))
  11. (begin (with-input-from-file f thunk) (delete-file f #t))
  12. (error 'with-input-from-url "system error in wget"))))
  13.  
  14. ; these use the letter oh, not the digit zero
  15. (define wget "c:\\cygwin\\bin\\wget -qO") ; windows/cygwin
  16. (define wget "/usr/local/bin/wget -qO") ; unix/hp
  17. (define wget "/usr/bin/wget -qO") ; linux/ubuntu
  18.  
  19. (define (string-split sep str)
  20. (define (f cs xs) (cons (list->string (reverse cs)) xs))
  21. (let loop ((ss (string->list str)) (cs '()) (xs '()))
  22. (cond ((null? ss) (reverse (if (null? cs) xs (f cs xs))))
  23. ((char=? (car ss) sep) (loop (cdr ss) '() (f cs xs)))
  24. (else (loop (cdr ss) (cons (car ss) cs) xs)))))
  25.  
  26. (define (string-find pat str . s)
  27. (let* ((plen (string-length pat))
  28. (slen (string-length str))
  29. (skip (make-vector plen 0)))
  30. (let loop ((i 1) (j 0))
  31. (cond ((= i plen))
  32. ((char=? (string-ref pat i) (string-ref pat j))
  33. (vector-set! skip i (+ j 1))
  34. (loop (+ i 1) (+ j 1)))
  35. ((< 0 j) (loop i (vector-ref skip (- j 1))))
  36. (else (vector-set! skip i 0)
  37. (loop (+ i 1) j))))
  38. (let loop ((p 0) (s (if (null? s) 0 (car s))))
  39. (cond ((= s slen) #f)
  40. ((char=? (string-ref pat p) (string-ref str s))
  41. (if (= p (- plen 1))
  42. (- s plen -1)
  43. (loop (+ p 1) (+ s 1))))
  44. ((< 0 p) (loop (vector-ref skip (- p 1)) s))
  45. (else (loop p (+ s 1)))))))
  46.  
  47. ; replace all occurrences of pat in str with rep
  48. (define (string-replace str pat rep)
  49. (let ((x (string-find pat str)))
  50. (if (not x) str
  51. (string-append (substring str 0 x) rep
  52. (string-replace (substring str (+ x (string-length pat))
  53. (string-length str)) pat rep)))))
  54.  
  55. (define (parse-mlb-lines)
  56. (let ((lines #f))
  57. (with-input-from-url
  58. "http://s...content-available-to-author-only...o.com/mlb/bottomline/scores"
  59. (lambda ()
  60. (let loop ((xs (list)))
  61. (if (not (eof-object? (peek-char)))
  62. (loop (cons (read-char) xs))
  63. (set! lines (list->string (reverse xs)))))))
  64. (map (lambda (xs) (list (car xs) (string-replace (cadr xs) "%20" " ")))
  65. (map (lambda (s) (string-split #\= s))
  66. (map (lambda (s) (substring s 6 (string-length s)))
  67. (cdr (string-split #\& lines)))))))
  68.  
  69. (define (cardinals)
  70. (let loop ((lines (parse-mlb-lines)))
  71. (when (pair? lines)
  72. (if (not (string-find "St. Louis" (cadar lines)))
  73. (loop (cdr lines))
  74. (let ((num (substring (caar lines) 4
  75. (string-length (caar lines)))))
  76. (let loop ((lines lines))
  77. (when (not (string=? (caar lines)
  78. (string-append "right" num "_count")))
  79. (display (cadar lines)) (newline)
  80. (loop (cdr lines)))))))))
  81.  
  82. (cardinals)
Runtime error #stdin #stdout #stderr 0.01s 7268KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
sh: 1: /usr/bin/wget: not found

Error: (with-input-from-url) system error in wget

	Call history:

	<syntax>	  [cardinals] (##core#undefined)
	<syntax>	  [cardinals] (parse-mlb-lines)
	<syntax>	  (cardinals)
	<eval>	  (cardinals)
	<eval>	  [cardinals] ((##core#letrec* ((loop (##core#loop-lambda (lines) (when (pair? lines) (if (not (string-find "St. L...
	<eval>	  [cardinals] (parse-mlb-lines)
	<eval>	  [parse-mlb-lines] (with-input-from-url "http://s...content-available-to-author-only...o.com/mlb/bottomline/scores" (lambda () (let loop ((xs (li......
	<eval>	  [with-input-from-url] (tempname)
	<eval>	  [tempname] ((##core#letrec* ((loop (##core#loop-lambda (i) (let ((f (string-append "temp" (number->string i))))...
	<eval>	  [tempname] (string-append "temp" (number->string i))
	<eval>	  [tempname] (number->string i)
	<eval>	  [tempname] (file-exists? f)
	<eval>	  [with-input-from-url] (zero? (system (string-append wget " " f " " url)))
	<eval>	  [with-input-from-url] (system (string-append wget " " f " " url))
	<eval>	  [with-input-from-url] (string-append wget " " f " " url)
	<eval>	  [with-input-from-url] (error (quote with-input-from-url) "system error in wget")	<--