cadr、caddr 等的可变版本

发布于 2024-11-14 07:15:50 字数 206 浏览 1 评论 0原文

我想知道如何在 Racket 中实现 cadr、caddr 等的可变版本,而不需要单独定义每个版本? IE。 not

(define (mcadr exp)
    (mcar (mcdr exp)))

似乎对于可变列表或对,Racket 仅支持 mcar 和 mcdr,但不支持“扩展”版本。我需要了解并擅长宏才能做到这一点吗?

I'm wondering how to implement mutable versions of cadr, caddr, and the likes in Racket without defining each one separately? ie. not

(define (mcadr exp)
    (mcar (mcdr exp)))

It seems that for mutable lists or pairs, Racket only supports mcar and mcdr but not the "expanded" versions. Do I need to know and be good at macros to be able to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荆棘i 2024-11-21 07:15:50

这是一个宏解决方案:

#lang racket/base

(require racket/mpair (for-syntax racket/base))

(define-syntax (define-combinations stx)
  (syntax-case stx ()
    [(_ n) (integer? (syntax-e #'n))
     (let ([n (syntax-e #'n)])
       (define options (list (cons "a" #'mcar) (cons "d" #'mcdr)))
       (define (add-options r)
         (apply append
                (map (λ (opt)
                       (map (λ (l) (cons (string-append (car opt) (car l))
                                         (list (cdr opt) (cdr l))))
                            r))
                     options)))
       (define combinations
         (cdddr
          (let loop ([n n] [r '(("" . x))])
            (if (zero? n) r (append r (loop (sub1 n) (add-options r)))))))
       (define (make-name combo)
         (let ([s (string->symbol (string-append "mc" (car combo) "r"))])
           (datum->syntax stx s stx)))
       (with-syntax ([(body ...) (map cdr combinations)]
                     [(name ...) (map make-name combinations)])
         #'(begin (define (name x) body) ...)))]))

(define-combinations 4)
(mcaddr (mlist 1 2 3 4 5))

Here's a macro solution:

#lang racket/base

(require racket/mpair (for-syntax racket/base))

(define-syntax (define-combinations stx)
  (syntax-case stx ()
    [(_ n) (integer? (syntax-e #'n))
     (let ([n (syntax-e #'n)])
       (define options (list (cons "a" #'mcar) (cons "d" #'mcdr)))
       (define (add-options r)
         (apply append
                (map (λ (opt)
                       (map (λ (l) (cons (string-append (car opt) (car l))
                                         (list (cdr opt) (cdr l))))
                            r))
                     options)))
       (define combinations
         (cdddr
          (let loop ([n n] [r '(("" . x))])
            (if (zero? n) r (append r (loop (sub1 n) (add-options r)))))))
       (define (make-name combo)
         (let ([s (string->symbol (string-append "mc" (car combo) "r"))])
           (datum->syntax stx s stx)))
       (with-syntax ([(body ...) (map cdr combinations)]
                     [(name ...) (map make-name combinations)])
         #'(begin (define (name x) body) ...)))]))

(define-combinations 4)
(mcaddr (mlist 1 2 3 4 5))
少女净妖师 2024-11-21 07:15:50

你可以这样做:

(define mcaar (compose mcar mcar))
(define mcadr (compose mcar mcdr))
;; ...
(define mcddddr (compose mcdr mcdr mcdr mcdr))

但是并没有真正避免重复。即使在 Racket 源代码中(查看 racket/src/list.c),也存在重复,尽管用 C 宏进行了一些美化。

You could do:

(define mcaar (compose mcar mcar))
(define mcadr (compose mcar mcdr))
;; ...
(define mcddddr (compose mcdr mcdr mcdr mcdr))

But there is no real getting around the repetition. Even in the Racket source (look in racket/src/list.c), the repetition is there, albeit prettified a little with C macros.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文