;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-beginner-abbr-reader.ss" "lang")((modname |12|) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ;; parse : (list-of symbol) -> (list-of (list-of symbol)) (define (first-line input) (cond [(empty? input) empty] [(cons? input) (if (equal? (first input) 'newline) empty (cons (first input) (first-line (rest input))))])) (check-expect (first-line empty) empty) (check-expect (first-line '(newline)) empty) (check-expect (first-line '(newline a)) empty) (check-expect (first-line '(a b c newline)) '(a b c)) (check-expect (first-line '(a b newline c d)) '(a b)) ; rest-input: (list-of symbol) -> (list-of symbol) (define (rest-input input) (cond [(empty? input) empty] [(cons? input) (if (equal? (first input) 'newline) (rest input) (rest-input (rest input)))])) (check-expect (rest-input empty) empty) (check-expect (rest-input '(newline)) empty) (check-expect (rest-input '(newline a)) '(a)) (check-expect (rest-input '(a b c newline)) empty) (check-expect (rest-input '(a b newline c d newline)) '(c d newline)) (define (parse input) (cond [(empty? input) empty] [(cons? input) (cons (first-line input) (parse (rest-input input)))])) (check-expect (parse empty) empty) (check-expect (parse '(a)) '((a))) (check-expect (parse '(a newline)) '((a))) (check-expect (parse '(a newline b newline)) '((a) (b))) (check-expect (parse '(a newline newline b)) '((a) () (b)))