Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can certainly write something that takes in a quoted s-expression and outputs the translation as a quoted s-expression.</p> <p>Start with simply translating well-formed lists like <code>'(#\c #\a #\d #\r)</code> into your first/rest s-expressions.</p> <p>Now build the solution with symbol?, symbol->string, regexp-match #rx"^c(a|d)+r$", string->list, and map</p> <p>Traverse the input. If it is a symbol, check the regexp (return as-is if it fails), convert to list, and use your starting translator. Recurse on the nested expressions.</p> <p>EDIT: here's some badly written code that can translate source-to-source (assuming the purpose is to read the output)</p> <pre><code>;; translates a list of characters '(#\c #\a #\d #\r) ;; into first and rest equivalents ;; throw first of rst into call (define (translate-list lst rst) (cond [(null? lst) (raise #f)] [(eq? #\c (first lst)) (translate-list (rest lst) rst)] [(eq? #\r (first lst)) (first rst)] [(eq? #\a (first lst)) (cons 'first (cons (translate-list (rest lst) rst) '()))] [(eq? #\d (first lst)) (cons 'rest (cons (translate-list (rest lst) rst) '()))] [else (raise #f)])) ;; translate the symbol to first/rest if it matches c(a|d)+r ;; pass through otherwise (define (maybe-translate sym rst) (if (regexp-match #rx"^c(a|d)+r$" (symbol-&gt;string sym)) (translate-list (string-&gt;list (symbol-&gt;string sym)) rst) (cons sym rst))) ;; recursively first-restify a quoted s-expression (define (translate-expression exp) (cond [(null? exp) null] [(symbol? (first exp)) (maybe-translate (first exp) (translate-expression (rest exp)))] [(pair? (first exp)) (cons (translate-expression (first exp)) (translate-expression (rest exp)))] [else exp])) 'test-2 (define test-2 '(cadr (1 2 3))) (maybe-translate (first test-2) (rest test-2)) (translate-expression test-2) (translate-expression '(car (cdar (list (list 1 2) 3)))) (translate-expression '(translate-list '() '(a b c))) (translate-expression '(() (1 2))) </code></pre> <p>As mentioned in the comments, I am curious why you'd want a macro. If the purpose is to translate source into something readable, don't you want to capture the output to replace the original?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload