return from nested define/loops

Q&A's, tips, howto's
Locked
newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

return from nested define/loops

Post by newdep »

Hi Anyone,

basicly a simple issue but i cant get it working...
(last resort of help was using catch and throw but thats not 100%
what i want)

I would like to have a nested (define...) to break/return at
a certain point and then return the value to the main (define..)

As in the following example where (one) should return nil of true
from (four) or from (three)

;--------
(define (one)
(define (two)
(unless (someone)
(define (three) (unless (here?) nil true)))
(define (four) (unless (this-is-it) nil true))))
)

(unless (one) "jep" "no")
;--------


I like the way of having a 'break inside any (define) that always
returns to main-(define) weather its a loop or a (define) itself, a break/return should stop at current execution point and return value...but i can only find the way of using throw and catch but that is not always working and is generaly creating more code then needed...

(assuming 'break is not used for debug/tracing)
;--------
(define (one)
(define (two)
(unless (someone)
(define (three) (unless (here?) (break nil) (break true))))
(define (four) (unless (this-is-it) (break nil) (break true)))))
)

(unless (one) "jep" "no")
;--------
Any idea's?

Norman.
-- (define? (Cornflakes))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Can you tell why you would want to do nested defines? They are perfectly legal, I just wonder what you want to accomplish with it. What would be the application?

I have used nested defines in rare occacions but during loading of sorce code when all defines are made, and then for later execution. Defining a function for immedeate execution would be a waste of CPU time. Perhaps for what you want to do it is better to conditionally construct a lambda expression and apply it in one swoop?

If you really have to, then 'catch' and 'throw' are the only way to return from from the middle of a user defined function. There is a more complex example of it in the 'exception handling' benchmark.

Lutz

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Thanks Lutz.. Ill rethink my code see if its possible..
-- (define? (Cornflakes))

Locked