3

I'm trying to make a simple bookmarking web-app in Racket.

It's meant to receive a url as a CGI argument, and right now, I'm just trying to confirm I received it by reflecting it back.

(define (start request)
  (response/xexpr
   (let* ([bindings (request-bindings request)]
          [url (if (exists-binding? 'url bindings)
                  (extract-binding/single 'url bindings)
                  "NO URL")])
     `(html
       (head (title "TITLE"))
       (body (h2 "TITLE")               
           (p "URL = " url))    
           ))))

However, instead of seeing what I expect to see .. which is a page that contains

URL = http://google.com

I'm seeing

URL = &url;

Which suggests that url is being quoted literally in the xexpr (treated as an entity), rather than being evaluated as a variable.

So what am I doing wrong? How do I get url evaluated?

interstar
  • 26,048
  • 36
  • 112
  • 180

2 Answers2

5

You need to use quasiquote and unquote to inject values into a quoted experession, more often seen as their reader abbreviation equivalents, ` and ,, respectively. When you use unquote/, inside of quasiquote/`, it will evaluate the expression and insert it into the surrounding quotation:

> (define url "http://google.com")
> `(p "URL = " ,url)
(p "URL = " "http://google.com")

You should put , in front of url in your template to unquote it.

For a more detailed explanation of quotation and quasiquotation, see Appendix A of this answer.

Community
  • 1
  • 1
Alexis King
  • 43,109
  • 15
  • 131
  • 205
  • OK I missed the , I thought the backtick at the beginning of the expression was sufficient to have variables evaluated as such. Thanks. – interstar Jul 28 '16 at 22:03
2

Use ,url :

 `(html
       (head (title "TITLE"))
       (body (h2 "TITLE")               
           (p "URL = " ,url))    
           ))))

Look for unquote in the documentation: http://docs.racket-lang.org/reference/quasiquote.html?q=unquote#%28form.%28%28quote.~23~25kernel%29._unquote%29%29

soegaard
  • 30,661
  • 4
  • 57
  • 106