Advertisement
PonaFly

summ(№2 19)

Apr 13th, 2016
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 1.11 KB | None | 0 0
  1. #lang scheme
  2. (define (sum )
  3.  (define in (open-input-file "input.txt"))
  4.  (define out (open-output-file "out.txt" #:exists 'replace))
  5.  (define (iter lst number n)
  6.   (define ch (read-char in))
  7.    (if (equal? ch eof) (begin (display (help (cons (+ (to-integer number 0 0) n)lst)) out) (close-output-port out))
  8.      (cond
  9.        ( (and (<= (char->integer ch) 57) (>= (char->integer ch) 48)) (iter lst (cons ch number) n))
  10.        ( (equal? ch #\+) (iter lst '() (to-integer number 0 0)))
  11.        ( (equal? ch #\newline) (iter lst '() 0))
  12.        (else (iter (cons (+ (to-integer number 0 0) n) lst) '() 0)))))
  13.   (iter '() '() 0))
  14.  
  15. (define (to-integer lst n degree)
  16.   (if (empty? lst) n
  17.     (to-integer (cdr lst) (+ n (* (expt 10 degree) (-(char->integer(car lst)) 48))) (+ 1 degree))))
  18.  
  19. (define (help lst)
  20.   (sort (myfilter lst '()) >))
  21.  
  22. (define (myfilter lst result)
  23.   (if (empty? lst) result
  24.     (if (pred (car lst) 0) (myfilter (cdr lst) (cons (car lst) result)) (myfilter (cdr lst) result))))
  25.  
  26. (define (pred n i)
  27.   (if (= n 0) true
  28.   (if (>=(remainder n 10) i) (pred (quotient n 10) (remainder n 10)) false
  29.       )))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement