Search code examples
schemeracketr5rs

Scheme definition error(short)


This is part of an interpreter I am making. I keep getting this error:

define not allowed in an expression context in: (define ret1 (list->string wl))

I am using DrScheme version 371, language Standard (R5RS).

(define (read-command)
  (set! com '( '() ))
  (set! wl (read-as-list))
  (define ret1 (list->string wl))
  (commRead)
  ret1
 )

similar issue here:

    (define repl(
                 lambda()
                  (display "\nUofL>")
                  (define inp (read-command))
                  (define lengtha (length com)


Solution

  • In you interpreter, it seems that definitions can only appear at the beginning of the function. You should use a let* instead:

    (define (read-command)
      (let* ((com '('())) ; are you sure you didn't mean '(()) ?
             (wl (read-as-list))
             (ret1 (list->string wl)))
      (commRead ret1)))
    

    For the second problem, try this:

    (define repl
      (lambda ()
        (display "\nUofL>")
        (let ((inp (read-command))
              (lengtha (length com)))
          ; return a value here
          )))
    

    As a side note, your code appears to be written in a procedural style - with all those set! and function calls executed for the effect. How on earth is ret1 going to be modified if you don't pass it as a parameter to commRead? I'd suggest you read a good book on Scheme programming and start writing code on a more functional style, currently your code isn't idiomatic and you'll get into trouble sooner or later.