Messing with `current-namespace` seems like overkill, though maybe this 
approach is too naive for your needs, which just involves hiding the `defun` 
identifiers in the current namespace with a `defun:` prefix, then using `#%app` 
to pull them out again.


#lang racket
(require rackunit (for-syntax racket/syntax) (prefix-in rb: (only-in 
racket/base #%app)))

(define-syntax (defun stx)
  (syntax-case stx ()
    [(_ name (arg ...) body0 body ...)
     (with-syntax ([defun:name (format-id stx "defun:~a" #'name)])
       #'(define (defun:name arg ...)
           body0
           body ...))]))

(define-syntax-rule (defvar name value)
  (define name value))

(define-syntax (#%app stx)
  (syntax-case stx ()
    [(_ proc-name args ...)
     (with-syntax ([defun:proc-name (format-id stx "defun:~a" #'proc-name)])
       (if (identifier-binding #'defun:proc-name)
           #'(rb:#%app defun:proc-name args ...)
           #'(rb:#%app proc-name args ...)))]))


;; Let's see whether these work.
(defvar example 'an-arbitrary-symbol)
(defun example (sym) (symbol->string sym))
(check-equal? (example example) "an-arbitrary-symbol")
(check-equal? (symbol->string example) "an-arbitrary-symbol")





On Apr 11, 2016, at 4:51 PM, Josh Tilles <merelyapseudo...@gmail.com> wrote:

> Hello Racketeers,
> 
> I come seeking advice. I'm trying to implement a Lisp-2 in Racket, but I've 
> been unsuccessful in my initial attempts.
> 
> My goal is to be able to do something like the following:
> 
> ```
> (defun example (sym)
>  (symbol->string sym))
> 
> (defvar example 'an-arbitrary-symbol)
> 
> (example example)
> ;=> "an-arbitrary-symbol"
> ```
> 
> And my here was my first stab at a solution:
> 
> ```
> #lang racket
> 
> (define-for-syntax functions-ns (make-empty-namespace))
> (define-for-syntax values-ns (make-empty-namespace))
> 
> (define-syntax (defun stx)
>  (syntax-case stx ()
>    [(_ name (arg ...) body0 body ...)
>     (parameterize ([current-namespace functions-ns])
>       #'(define (name arg ...)
>           body0
>           body ...))]))
> 
> (define-syntax (defvar stx)
>  (syntax-case stx ()
>    [(_ name value)
>     (parameterize ([current-namespace values-ns])
>       #'(define name value))]))
> 
> ;; Let's see whether these work.
> (defvar example 'an-arbitrary-symbol)
> (defun example (sym) (symbol->string sym))
> (example example)
> ;;=> symbol->string: contract violation
> ;;=>   expected: symbol?
> ;;=>   given: #<procedure:example>
> ;;=>   context...:
> ;;=>    /opt/homebrew-cask/Caskroom/racket/6.4/Racket 
> v6.4/collects/racket/private/misc.rkt:87:7
> ```
> 
> I'll be deeply grateful for help you can provide.
> 
> Cheers,
> Josh
> 
> -- 
> 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.

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