gpt4 book ai didi

macros - 如何在 guile scheme 中扩展宏?

转载 作者:太空宇宙 更新时间:2023-11-03 19:02:01 26 4
gpt4 key购买 nike

我正在尝试写 let over lambda defmacro/g!在诡计中。我有这个:

(use-modules (srfi srfi-1))

(define (flatten x)
(let rec ((x x) (acc '()))
(cond ((null? x) acc)
((not (pair? x)) (cons x acc))
(else
(rec (car x)
(rec (cdr x) acc))))))

(define (g!-symbol? s)
(and (symbol? s)
(let ((symbol-string (symbol->string s)))
(and (> (string-length symbol-string) 2)
(equal? (string-downcase (substring symbol-string 0 2)) "g!")))))


(define-macro (define-macro/g! name-args . body)
(let ((syms (delete-duplicates
(filter g!-symbol? (flatten body)))))
`(define-macro ,name-args
(let ,(map
(lambda (s)
`(,s (gensym ,(substring (symbol->string s) 2))))
syms)
,@body))))

但是当我尝试宏扩展 define-macro/g 时!使用这个:

(use-modules (language tree-il))
(tree-il->scheme (macroexpand '(define-macro/g! (foo . body) `(let ((g!car ,(car body))) g!car))))

我有这个:

$15 = (if #f #f)

为什么我得到这个结果?如何扩展 define-macro/g!

最佳答案

我需要使用这段代码:

(define macro '(define-macro/g! (foo . body) `(let ((g!car ,(car body))) g!car)))
(tree-il->scheme (macroexpand macro 'c '(compile load eval)))

关于macros - 如何在 guile scheme 中扩展宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38573041/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com