Scheme Text Alignment Program帮助[只需要帮助理解如何编写函数]

时间:2013-11-27 22:44:44

标签: stream scheme racket

我正在开发一个方案中的文本对齐程序,该程序用于从文件中读取文本,将其转换为流,变换流以删除多余的空格和换行符,然后将文本对齐到右侧。这是我几乎完成的任务的一部分,所以我只需要在概念上理解如何去做。

我无法理解如何做的功能称为“右对齐”。 我在程序结束时调用了main函数,它使用了一个名为'hollow.txt'的文件,如果你们想要的话,我可以提供,但我想我只需要帮助就如何开始正确对齐。

非常感谢。

#lang racket

(require racket/stream)

(define file->stream 
  (lambda (filename)
    (let ((in-port (open-input-file filename)))
      (letrec
        ((build-input-stream
          (lambda ()
            (let ((ch (read-char in-port)))
              (if (eof-object? ch)
                  (begin
                    (close-input-port in-port)
                    (stream))
                  (stream-cons ch (build-input-stream)))))))
        (build-input-stream)))))

;main function

(define formatter ;from homework page
  (lambda (input-filename output-filename line-length)
    (stream->file output-filename
   ;   (right-justify 
        (insert-newlines line-length 
          (remove-extra-spaces ;str ;stream argument given.
            (remove-newlines ;str ;stream argument given.
              (file->stream input-filename)))))));)


  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MORPHING STREAMS.

(define right-justify ;not fully sure how to solve this one.
  (lambda (str)
    (cond
      [ (stream-empty?) str ]
      [ (char=? #\newline (stream-rest str))((stream-cons #\space (stream-rest str))) ] 
      [ 


(define remove-newlines 
  (lambda (str)
    (cond 
      [ (stream-empty? str)                      str                                                                  ]
      [ (char=? #\newline (stream-first str))    (stream-cons #\space (remove-newlines (stream-rest str)))            ]
      [ else                                     (stream-cons (stream-first str) (remove-newlines (stream-rest str))) ]
 )))


  (define remove-extra-spaces ;Remove all spaces in the stream.
    (lambda (str)
      (cond
        [ (stream-empty? str)  str ]
      [(char=? #\space (stream-first str))       (stream-cons #\space ( remove-extra-spaces (space-helper (stream-rest str))))]
      [ else                                     (stream-cons (stream-first str) (remove-extra-spaces (stream-rest str))) ]

      )))

  (define space-helper ;A helper for the remove-extra-spaces function.
    (lambda (str)
     (cond
      [ (stream-empty? str)                      str]
      [ (char=? #\space (stream-first str))     (space-helper(stream-rest str))]
      [ else                                     str  ]

  )))
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; THESE FUNCTIONS MAY NEED WORK.

(define stream->file
  (lambda (filename stream-in)
    (let ((out-port (open-output-file filename #:exists 'replace )))
      (letrec
        ((build-output-stream
          (lambda (str) ;stream

              (if (stream-empty? str)
                (close-output-port out-port)
                (begin
                    (write-char (stream-first str) out-port)
                 (build-output-stream (stream-rest str))) 
                 ))))
        (build-output-stream stream-in)))))







(define insert-newlines 
  (lambda (line-length str)
    (letrec
      ((insert 
        (lambda (str count)
      (if (stream-empty? str)
          str
          (let ((n (count-chars-to-next-space str)))
            (if (and (< count line-length) 
                 (<= (+ n count) line-length))
            (stream-cons
              (stream-first str)
              (insert (stream-rest str) (+ count 1)))
            (stream-cons
              #\newline
              (insert (trim-spaces str) 0))))))))
      (insert (trim-spaces str) 0))))




(define trim-spaces 
  (lambda (str)
    (cond ((stream-empty? str) (stream))
      ((char=? (stream-first str) #\space)
       (trim-spaces (stream-rest str)))
      (else str))))




(define count-chars-to-next-space 
  (lambda (str)
    (letrec
      ((count-ahead
        (lambda (str count)
      (cond ((stream-empty? str) count)
            ((char=? (stream-first str) #\space) count)
            (else (count-ahead (stream-rest str) (+ count 1)))))))
      (count-ahead str 0))))





(formatter "hollow.txt" "h.txt" 30) ;calling the function at the end so that it will run automatically.

0 个答案:

没有答案