3
votes

I'm writing a program in which i have to reuse code from one of my professors. My program is written in Racket and the code i want to reuse is written in r6rs.

When I want to test my program it always fails. This is because I call a procedure with as argument a list (racket list), but that procedure is in the R6RS file. In the R6RS file there is (assert (list? argument)) , this is always false...

Here a simple example : Racket code :

#lang racket
(require "test2.ss")

(define a (list 1 2 3))
(b a)

R6RS code :

#!r6rs

(library 
 (test)
 (export b)
 (import (rnrs base (6))
         (rnrs control (6))
         (rnrs lists (6))
         (rnrs io simple (6)))

 (define (b a)
   (display "a is : ") (display a) (newline)
   (display "list? : ") (display (list? a)) (newline)))

The list? test in the R6RS file is always false... even if I pass as argument a newly created list like in the above example.

How can I do the same as in the example above, so that the list? tests results true.

Thanks for your help!

EDIT : I could not find a r6rs test that results in true on a immutable list, but I found another way to resolve my problem (by passing a mutable list to the procedure).

3
Racket pairs are immutable, Scheme pairs are not, hence the discrepancy.Alexis King
Indeed, does there exist another test in Scheme that will result true when tested on an immutable list? Because I can't find it..Kevin

3 Answers

3
votes

Racket pairs are different from Scheme pairs since Racket pairs are immutable while Scheme pairs are not.

As far as I know, there is no way to check for Racket's immutable lists in pure RnRS Scheme. However, it is possible to use Scheme's mutable lists in Racket (though of course that isn't really recommended).

#lang racket

(require compatibility/mlist
         "test2.ss")

(define a (mlist 1 2 3))
(b a)

Here's an excerpt from the documentation for compatibility/mlist:

This compatibility/mlist library provides support for mutable lists. Support is provided primarily to help porting Lisp/Scheme code to Racket.

Use of mutable lists for modern Racket code is strongly discouraged. Instead, consider using lists.

Still, if you need to interact with Scheme code, that's probably your only reasonable option.

2
votes

This is just an addendum to Alexis King's answer (code examples can't be in comments). Since the r6rs language (as implemented in Racket) uses mutable lists, and all racket libraries expect immutable lists, you can't reuse the r6rs code as-is. The fastest way to reuse the code is to port it to the #lang racket language.

Change the language, remove the import statement, and then fix each error one at a time.

#lang racket

 (define (b a)
   (display "a is : ") (display a) (newline)
   (display "list? : ") (display (list? a)) (newline)))
2
votes

When you say your code is written in Racket. Do you mean Racket, the software, or #!racket, one of the multiple compatible languages that Racket (the software) supports?

Since your library is written in #!r6rs, you either need to port it to a #!racket module or you main program can be written in #!r6rs and you can use the library as is. A third option is to make mutable lists to pass to the library function and convert back but or ban lists all togerther, but I find this option somewhat suboptimal.

To do full #!r6rs you need to install your library like this:

plt-r6rs --force --install ./test.sls

I assume test.sls is in the current directory. You'll get a confirmation. you need not restart DrRacket. (Force is not needed, but it will overwrite an earlier version.) Then you just change your code to be Scheme code:

#!r6rs
(import (rnrs)
        (test))

(define a (list 1 2 3))
(b a) ; #<void> (and prints stuff to stdout)

Hit [Run] in DrRacket and see the magic!