Re: [Chicken-users] lambda's in a hash table
On Sat, 5 Apr 2014, Thomas Chust wrote: On 2014-04-05 01:54, Claude Marinier wrote: [...] I would like to have the compiler do some of this for me. I probably cannot write a literal hash table but I expected to be able to write a literal association list. I have tried this but it does not work. [...] (define a-list `( (dot . ,(lambda () (display "dot\n"))) (dash . ,(lambda () (display "dash\n"))) )) [...] Hello, what you have written down here is not a literal list, but a quasiquotation, which is just syntactic sugar that expands to an expression dynamically constructing a list. Nevertheless, the program you posted works just fine as it is. The only problem I can see with it is that nothing visible happens because [...] (let ((func-dot (hash-table-ref dict 'dot)) (func-dash (hash-table-ref dict 'dash))) func-dot func-dash) [...] doesn't call the two procedures. To actually run the procedures, you would have to write something like (let ((func-dot (hash-table-ref dict 'dot)) (func-dash (hash-table-ref dict 'dash))) (func-dot) (func-dash)) That works. Thank you. I was so close. I think I lost track of the many variations I tried. :-( -- Claude Marinier ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] lambda's in a hash table
On 2014-04-05 01:54, Claude Marinier wrote: > [...] > I would like to have the compiler do some of this for me. I probably > cannot write a literal hash table but I expected to be able to write a > literal association list. I have tried this but it does not work. > [...] > (define a-list > `( > (dot . ,(lambda () (display "dot\n"))) > (dash . ,(lambda () (display "dash\n"))) > )) > [...] Hello, what you have written down here is not a literal list, but a quasiquotation, which is just syntactic sugar that expands to an expression dynamically constructing a list. Nevertheless, the program you posted works just fine as it is. The only problem I can see with it is that nothing visible happens because > [...] > (let ((func-dot (hash-table-ref dict 'dot)) > (func-dash (hash-table-ref dict 'dash))) > func-dot > func-dash) > [...] doesn't call the two procedures. To actually run the procedures, you would have to write something like (let ((func-dot (hash-table-ref dict 'dot)) (func-dash (hash-table-ref dict 'dash))) (func-dot) (func-dash)) Ciao, Thomas -- When C++ is your hammer, every problem looks like your thumb. ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
[Chicken-users] lambda's in a hash table
Bonjour, I can add lambdas to a hash table at run time, retrieve them, and execute them. This is very useful. I would like to have the compiler do some of this for me. I probably cannot write a literal hash table but I expected to be able to write a literal association list. I have tried this but it does not work. (declare (uses srfi-69)) (define a-list `( (dot . ,(lambda () (display "dot\n"))) (dash . ,(lambda () (display "dash\n"))) )) (define dict (alist->hash-table a-list)) (let ((func-dot (hash-table-ref dict 'dot)) (func-dash (hash-table-ref dict 'dash))) func-dot func-dash) I have tried various ways of quoting but have not yet found something which works. Can this be done or do I have to create it when the program starts? Merci. -- Claude Marinier ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users