.
먼저 '(#\C#\a #\d #\r)
과 같은 올바른 형식의 목록을 첫 번째/나머지 s- 표현식으로 변환하는 것으로 시작하십시오.
지금 기호의 해결책? 기호 설명> 문자열, 정규 표현식 매치 #rx "^ C (A | D) + r에 $"구축, 현악기와> 목록을, 입력 트래버스
매핑합니다. 기호이면 regexp (실패한 경우 그대로 반환)을 확인하고 목록으로 변환 한 다음 시작 번역자를 사용하십시오. 중첩 된 표현식에 대한 재귀.
편집 : 여기에 소스 - 소스를 번역 할 수있는 몇 가지 잘못 작성된 코드의
코멘트에서 언급 한 바와 같이
;; 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->string sym))
(translate-list (string->list (symbol->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)))
당신이 원하는 것, 왜 내가 궁금 (목적을 가정하면 출력을 읽는 것입니다) 매크로. 소스를 읽을 수있는 것으로 변환하는 것이 목적이라면 원본을 대체 할 출력을 캡처하고 싶지 않습니까?
내가 할 수 있었다면 나는 이것을 여러 번 +1 할 것이다. 아주 좋아! –
우수! 이 솔루션은 제가 원했던 것입니다, 감사합니다! 라켓은 정말 아름답고 강력한 언어입니다. –
@RacketNoob 대부분의 Racket 개발자는 프로그램 설계 방법을 권장합니다. 그것은 Racket 설명서가 아니며 '# % top'을 다루지는 못하지만 읽을만한 유용한 책입니다. –