Which identifier am I supposed to use to close my library in R7RS/Scheme?

125 Views Asked by At

I'm trying to write a R7RS library which will reverse a list in a destructive manner,

I currently have written this code;

#lang r7rs


(define-library (in-place-reverse!)
  (export reverse!)
  (import (scheme base))

(ignore the code below)
;    (define (reverse! list)
;      (if (null? list)
;          '()
;          (append (reverse (cdr list))
;                  (list (car list)))))))
(begin
  
    (define (reverse! lst)
      (define (reverse-hulp! prev cur)
        (cond
          ((null? cur) prev)
          ((next) cdr cur)
          ((set-cdr! cur prev))
          (else
            (reverse-hulp! cur next))))))
              
      (reverse! '() lst))

The only thing I'm worried about is the error i'm receiving.

 define-library: expected one of these identifiers: `import', `export', `begin', `cond-expand', or `include'
  parsing context: 
   while parsing library clause in: reverse!

Is my code even working as i'm asking it to? any kind of advice is appreciated!

Tried to implement a reverse procedure as I would in R5RS only this time i'm using destructive operators.

1

There are 1 best solutions below

0
On

Your error message doesn't match your code. When I run your example I get the error:

define-library: expected one of these identifiers: `import', `export', `begin', `cond-expand', or `include'
  parsing context: 
   while parsing library clause in: ignore

If I comment out the ignore form, I get your error and the expression (reverse! '() lst) is highlighted.

The problem is that the expression is outside the begin. Also, the intention was probably to make start the process in reverse!.

#lang r7rs


(define-library (in-place-reverse!)
  (export reverse!)
  (import (scheme base))

  (begin
  
    (define (reverse! lst)
      (define (reverse-hulp! prev cur)
        (cond
          ((null? cur) prev)
          ((next) cdr cur)
          ((set-cdr! cur prev))
          (else
           (reverse-hulp! cur next))))
      (reverse! '() lst))))

Then you need to figure out what to do about next.