COMP 210, Spring 2001
Homework 5 : Mutual Recursion
Due Oct.03 (wed), in class
All problems will require all (applicable) steps from the design recipe, including a template. Before you tackle the homework, remind yourself of our General Advice, Advice on Homeworks (in particular: staple and provide questions), and the Grading Guidelines.
Do the following:
For context, read over xml and xexpr background.
If you want, there is a provided teachpack (see below), which will let you read an xml file (giving you the corresponding xexpr), and similarly let you write an xexpr to a file (it will be written as an xml file, which may happen to be an html file (see wrap-into-html below)). However, this assignment is purely about manipulating and transforming xexprs; no knowledge of xml or html is needed, and any html files you create are entirely optional.
Often people comment out parts of their file. For instance, they might comment out one caption and use an alternate one. Or they might comment out an entire stretch of several pictures (which might themselves contain commented-out sections). Commenting out is easy, with xexprs: we simply declare anything occurring between <comment> and its matching </comment> as being commented out.
Make sure you have examples of xexprs containing comments, including some nested comments.
Of course, programs which interpret the data don't
want to even see anything which has been commented out.
Write strip-comments:
Given
an xlist,
return a corresponding xlist,
except w/ any 'comment sub-xexprs removed,
no matter how deeply nested.
(This will include a helper function for individual xexprs,
of course;
what if this helper is itself given a comment xexpr?
You can either mention in the contract/purpose that your function
only works on non-comment xexprs,
or you can return the empty string, "".)
Hint: compare with functions we've written before, which (say) remove each occurrence of 7 from a list of numbers. You might want to quickly write that function, you you can discuss the exact analogy here. What does the template suggest for the first and the rest of your xlist?
Write xexpr=?, which takes two xexpressions and determines whether they're equal. (This wil be helpful when doing your test cases.) Note: include a template for functions-processing-two-xexprs, Be sure your template explicitly lists all possible cases of the input. Note 2: You may want to defer this problem until you've done a couple of the following, since it's harder to process two xexprs simultaneously, than functions which just process one.
Do not use the built-in function equal? for this homework. Essentially, you are writing that function yourself. After this homework, you will be allowed to use equal?.
(list 'html (list 'head (list 'title "my html page")) (list 'body ...))where the ... should be replaced by the elements of Xs.
By the way, .html files happen to always consists of
exactly two elements:
a head (containing just the title, usually displayed in the title-bar of
a browser window),
and the body (which is the actual contents).
(Coincidence ?-)
Correction: In the original version,
I forgot that the title is itself an element of the head (according to html).
This version fixes that, but of course
we'll accept either the old or new for the
homework.
frame-picture: picture --> xexpr. A picture is a particular type of xexpr: it is of the form (list 'picture (list 'caption ..) (list 'filename ..)), except that the filename and caption can come in any order (but there is only one of each). Your function should take in such an xexpr, and output
(list 'li (list 'img (list 'attr 'src filename)) (list 'p (list 'attr 'align "center") ..caption))where filename is a string, and ..caption is the contents of an xlist.
Why this particular mumbo-jumbo? It's html for a (single) list-item, whose contents are an 'img, and a paragraph. These elements each contain some attributes: The image contains an attribute saying what the image's source-file is, and the paragraph contains an attribute suggesting that it's centered. The rest of the paragraph is of course the caption.
Notes: You may return the same info in different html, if you like; the details are up to you. Also, you do not need to check that each picture has exactly one caption and filename. (If you want to for fun, that's fine; check out the scheme's functions error and format functions. E.g., (error 'frame-picture (format "Only one caption should be in ~s." the-pict)))
(list ('p ..title) ('ol ..framed-pictures))That is, a paragraph whose contents are the gallery's title, and then an ordered-list, containing the results of frame-picture on each of the gallery's pictures.
;; file->xexpr: string --> xexpr ;; (file->xexpr filename) ;; Given a filename of a file in xml format, return the corresponding xml string. ;; ;; file->xexpr: string boolean --> xexpr ;; (file->xexpr filename attrs-as-tags?) ;; As before, but the if the boolean "attrs-as-tags" is false, ;; then the any tagged-xexpr has as its second item ;; an association list.For instance, on owlnet you can try (file->xexpr "/home/comp210/Homeworks/hw05-gallery.xml"), to read the example file (which may include some irrelevant white space).
;; xexpr->file: xexpr string boolean --> true ;; (xexpr->file xexpr filename overwrite?) ;; Write xexpr to the indicated filename, in xml format. ;; The boolean indicates whether or not to overwrite the file, ;; if it already exists. ;; Side-effect: A text message is displayed, indicating ;; the file has been written. ;; WARNING: if overwrite? is true, any existing copy ;; of the file will be overwritten! ;; ;; xexpr->file: xexpr string boolean boolean boolean --> true ;; (xexpr->file fname xexpr attrs-as-tags? overwrite? verbose?) ;; As before, but include flags for whether or not ;; to to interpret "attr" tags as attributes, and ;; whether or not to display the file's-been-written message. ;;If the expression you write happens to be an html expression, then you can view the file with your browser: file:///home/username/filepath. (Furthermore, if you put this file inside /home/username/public_html/, then it's on your public web page.)
;; current-date-string: boolean --> string ;; (current-date-string include-time?) ;; Return the string representation of the current date; ;; include the time if the argument is true. ;;This last one is handy, if you want your wrap-in-html to include a last-modified date at the bottom of the page. (This teachpack will be out once i figure out how to convert a regular file into a library (teachpack) file.