From: Ken A. <kan...@bb...> - 2004-09-29 14:06:40
|
At 08:54 AM 9/29/2004 -0400, Michael Thome wrote: >I've never used scheme enough to know if these are stupid questions, but: They're not. >1. why does "define" only act in the dynamic context where "set!" works will merely prefer the dynamic context? I would have expected define to always apply to the global context... >> (let ((x 1)) (define blab (lambda () (set! x (+ x 1))))) >(lambda ?? ()...) >> (blab) >(blab ) > ==================================== >SchemeException: ERROR: undefined variable "blab" >> 5.2.2 Internal definitions Definitions may occur at the beginning of a <body> (that is, the body of a lambda, let, let*, letrec. These definitions are local to be body. >2. why can't you use define wherever you want? e.g.: >> (let () (print "who") (define (me) 1)) >SchemeException: Jscheme requires all embedded defines to appear first in procedure bodies >You must move (define (me) 1) up Because the defines at the top of the body are collected and turned into a letrec. I once wrote a (def) macro that let you put defines anywhere. It grouped defines into nested letrec's. While a real Schemer might find this strange, it acts more like java and tends to keep things less nested. I'll see if i can come up with it again. >3. is there a destructuring-bind hiding in there somewhere? No. But i have used a simple macro: (define-macro (dbind var pattern . body) ;; pattern must be valid lambda argument list. `(apply (lambda ,pattern ,@body) ,var)) (dbind '(1 2 3 4 5) (a b . c) (+ a b)) -> 3. For the notam project, look at src/scm/matcher.scm for a simple matcher here's an example: (case-match e (`((xsd:group . ,x))) ; ignore (`((xsd:element (name ,name) (type ,type) . ,notes) (xsd:annotation (xsd:documentation ,doc))) (set! properties (cons (makeProperty name type doc notes) properties))) ...) Keep the questions coming! |