Sentence lab - CPSC PDF

Title Sentence lab - CPSC
Course Computation Programs And Programming
Institution The University of British Columbia
Pages 4
File Size 48 KB
File Type PDF
Total Downloads 94
Total Views 126

Summary

Completed lab work for Lab 5...


Description

;; DATA DEFINITIONS =============== ;; Data definition for sentence tree (@problem 1) (@htdd SentenceTree ListOfSentenceTree) (define-struct sents (name no-subs subs)) ;; Sentence is (make-sents String Integer ListOfSentenceTree) ;; interp. A sentence, and EITHER words or subs. ;; If no-subs is 0, then subs is considered to be list of sub sentences. ;; If no-subs is not 0, then subs is ignored. ;; ListOfSentenceTree is one of: ;; - empty ;; - (cons Sentence ListOfSentenceTree) ;; interp. A list of sentence trees (define W1 (make-sents "JOKING ABOUT JEALOUSY" 1 empty)) (define W2 (make-sents "YOU REALLY MEAN IT" 1 empty)) (define W3 (make-sents "IN A BACK TO SCHOOL SPECIAL ABOUT MONO" 1 empty)) (define W4 (make-sents "PERCHED ON THE TIP OF A SINKING SHIP" 1 empty)) (define W5 (make-sents "FREEZE TIME" 1 empty)) (define W6 (make-sents "MY FAVOURITE SONG ON REPEAT" 1 empty)) (define W7 (make-sents "WE ARE" 0 (list W3 W4))) (define W8 (make-sents "LIKE" 0 (list W2 W7))) (define W9 (make-sents "TO" 0 (list W5 W6))) (define ST1 (make-sents "KISS ME" 0 (list W1 W8 W9))) (define (fn-for-sentencetree st) (... (sents-name st) ;String (sents-no-subs st) ;Integer (fn-for-lost (sents-subs st)))) (define (fn-for-lost lost) (cond [(empty? lost) (...)] [else (... (fn-for-sentencetree (first lost)) (fn-for-lost (rest lost)))])) ;; CONSTANTS ====================== (define TEXT-SIZE 14) (define TEXT-COLOR "BLACK") (define MTTREE empty-image) (define VSPACE (rectangle 1 10 "solid" "white")) (define HSPACE (rectangle 10 1 "solid" "white"))

;; FUNCTIONS ====================== ;; count-sentences (@problem 2) (@htdf count-sents--st count-sents--lost) (@signature SentenceTree -> Natural) (@signature ListOfSentenceTree -> Natural) ;; takes SentenceTree / ListOfSentenceTree and produces number of sentences (check-expect (count-sents--lost empty) 0) (check-expect (count-sents--st W1) 1) (check-expect (count-sents--st W7) 2) (check-expect (count-sents--st ST1) 6) (check-expect (count-sents--lost (list W3 W4)) 2) (check-expect (count-sents--lost (list W2 W7)) 3) ;(define (count-sents--st st) 0) ;stubs ;(define (count-sents--lost lost) 0) (@template SentenceTree) (define (count-sents--st st) (if (zero? (sents-no-subs st)) (count-sents--lost (sents-subs st)) 1)) (@template ListOfSentenceTree) (define (count-sents--lost lost) (cond [(empty? lost) 0] [else (+ (count-sents--st (first lost)) (count-sents--lost (rest lost)))])) ;; render (@problem 3) (@htdf render--st render--lost) (@signature SentenceTree -> Image) (@signature ListOfSentenceTree -> Image) ;; takes SentenceTree / ListOfSentenceTree and renders it as a tree image (check-expect (render--lost empty) MTTREE) (check-expect (render--st W1) (beside (text (sents-name W1) TEXT-SIZE TEXT-COLOR) MTTREE)) (check-expect (render--st W2) (beside (text (sents-name W2)

TEXT-SIZE TEXT-COLOR) MTTREE)) (check-expect (render--st W3) (beside (text (sents-name W3) TEXT-SIZE TEXT-COLOR) MTTREE)) (check-expect (render--st W4) (beside (text (sents-name W4) TEXT-SIZE TEXT-COLOR) MTTREE)) (check-expect (render--st W5) (beside (text (sents-name W5) TEXT-SIZE TEXT-COLOR) MTTREE)) (check-expect (render--st W6) (beside (text (sents-name W6) TEXT-SIZE TEXT-COLOR) MTTREE)) (check-expect (render--st W7) (beside (text (sents-name W7) TEXT-SIZE TEXT-COLOR) (above/align "left" (render--st W3) (render--st W4)))) (check-expect (render--st W8) (beside (text (sents-name W8) TEXT-SIZE TEXT-COLOR) (above/align "left" (render--st W2) (render--st W7)))) (check-expect (render--st W9) (beside (text (sents-name W9) TEXT-SIZE TEXT-COLOR) (above/align "left" (render--st W5) (render--st W6)))) (check-expect (render--lost (list W9 W1)) (above/align "left" (beside (text (sents-name W9) TEXT-SIZE TEXT-COLOR)

(above/align "left" (render--st W5) (render--st W6))) (beside (text (sents-name W1) TEXT-SIZE TEXT-COLOR) MTTREE))) ;(define (render--st st) empty-image) ;stub ;(define (render--lost lost) empty-image) ;stub (@template SentenceTree) (define (render--st st) (beside (text (sents-name st) TEXT-SIZE TEXT-COLOR) (render--lost (sents-subs st)))) (@template ListOfSentenceTree) (define (render--lost lost) (cond [(empty? lost) MTTREE] [else (above/align "left" (render--st (first lost)) (render--lost (rest lost)))]))...


Similar Free PDFs