[plt-scheme] Define in define

From: Danny Yoo (dyoo at hkn.eecs.berkeley.edu)
Date: Tue Sep 19 09:28:23 EDT 2006

On Tue, 19 Sep 2006, Ahmad Issa wrote:

> I receive the error "define: not allowed in an expression context in:
> (define (part cur max) (cond ((= cur 0) 1) ((not (= (vector-ref (vector-ref
> results cur) max) 0))...", why can't I put a define in a define after a let
> statement?

Hi Ahmad,

LET is an expression.  According to the standard:


the body of a function consists of a bunch of definitions, followed by a 
bunch of expressions.  So it's just a matter of not fitting the grammar.

You could push up the vector initialization up a bit earlier, like:

   (define results
     (let ([results (make-vector (+ n 1))])
       (let loop ((i 0))
         (cond ((> i n) 0)
               (else (vector-set! results i (make-vector (+ n 1) 0))
                     (loop (+ i 1)))))

If you're willing to use a PLT Scheme-specific library, you may want to 
look into using the BUILD-VECTOR function within "etc.ss".


since it couples vector creation with an initialization function for every 
element: using it can help you reduce the code above to a one-liner.

Posted on the users mailing list.