-1

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)
Óscar López
  • 232,561
  • 37
  • 312
  • 386
zero6142
  • 1
  • 2
  • 1
    Why on Earth are you using version 371? That's almost five years old! The latest version is 6.1.1, now called [Racket](http://racket-lang.org/). – Alexis King Apr 02 '15 at 20:19

1 Answers1

1

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.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • ah ok, i have a smilar issue here (define repl( lambda() (display "\nUofL>") (define inp (read-command)) (define lengtha (length com) – zero6142 Apr 02 '15 at 20:32
  • just did, could you help with the edited example aswell. – zero6142 Apr 02 '15 at 20:34
  • The solution is the same: stop using `define` inside a function and instead use `let*` at the beginning for declaring local variables. – Óscar López Apr 02 '15 at 20:35
  • it gives me error; let*: bad syntax (not a sequence of identifier--expression bindings) in: inp – zero6142 Apr 02 '15 at 20:38
  • 1
    @zero6142 ok, I edited my answer. But seriously, you _must_ read a good book or tutorial, this is basic Scheme. – Óscar López Apr 02 '15 at 20:40
  • thanks for the help, but it just give me a bad syntax error, the particular code, is part of a bit larger set for the definition repl let: bad syntax in: (let ((inp (read-command)) lengtha (length com))) – zero6142 Apr 02 '15 at 20:49
  • @zero6142 maybe you should post the whole code, but please do so in a new question. We can't help you if you don't provide all the information. – Óscar López Apr 02 '15 at 20:53
  • @zero6142 post a _new_ question, don't keep changing this question! and the problem with your code is that you should put all that big `cond` _inside_ the let, in the part that I marked as "return a value here" in my answer – Óscar López Apr 02 '15 at 21:04
  • sorry, still got the error let: bad syntax (not an identifier and expression for a binding) in: inp – zero6142 Apr 02 '15 at 21:08