[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

No Subject




Close enough: 

; String -> (listof String)
(define (string->words s)
  (map symbol->string
       (read
        (open-input-string
         (list->string
          ; turn s into a parenthesized expression: assume s doesn't contain parens
          (append '(#\() (string->list s) '(#\))))))))

; (listof X) (X -> Boolean) -> (listof (listof X))
(define (split-by sample pred?)
  (foldr (lambda (fst rst)
           (if (pred? fst)
               (cons '() rst)
               (cons (cons fst (first rst)) (rest rst))))
         '(())
         sample))
               
;; Tests: 
(define sample 
  '(#\o #\n #\e #\space #\l #\i #\n #\e #\newline #\space #\w #\i #\t #\h 
     #\space #\m #\a #\n #\y #\space #\w #\o #\r #\d #\newline #\newline #\space 
     #\a #\n #\d #\space #\w #\h #\i #\t #\e #\s #\p #\a #\c #\e #\s #\newline))

(define list-of-lines
  (map string->words
       (map list->string (split-by sample (lambda (ch) (char=? ch #\newline))))))
list-of-lines

(define list-of-words
  (apply append list-of-lines))
list-of-words

(define list-of-paras
  (split-by list-of-lines empty?))
list-of-paras

You can figure out the rest. Who assigned this homework? -- Matthias