When you use the macro with 

   (prelude/connect-to www.google.com <http://www.google.com/> 80)

you are asking it to place the identifier www.google.com 
<http://www.google.com/> wherever HOST shows up in the right-hand-side of the 
macro definition. 

I prefer to teach syntax transformers using define-syntax and syntax-parse so 
that you can say 

 (define-syntax (prelude/connect-to stx)
    (syntax-parse stx 
      [(_ HOST:str PORT:nat) #’(  . . HOST . . PORT ..)])) 

That way misuses of a ‘macro’ are caught immediately and you get sensible error 
messages. 

[I just went thru this with freshmen in the spare weeks after Thanksgiving and 
it worked like a charm.
I don’t understand the appeal of define-macro.]





> On Dec 29, 2017, at 3:31 PM, Robert Girault <rfrancoisgira...@gmail.com> 
> wrote:
> 
> I thought I had understood mostly everything I learned in Beautiful Racket.  
> But I must be very confused as a whole.  I gave myself the task of 
> translating something like this 
> 
> #lang tcp
> connect www.google.com <http://www.google.com/> 80
> text GET / HTTP/1.1
> text Host: www.google.com <http://www.google.com/>
> text
> read-until-eof
> exit
> 
> into something like this
> 
> #lang racket/base
> (require racket/tcp)
> (require racket/port)
> 
> (define host "www.google.com <http://www.google.com/>")
> (define port 80)
> 
> (define-values (input-port output-port)
>   (tcp-connect host port))
> 
> (define (sendline ln)
>   (display ln (current-output-port))
>   (display ln output-port)
>   (flush-output output-port))
> 
> (displayln (format "Connected @ ~a on port ~a." host port))
> 
> (sendline "GET / HTTP/1.1\r\n")
> (sendline (format "Host: ~a\r\n\r\n" host))
> 
> (write (port->lines input-port))
> (exit)
> 
> I've achived a bit.  I translated that dsl-source into this.
> 
> '(prelude/connect-to www.google.com <http://www.google.com/> 80)
> '(send-line GET / HTTP/1.1)
> '(send-line Host: www.google.com <http://www.google.com/>)
> '(send-line)
> '(loop read until eof)
> '(exit)
> 
> Now I don't know how to proceed.  I think I must define a macro 
> prelude/connect-to.  For example
> 
>   (prelude/connect-to www.google.com <http://www.google.com/> 80)
> 
> should expand to 
> 
>   (begin
>       (require racket/tcp)
>       (require racket/port)
>       (define-values (input-port output-port)
>         (tcp-connect "www.google.com <http://www.google.com/>" 80))))
> 
> Here's what I tried.  (See my full code at http://pasterack.org/pastes/16943 
> <http://pasterack.org/pastes/16943>)
> 
> (define-macro (prelude/connect-to HOST PORT)
>   #'(begin
>       (require racket/tcp)
>       (require racket/port)
>       (define-values (input-port output-port)
>         (tcp-connect HOST PORT))))
> 
> This doesn't work because I get the error message
> 
>   www.google.com <http://www.google.com/>: unbound identifier in module in: 
> www.google.com <http://www.google.com/>
> 
> This must be because www.google.com <http://www.google.com/> is not a string. 
>  I don't know how to turn into a string and this must mean I'm very confused. 
>  What should my macro do so I'd get the desired expansion?
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com 
> <mailto:racket-users+unsubscr...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to