[racket] procedure to string
Thank you for your kind responses. Your solutions are good ones but
unfortunately don't fit my purpose. Imagine a mobile code scenario where
you have no control of the definition of a closure. What I want to do is to
be able to print the body of any incoming closure that arrives in a
message. So if server A sends server B a closure (lambda () (displayln
"hello world")), B could grab that incoming closure and capture it as the
string "(lambda () (displayln "hello world"))" so it can be inspected
before executing it. Does this make sense? How can I then construct a
function procedure->string that transforms at runtime (without using
macros) a closure's body to a string?
Thank you again and happy new year.
Alegria
On Tue, Dec 31, 2013 at 10:53 AM, Alexander D. Knauth
<alexander at knauth.org>wrote:
> Yes, you can do it with a struct with the property prop:procedure.
>
> #lang racket
>
> (require rackunit)
>
> (struct my-proc (proc str)
> #:property prop:procedure (struct-field-index proc))
>
> (define f
> (my-proc (lambda (x) (+ x 1))
> "(lambda (x) (+ x 1))"))
>
> (check-true (procedure? f))
> (check-equal? (f 1) 2)
> (check-equal? (my-proc-str f)
> "(lambda (x) (+ x 1))")
>
> (define-syntax-rule (my-lambda args body ...)
> (my-proc (lambda args body ...)
> (substring (~v '(lambda args body ...)) 1)))
>
> (define f2
> (my-lambda (x) (+ x 1)))
>
> (check-true (procedure? f2))
> (check-equal? (f2 1) 2)
> (check-equal? (my-proc-str f2)
> "(lambda (x) (+ x 1))")
>
>
> On Dec 29, 2013, at 10:45 PM, Alegria Baquero wrote:
>
> Hello,
>
> is there any way to transform a function's body to a string such as
> "(lambda(x)...)"?
>
> Thanks
>
> Alegria
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users
>
>
>
--
---------------------------------------------------------------------------------------
PhD candidate
Department of Informatics
Donald Bren School of Information and Computer Sciences
University of California, Irvine
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140102/4572a8d9/attachment-0001.html>