Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 1.35 KB | None | 0 0
  1. (define FILE "save_test3.txt")
  2. (define WORD "blabla")
  3.  
  4.  
  5. (define (save-data data path)
  6. (with-output-to-file path
  7. (lambda ()
  8. (write data))))
  9.  
  10. (save-data FILE WORD)
  11.  
  12. (define (load-data path)
  13. (with-input-from-file path
  14. (lambda () (read))))
  15.  
  16. (load-data FILE)
  17. (define (string-prefix? n m)
  18.   (define (helper xs ys)
  19.     (or (null? xs)
  20.         (and (equal? (car xs) (car ys))
  21.              (helper (cdr xs) (cdr ys)))))
  22.   (helper (string->list n) (string->list m)))
  23.  
  24. (define (string-split xs x)
  25.   (define (helper ys ms ns)
  26.     (if (null? ys)
  27.         '()
  28.         (if (string-prefix? (list->string ms) (list->string ys))
  29.             (cons ns (helper (list-tail ys (length ms)) ms ""))
  30.             (helper (cdr ys) ms (string-append ns (list->string (list (car ys))))))))
  31.   (helper (string->list (string-append xs x)) (string->list x) ""))
  32.  
  33.  
  34. (define (string-counter file)
  35.   (define file_port (open-input-file file))
  36.   (define (helper file)
  37.     (if (eof-object? (peek-char file))
  38.         '()
  39.         (cons (read-char file) (helper file))))
  40.   (define (iter lst)
  41.     (if (null? lst)
  42.         '()
  43.         (if (eq? (car lst) "")
  44.             (iter (cdr lst))
  45.             (cons (car lst) (iter (cdr lst))))))
  46.   (display (length (iter (string-split (list->string (helper file_port)) "\n"))))
  47.   (close-input-port file_port))
  48.  
  49. (string-counter "test.txt")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement