LINUX.ORG.RU

История изменений

Исправление qaqa, (текущая версия) :

Куда ж рэкетирам без контрактов c тестами :)

#lang at-exp racket

(define/contract ((find-num n) in-prt)
  (exact-positive-integer? . -> . (input-port? . -> . number?))
  (let loop ([i (sub1 n)])
    (define p (regexp-match #px"\\S+" in-prt))
    (unless p
      (error 'find-num "eof!"))
    (if (zero? i)
        (string->number (bytes->string/utf-8 (car p)))
        (loop (sub1 i)))))

(module+ test
  (require rackunit)
  (define txt @~a{1.45E+4 5E+6 7E+8
                  2.42E+1 7.7E+2 1E-1 5E-6 4E-6
                  1E-6 1E-8})
  (define (test-helper n)
    (call-with-input-string txt (find-num n)))
  (check-equal? (test-helper 9) 1e-6)
  (check-exn exn:fail? (λ () (test-helper 100) 1e-6)))

(call-with-input-file "numbers.txt" (find-num N))

Исходная версия qaqa, :

Куда ж рэкетирам без контрактов c тестами :)

#lang at-exp racket

(define/contract ((find-num n) in-prt)
  (exact-positive-integer? . -> . (input-port? . -> . number?))
  (let loop ([i (sub1 n)])
    (define p (regexp-match #px"\\S+" in-prt))
    (unless p
      (error 'find-num "eof!"))
    (if (zero? i)
        (string->number (bytes->string/utf-8 (car p)))
        (loop (sub1 i)))))

(module+ test
  (require rackunit)
  (define txt @~a{1.45E+4 5E+6 7E+8
                  2.42E+1 7.7E+2 1E-1 5E-6 4E-6
                  1E-6 1E-8})
  (define (test-helper n)
    (call-with-input-string txt (find-num n)))
  (check-equal? (test-helper 9) 1e-6)
  (check-equal? (test-helper 0) 1e-6)
  (check-exn exn:fail? (λ () (test-helper 100) 1e-6)))

(call-with-input-file "numbers.txt" (find-num N))