r7rs-tests/util.scm

47 lines
1.3 KiB
Scheme
Raw Permalink Normal View History

2024-10-05 04:55:06 -04:00
(define slurp-loop
(lambda (line result)
(if (eof-object? line)
result
(slurp-loop (read-line) (string-append result line (string #\newline))))))
(define slurp
(lambda (path)
(with-input-from-file
path
(lambda ()
(slurp-loop (read-line) "")))))
(define string-starts-with?
(lambda (str prefix)
(and (>= (string-length str) (string-length prefix))
(string=? (string-copy str 0 (string-length prefix)) prefix))))
2024-10-05 07:17:45 -04:00
(define file-tail
(lambda (path linecount)
(with-input-from-file
path
(lambda ()
(letrec
((looper
(lambda (line count lines)
(if (eof-object? line)
2024-10-05 07:40:50 -04:00
(if (< (length lines) linecount)
(list)
(list-tail lines (- (length lines) linecount)))
2024-10-05 07:17:45 -04:00
(looper (read-line) (+ count 1) (append lines (list line)))))))
(looper (read-line) 0 (list)))))))
2024-10-05 04:55:06 -04:00
(define number-of-line->number
(lambda (str)
(letrec
((looper
(lambda (chars result)
(if (and (not (null? chars))
(char-whitespace? (car chars)))
(begin
(string->number result))
(looper (cdr chars) (string-append (string (car chars)) result ))))))
(looper (reverse (string->list str)) ""))))