On this page:
Partner Switch
The Glory of the Sexpr
Sexprs in Practice, A First Glance
If you’re done early
Before you go...

Lab 7 S-expressions

home work!

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.

image

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.

To ensure that the partnerships are working out for the last five weeks, each of you must:
  • 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.

image

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:

> "hello!"

"hello!"

> '(1 2 3)

'(1 2 3)

> '(define (insult name)
     (string-append "You're dumb, " name "."))

'(define (insult name) (string-append "You're dumb, " name "."))

What you didn’t know is that many S-expressions are also good for coding web pages. The source code for a page is XHTML (a flavor of XML), and we represent such codes with special S-expressions. For example,
(define wp-page
  '(html
     (head
       (title "My first generated web page"))
     (body
       (p "This is my first generated web page. More to come"))))
is a simple web page that consists of a title and a body.

You can translate this XHTML representation into a string, write to a file, and show it in a browser. To do so,
  • open a lab specific teachpack in your browser

  • download the file to the folder for this lab;

  • create a lab-7 file and add
    (require "lab8-teachpack.rkt")
    (require 2htdp/batch-io)
    to 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.)

Once you have that, add these lines to your wp-page definition:
(write-file "1.html" (xexpr-as-string wp-page))
(show "1.html")
The last expression is the first command you see in this course; it opens a browser tab and points it to the file you just wrote.

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.

With quote alone, this won’t be easy and cons or list are to cumbersome. Yes, quasiquote and unquote are your friends:
; 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.

image

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!

Your professors have provided their favorite playlists for you to work with.
When you click on one of these links, you see files in a language that looks like XHTML but it is actually a form of XML, a generalization of the markup language. Again, it has a convenient representation as S-expressions:
; 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
Now download these files but save them as ".xml" files. That is, save each of these files in the same folder as your Lab 7 program file.

The teachpack that you installed comes with a function—read-xexprthat can read these files. When you have downloaded the files, make sure they are available:

> (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.

When you have a song list, you can make a web page per song. Here is a page for the first song on Prof. Ahmed’s play list:
(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.

image

If you’re done early

We again proceeded like mini-scientists. The "store" pages we created are absolutely minimal. Here are some ideas on how to refine the program:
  • 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.

If you know some XHTML and wish to interlink these pages, you can use

`(a ((href ,(file-name n))) ,(title-of-song n))

to create a link from one HTML file to another.

image

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.