[plt-scheme] Define in define
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:
http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-8.html#%25_sec_5.2.2
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)))))
results))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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".
http://download.plt-scheme.org/doc/352/html/mzlib/mzlib-Z-H-16.html#node_chap_16
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.