in case someone finds this useful :)
++++++++++++++++ hi vincent, how are you? i would like to ask you this , i'm trying to mass produce definitions, however `eval is tricky: (define l1 (list 'a 'b)) (define l2 (list (list 1 2) (list 3 4))) (for ([i (in-list l1)] [j (in-list l2)]) (eval `(define i j))) i have tried (eval (list 'define i j)) and other variants but nothing works. =.= can you explain to me a bit about this? (i have 2 exams this week in order to be accepted into the second year, that's why i have gone into hibernation since september y.y) thank you, +++++++++++++++++ Hi Chi, On Wed, 26 Oct 2016 04:41:16 -0500, Nguyen Linh Chi wrote: > > hi vincent, > > how are you? I'm doing great! Now that I'm done with releasing Racket 6.7, I'm a little bit less busy. :) > i would like to ask you this , i'm trying to mass produce definitions, > however `eval is tricky: > > (define l1 (list 'a 'b)) > (define l2 (list (list 1 2) (list 3 4))) > (for ([i (in-list l1)] [j (in-list l2)]) (eval `(define i j))) > > i have tried (eval (list 'define i j)) and other variants but nothing > works. > =.= > can you explain to me a bit about this? As you say, `eval` is tricky. It's almost never the right tool to use in Racket. Matthew wrote a great blog post about this: http://blog.racket-lang.org/2011/10/on-eval-in-dynamic-languages-generally.html What you're trying to do, really, is to write a program (the loop) that generates a program (a bunch of definitions). That's exactly what Racket's macros are for. As a general intro to macros, and Racket's syntax system in general, I highly recommend Greg Hendershott's Fear of Macros: http://www.greghendershott.com/fear-of-macros/ Now, as for the specific example you're interested in, here's how you would do it with a macro: (define-syntax-rule (multi-define (var ...) (val ...)) (begin (define var val) ...)) (multi-define (a b) ((list 1 2) (list 3 4))) and then you use `a` and `b` as if they were any variables. What this code does is define a new form, `multi-define`. This new form expects, first, a sequence of variable names (identifiers) inside parentheses, then second, a sequence of values to use for their bindings. Then, this new form *expands* into (that is, creates new code) a series of definitions (that's what the `...` do. that's actual syntax). Then, the code uses that new form to define `a` and `b`. Note that the subforms (like arguments, but for syntactic forms) are just sequences within parentheses. I.e., they are not list *values*. They exist purely as syntactic constructs. Now, what this means is that if you want to *compute* the lists of variables and values, things get a bit more complicated. You'd need to do that computation at expansion time. That's absolutely doable, and I'm happy to discuss it further if that's indeed what you want to do. If so, I'd recommend you read Greg's guide first, just to get the general idea. With that said, that was the answer to the question you actually asked. Now, I'll give you the answer to a slightly different question, whose answer is simpler, but that may still solve the more general problem you're having. Variables are useful because they allow you to use the *name* of something (e.g., `x`) and get access to its *value* (e.g., `2`, assuming you had `(define x 2)` somewhere). Now, variables are not the only way of mapping names (or *keys*) to values. A hash table (i.e., `make-hash`) gives you that too. You put key-value pairs in the hash table, then later you can ask for the value that is associated with a certain key. Of course, they're pros and cons to using variables vs hash tables. So each of them is appropriate in different contexts. In this case, the advantage of hash tables that interest you is that they're regular values, like numbers, or lists. Which means you can create them easily at runtime, from lists of keys and lists of values (e.g., using `for/hash`). One disadvantage is that hash tables are just "global" mappings (i.e., a key will map to one value, no matter what), whereas variables can have scope, shadowing, etc. This may or may not matter in your case. Finally, more of a surface difference, you can get the value of a variable just by writing its name down (e.g., `x`). With a hash table, you need to get the value explicitly (e.g., `(hash-ref my-table x))`. In any case, I'd recommend you look into whether hash tables may be able to solve your problem before diving too deep into macros. Both are useful, of course, but I'd recommend using the simplest tool that will do the job, and hash tables are simpler than macros. > (i have 2 exams this week in order to be accepted into the second year, > that's why i have gone into hibernation since september y.y) Oh wow. I remember exams; that was a long time ago. I don't miss them. :) Good luck! +++++++++++++++ vincent if you answer this long, prob'ly i should post it on the mailing list ^^ try kungfu & qigong vincent, see how you manifest in combat i'd read your mail now ^^ have a happy day -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.