Scheme 产品作为输入参数?

Scheme 产品作为输入参数?,scheme,racket,Scheme,Racket,在lisp/scheme中,是否有任何形式使用集合乘积作为函数输入?映射表单对需要n个参数的函数使用n个长度相等的列表。有时,我们需要参数来自一组集合的乘积。例如: (pmap (λ (d p) foo) A B) 这里,列表A可能与B具有不同的长度,并且pmap将A和B的乘积的每个元素馈送到lambda表达式 方案/球拍可完成此项工作的表格*: (for* ([x '(0 2 4)] [y '(1 3 5)]) ((λ (d p)

在lisp/scheme中,是否有任何形式使用集合乘积作为函数输入?映射表单对需要n个参数的函数使用n个长度相等的列表。有时,我们需要参数来自一组集合的乘积。例如:

(pmap (λ (d p) foo)
           A B)
这里,列表A可能与B具有不同的长度,并且pmapAB的乘积的每个元素馈送到lambda表达式

方案/球拍可完成此项工作的表格*:

(for* ([x '(0 2 4)]
       [y '(1 3 5)])
      ((λ (d p)
           (printf "(~a, ~a)\n" d p))
       x y))
输出:

(0, 1)
(0, 3)
(0, 5)
(2, 1)
(2, 3)
(2, 5)
(4, 1)
(4, 3)
(4, 5)

我想知道是否存在类似于mapfold的其他方法来在scheme中执行此操作。

据我所知,标准中不存在此类方法。然而,写一个也不是问题

对于有用的列表函数的概述,我可以推荐srfi1,它提供了除了map和fold之外的许多有用的操作


我编写了
pmap
的以下实现。它只使用
cons
car
cdr
null?
apply
map
reverse
工作,并支持像
map
那样的任意数量的参数

(define (pmap f . xs) 
  (define (carry a xs ys then)
    (if (and (not (null? ys)) (null? (car ys)))
        '()
        (if (null? xs) 
            (then (reverse a))
            (if (null? (car xs))
                (if (null? (cdr xs)) 
                    '()
                    (carry (cons (car ys) a) (cons (cdr (car (cdr xs))) (cdr (cdr xs))) (cdr ys) then))
                (carry (cons (car xs) a) (cdr xs) (cdr ys) then)))))

  (define (pmap-helper f xs ys)
    (carry '() xs ys 
           (lambda (xs)
             (cons (apply f (map car xs)) 
                   (pmap-helper f (cons (cdr (car xs)) (cdr xs)) ys)))))

  (pmap-helper f xs xs))

(display (pmap list '(0 2 4) '(1 3 5))) (newline)
;((0 1) (2 1) (4 1) (0 3) (2 3) (4 3) (0 5) (2 5) (4 5))
唯一的区别是,前面的列表的迭代速度比后面的列表快,而后面的列表正好相反。此
pmap
可修改为:

(define (pmap f . xs) 
  (define (carry a xs ys then)
    (if (and (not (null? ys)) (null? (car ys)))
        '()
        (if (null? xs) 
            (then (reverse a))
            (if (null? (car xs))
                (if (null? (cdr xs)) 
                    '()
                    (carry (cons (car ys) a) (cons (cdr (car (cdr xs))) (cdr (cdr xs))) (cdr ys) then))
                (carry (cons (car xs) a) (cdr xs) (cdr ys) then)))))

  (define (pmap-helper f xs ys)
    (carry '() xs ys 
           (lambda (xs)
             (cons (apply f (reverse (map car xs))) 
                   (pmap-helper f (cons (cdr (car xs)) (cdr xs)) ys)))))

  (let ((xs (reverse xs)))
    (pmap-helper f xs xs)))

(display (pmap list '(0 2 4) '(1 3 5))) (newline)
; ((0 1) (0 3) (0 5) (2 1) (2 3) (2 5) (4 1) (4 3) (4 5))