-1
(define while
      (lambda (rounds)
        (if (> rounds 0) 
            ((define compMove (random 3)) (display compMove))
             (while (- rounds 1))
        )
      )
  )

Above is program written in scheme, however I get an error saying that invalid context for definition. This is around (define compMove (random 3)).

1 Answers1

0

Your code is not Scheme.

According to the report you are only allowed to have define top level and The first expression of the body of a lambda, let, let*, let-values, let*-values, letrec, or letrec* expression.

In addition you have wrapped two expressions in parentheses as if that would make a block. It doesn't. What it will try to do is to evaluate the operator and then call the result as a procedure. Since define is special it fails. With any other form you would get Application: not a procedure.

Sylwester
  • 47,942
  • 4
  • 47
  • 79
  • Ok that makes sense, but I did that so that the value of compMove changes. I thought that by doing a call function that would work. So how would I have it so each time compMove changes? – Joseph Menezes Oct 21 '20 at 00:25
  • @JosephMenezes It is a useless binding since you can just have the expression that creates the value as the argument to `display`: `(display (random 3))`. If you want a local binding you should be looking at `let` – Sylwester Oct 21 '20 at 00:31
  • I found that the issue was actually a bug in my IDE, but thank you for the suggestion, as it makes the way my program looks more readable. – Joseph Menezes Oct 26 '20 at 17:00