Lab 7 S-expressions
Purpose In this lab we’re going to work with S-expressions, a type of data that you’ve already used quite a bit informally. We’ll slice up lists with unquote-splicing and reach outside our walled garden to touch the file system for the first time.
Partner Switch
TAs: prepare partnering
It is time for another partner. Unlike in a company, where you may switch partners twice a day, we just switch three times over the course of the semester. This time the head TAs are picking partners for you in the hope of finding good matches.
keep a diary of partner interactions; record when you meet, what you work on, what your next meeting date is;
report problems to the head TA of your lab; and
meet with your instructor concerning repeat problems.
The Glory of the Sexpr
You’ve written quite a bit of code in BSL and ISL. Take any (syntactically valid) expression you’ve written to date, toss a ’ in front, and what do you have? It’s an S-Expression:
; An S-Expression (or Sexpr) is one of: ; - Symbol ; - String ; - Number ; - [Listof Sexpr]
Examples: | ||||||||
|
(define wp-page '(html (head (title "My first generated web page")) (body (p "This is my first generated web page. More to come"))))
open a lab specific teachpack in your browser
download the file to the folder for this lab;
- create a lab-7 file and addto the definitions area and save it in a file. (The 8 is not a typo unless you rename it to "lab7-teachpack.rkt" when you download.)
(write-file "1.html" (xexpr-as-string wp-page)) (show "1.html")
Let’s try to generate the second, third, ..., 200th web page. You see, you don’t want to generate these by hand. You want to abstract over the above web page. Doing so calls for a function and some good way of writing down schemas for web pages.
; Nat -> Sexpr ; generate the nth web page (define (my-nth-page n) (local ((define n-as-string (number->string n))) `(html (head (title ,(string-append "My " n-as-string " generated web page."))) (body (p ,(string-append "This is my " n-as-string " generated web page. More to come."))))))
Time to program
Exercise 1 Design a function that consumes a natural number and generates a file name with a ".html" extension.
Exercise 2 Design a function that consumes a natural number n, generates your nth web page, and writes it to a an ".html" file.
The tests for this function are not sufficient to check the entire result because it writes to a file as a side-effect. Use show from the teachpack to view the page(s) you generate with the tests.
Exercise 3 Design a function that consumes a natural number n and creates that many web pages in your directory, named "1.html", "2.html", and so on. The function returns the list of file names, assuming the files have been created successfully.
Once the function successfully passes its test, use show to look at one of the generated web pages.
Exercise 4 Design the function replace-symbol, that given a Symbol and two Sexprs, replaces any instance of the Symbol in the second Sexpr with the first Sexpr.
Partner switch
Sexprs in Practice, A First Glance
Your professors have been quite busy this semester. Not only do they teach and slave away on research, but they’ve been working on digital music software: picture iTunes with more parentheses. Their start-up, λTunes, is about to be acquired by Google for just under a billion dollars, but the deal is stalled because Professor Van Horn forgot to implement an HTML generator for playlists . Let’s help him out!
; All PlayXexprs are of the form: ; (cons 'playlist (cons [Listof Attribute] [Listof SongXexpr])) ; where the only Attribute is associated with 'name. ; Interpretation: a PlayXexpr is a data representation for playlists ; ; All SongXexprs are of the form: ; (cons 'song (list [Listof Attribute])) ; where the valid Attributes are: 'album, 'artist, 'title, and 'year ; Interpretation: a SongXexpr is a data representation for songs in a playlist ; ; An Attribute is a: ; (list Symbol String) ; Interpretation: '(a "b") represents the attribute ; a = "b" in a piece of XML data
The teachpack that you installed comes with a
function—
> (read-plain-xexpr "lab7-ahmed.xml")
'(playlist
((name "Groovy Tunes, Dude"))
(song
((album "Wish You Were Here")
(artist "Pink Floyd")
(title "Shine on You Crazy Diamond, Pts. 1-5")
(year "1975")))
(song
((album "Wish You Were Here")
(artist "Pink Floyd")
(title "Shine on You Crazy Diamond, Pts. 6-9")
(year "1975")))
(song
((album "London 1966-1967")
(artist "Pink Floyd")
(title "Nick's Boogie")
(year "2005")))
(song
((album "Obscured By Clouds")
(artist "Pink Floyd")
(title "Childhood's End")
(year "1972")))
(song ((album "Animals") (artist "Pink Floyd") (title "Dogs") (year "1977")))
(song
((album "The Wall")
(artist "Pink Floyd")
(title "Goodbye Blue Sky")
(year "1979")))
(song
((album "Meddle") (artist "Pink Floyd") (title "Echoes") (year "1971"))))
> (ev '(current-directory "../")) ev: undefined;
cannot reference undefined identifier
Some Xexprs contain a list of Attributes. A list of Attributes is a list of two element lists. Each of the internal lists has a Symbol as the first element and a String as the second element. This is a fairly common pattern in Sexpr-based languages called an association list.
Exercise 5 Design the function retrieve, which given a list of Attributes and a Symbol returns the String content associated with that Symbol. If the Symbol is not a valid attribute, the function may raise an error and/or return false.
Exercise 6 Using retrieve and map, design the function all-songs, which consumes a PlayXexpr and produces a list of all song titles (Strings).
Hints How can you extract the list of SongXexprs from a PlayXexpr? If SongXexpr were a structure, you would have the function playlist-songs available.
Where is the Attribute list in a SongXexpr? If SongXexpr were a structure, you would have the function song-attributes available.
(define song-page '(html (head (title "Shine on You Crazy Diamond, Pts. 1-5")) (body (p "This is the web page for:") (p "Shine on You Crazy Diamond, Pts. 1-5"))))
Exercise 7 Define a function that creates a web page representation for a song title.
We use the word “define” here because you are generalizing from an S-expression, just like we did above. What would change in song-page if we wanted to use the second song on Prof. Ahmed’s list?
This exercise will require a hint from the TA. Please do experiment using the design recipe as rigorously as possible for the critical auxiliary function.
Exercise 8 Design a function that creates one web page representation per song on a playlist, writes it as a string to a numbered file (see file-name above), and returns the list o file names.
Use show to look at some of the pages you created.
Phew! If you’ve made it this far, give yourself a pat on the back. You have just seen the rudimentary idea of how to generate an on-line store.
If you’re done early
Consider adding information to each page using extra paragraphs (p).
Consider creating pages for entire albums. They should list the title of the album, the artist, and the songs.
Consider creating pages for artists. They should list the artists and the albums.
`(a ((href ,(file-name n))) ,(title-of-song n))
Before you go...
We’ve got one test behind us. If you bombed it, now is the time to sink or swim. Once the next test passes, those who are failing will have failed and it will be too late to help. If you had trouble finishing any of the exercises in this lab or your homework, or just feel like you’re struggling with any of the class material, come to office hours and talk to a TA or tutor for additional assistance.