[racket users] module question

44 views
Skip to first unread message

Kevin Forchione

unread,
Aug 6, 2019, 6:58:22 PM8/6/19
to Racket-Users List
Is there a way to define 2 modules within a file and reference them or is a file limited to 1 module only (but may have multiple submodules)?

Here’s a small sample:

#lang racket

(module A racket
˘ (provide X)
(define X 'foo))

(module B racket
(require 'A)
(printf "X=~a~%" X))

Apparently, as I understand it, I’m already nesting modules within the #lang racket. But I’m not sure how to require A from within B in this situation.

Kevin

Sorawee Porncharoenwase

unread,
Aug 6, 2019, 7:07:04 PM8/6/19
to Kevin Forchione, Racket-Users List

You need:

(module B racket
  (require (submod ".." A))
  (printf "X=~a~%" X))

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/6326DC5D-44D5-4CE9-B2B8-E781A6D51561%40gmail.com.

Luis Sanjuán

unread,
Aug 7, 2019, 3:30:59 AM8/7/19
to Racket Users
As Sorawee has said. To expand a bit.

First,

#lang modname

is a shorthand for:

(module a-name modname)

where a-name derives from the filename.

So, modules within are actually submodules.

On the other hand, the .. in the submod is a relative module path. The symbol is borrowed, I believe, from Unix.
where '.' means the current path, and '..' means the parent of the current path. Here,
'..' points to the enclosing module.

Kevin Forchione

unread,
Aug 7, 2019, 11:55:44 AM8/7/19
to Sorawee Porncharoenwase, Racket-Users List


On Aug 6, 2019, at 4:06 PM, Sorawee Porncharoenwase <sorawe...@gmail.com> wrote:

(module B racket
  (require (submod ".." A))
  (printf "X=~a~%" X))

Why doesn’t printf display anything ? 

Kevin 

Kevin Forchione

unread,
Aug 7, 2019, 12:41:45 PM8/7/19
to Sorawee Porncharoenwase, Racket-Users List
Oh, wait. modules are “lazy” until they’re required. Something like that. I think I’m beginning to understand. :) 

#lang racket


(module A racket
  (provide X)
  (define X 5)
  (printf "A.X=~a~%" X))

(module B racket
  (require (submod ".." A))
  (printf "B.X=~a~%" X))


(require ‘B)

Kevin

Reply all
Reply to author
Forward
0 new messages