4 ms·
You can create macros that look like functions, but the macro actually has access to the the "program code", so you can do arbitrary transformations. The imple
by gus_massa 12y ago
You can create macros that look like functions, but the macro actually has access to the the "program code", so you can do arbitrary transformations.
The implementation is easier if you assume that you have exactly two generators. Here is an example in Racket. It uses define-syntax-rule that is the method to define simple macros (there are more advanced options).
(The program also define string->list/string that splits a list in a list of one-character string, because the result of string->list is a list of chars. You can ignore it safety.)
#lang racket
(define (string->list/string x)
(map (lambda (c) (list->string (list c)))
(string->list x)))
(define-syntax-rule (list-comp2 (proc v ...)
[u1 gen1]
[u2 gen2])
(map (lambda (u1 u2) (proc v ...))
gen1
gen2))
(list-comp2 (list x y)
[x (range 8)]
[y (string->list/string "ABCDEFGH")])
;==> '((0 "A") (1 "B") (2 "C") (3 "D") (4 "E") (5 "F") (6 "G") (7 "H"))
- foobarian 12y ago> but the macro actually has access to the the "program code", so you can do arbitrary transformations. I like to compare the way lisp works to Javascript and the DOM. Just like a webpage (or its JS code) has access to its internal structure and can do arbitrary transformations, so does lisp except instead of the DOM you have the abstract syntax tree.