# [plt-scheme] Define in define

 From: Jos Koot (jos.koot at telefonica.net) Date: Tue Sep 19 11:04:30 EDT 2006 Previous message: [plt-scheme] Define in define Next message: [plt-scheme] Scheme efficiency guidelines (or: the fastest way to calc a cartesian product) Messages sorted by: [date] [thread] [subject] [author]

```Hi
Are you sure you want to use vectors? If you represent a partition by a list and let the procedure make a list of partitions, it can be written in 151 characters, not counting lay-out characters but including necessary spaces. This is not easy though. Are you familiar with named let and the procedures map and append? You should master these things before doing this problem, I think.
Jos Koot

((((lambda(x)((((((x x)x)x)x)x)x))
(lambda(x)(lambda(y)(x(x y)))))
(lambda(x)(write x)x))
"greetings, Jos")
----- Original Message -----
To: Jos Koot
Sent: Tuesday, September 19, 2006 2:20 PM
Subject: Re: [plt-scheme] Define in define

Thanks for your reply. Can a body contain an internal definition, followed by an expression, then another internal definition, followed by an expression?

I see the code i posted above does not return something (since define isn't an expression), but adding an expression to the end still causes the same error. I suspect it's because theres a 'let' expression inbetween the internal defines.

Here's the modified version:

(define (partition n)
(define results (make-vector (+ n 1)))
(define (update cur max val)
(vector-set! (vector-ref results cur) max val)
val)

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

(define (part cur max)
(cond ((= cur 0) 1)
((not (= (vector-ref (vector-ref results cur) max) 0))
(vector-ref (vector-ref results cur) max))
(else (update cur max (let loop ((i 1))
(cond ((or (> i max) (> i cur)) 0)
(else (+ (part (- cur i) i)
(loop (+ i 1))))))))))

(part n n)) ;; Just added this in, this is an expression! so it should work?

On 9/19/06, Jos Koot <jos.koot at telefonica.net> wrote:
Look in your tutorial text, and closely follow its instructions. Which book are you using? Even if it were allowed to intermix define forms and expressions, your procedure would not return any specified value.
(define (name argument) body) means
(define name (lambda (argument) body)
A body consists of any number of define forms (called internal definitions)followed by at least one expression. The procedure returns the value of the last expression. The computation of the values of the define forms must not require the value of any of the variables being defined. (MzScheme guarantees that the values of the define forms are evaluated from left to right, but R5RS allows an interpreter/compiler to choose any arbitrary order) There are several reasons for these restrictions, one of which is good style. It is very tempting to experiment with constructs in another way than shown in your textbook, but in order to acquire good style it is wise to let yourself be guided by your book or teacher. Good style is not merely a matter of beauty, but in the end allows you to write correct programs in relatively short time and without making methodological errors. It even helps decrease the number of trivial bugs.

Jos Koot

((((lambda(x)((((((x x)x)x)x)x)x))
(lambda(x)(lambda(y)(x(x y)))))
(lambda(x)(write x)x))
"greetings, Jos")
----- Original Message -----
To: plt-scheme at list.cs.brown.edu
Sent: Tuesday, September 19, 2006 8:32 AM
Subject: [plt-scheme] Define in define

Hi, i'm using MzScheme, with my code:

(define (partition n)
(define results (make-vector (+ n 1)))
(define (update cur max val)
(vector-set! (vector-ref results cur) max val)
val)

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

(define (part cur max)
(cond ((= cur 0) 1)
((not (= (vector-ref (vector-ref results cur) max) 0))
(vector-ref (vector-ref results cur) max))
(else (update cur max (let loop ((i 1))
(cond ((or (> i max) (> i cur)) 0)
(else (+ (part (- cur i) i)
(loop (+ i 1)))))))))))

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?

----------------------------------------------------------------------------

_________________________________________________