[racket] submodule paths and DrRacket

20 views
Skip to first unread message

Harry Spier

unread,
Nov 18, 2012, 9:10:25 PM11/18/12
to users
Dear list members,

Can someone explain the following behavior

Case 1 as follows works in the DrRacket Definitions window but doesn't work in the Interactions window
#lang racket
(module m racket
(provide color)
(define color "blue"))

(module n racket
(require (submod ".." m))
(printf "my favorite color is ~a\n" color))

Case 2 as follows works in the DrRacket Interactions window but doesn't work in the Definitions window
> (module m racket
(provide color)
(define color "blue"))

(module n racket
(require 'm)
(printf "my favorite color is ~a\n" color))
(require 'n)

I'm clear why in case 1    (submod ".." m) resolves to submodule m when the code is in the definitions window, but I'm not clear why it doesn't do so  when the code is in the Interactions window. 

I'm also not clear why case 2 works in the Interactions window.  I.e.  I'm not clear why in case 2 in the interactions window (require 'm), which  according to the Racket Guide expands to (require (submod "." m)  works and (require (submod ".." m) doesn't work for this case.

Also is there a way to have 
    #lang racket
     (module m racket
     (provide color)
     (define color "blue"))

in the definitions window but the following in the interactions window

     >(module n racket
     (require SOME SUBMODULE PATH TO m)
      (printf "my favorite color is ~a\n" color))
      (require 'n) 

and have it work.

Thanks,
Harry Spier

Asumu Takikawa

unread,
Nov 18, 2012, 9:31:49 PM11/18/12
to Harry Spier, users
On 2012-11-18 21:10:25 -0500, Harry Spier wrote:
> Case 1 as follows works in the DrRacket Definitions window but doesn't
> work in the Interactions window
>
> #lang racket
>
> (module m racket
> (provide color)
> (define color "blue"))
> (module n racket
> (require (submod ".." m))
> (printf "my favorite color is ~a\n" color))

In the definitions window, the use of #lang implicitly wraps the entire
contents of the window in a module. This allows the use of relative
submodule references to module `m`.

In the interactions window, you are defining top-level modules instead.
You can get the same behavior if you wrap both `m` and `n` in another
module in the interactions window:

(module around racket
;; put m and n here
...)

> Case 2 as follows works in the DrRacket Interactions window but doesn't
> work in the Definitions window
>
> > (module m racket
> (provide color)
> (define color "blue"))
> (module n racket
> (require 'm)
> (printf "my favorite color is ~a\n" color))
> (require 'n)

This time, it works in the interactions window because they are both
top-level modules. Within a module, you can't refer to the parent's
submodules directly with a quoted name.

> Also is there a way to have
>
> #lang racket
>
> (module m racket
> (provide color)
> (define color "blue"))
>
> in the definitions window but the following in the interactions window
>
> >(module n racket
> (require SOME SUBMODULE PATH TO m)
> (printf "my favorite color is ~a\n" color))
> (require 'n)

Yes. Suppose the first snippet is defined in "foo.rkt". Then you can
just have (require (submod "foo.rkt" m)) if you're in the right
directory.

Cheers,
Asumu
____________________
Racket Users list:
http://lists.racket-lang.org/users
Reply all
Reply to author
Forward
0 new messages