我将在数学中进行解释,这是我正在努力编写Scheme代码的转换:
(f '(a b c) '(d e f)) = '(ad (+ bd ae) (+ cd be af) (+ ce bf) cf)
其中两个字母在一起,例如
ad
表示(* a d)
。我试图以一种纯粹的功能性方式编写它,但是我却在努力寻找方法。任何建议将不胜感激。
这里有些例子:
(1mul '(0 1) '(0 1)) = '(0 0 1)
(1mul '(1 2 3) '(1 1)) = '(1 3 5 3)
(1mul '(1 2 3) '(1 2)) = '(1 4 7 6)
(1mul '(1 2 3) '(2 1)) = '(2 5 8 3)
(1mul '(1 2 3) '(2 2)) = '(2 6 10 6)
(1mul '(5 5 5) '(1 1)) = '(5 10 10 5)
(1mul '(0 0 1) '(2 5)) = '(0 0 2 5)
(1mul '(1 1 2 3) '(2 5)) = '(2 7 9 16 15)
因此,该模式类似于我一开始发布的内容:
将列表中的第一个数字乘以第二个列表中的每个数字(ad,ae,af),然后继续(bd,be,bf,cd,ce,cf)并按“某种方式”排列数字以添加相应的数字价值观。我称其为重叠的原因是因为您可以像这样将其可视化:
(list
aa'
(+ ba' ab')
(+ ca' bb' ac')
(+ cb' bc')
cc')
再次,
(f '(a b c) '(d e f)) = '(ad (+ bd ae) (+ cd be af) (+ ce bf) cf)
但是,不仅限于3x3列表,还包括任何大小的列表。
最佳答案
这是我的代码。在球拍里
#lang racket
(define (drop n xs)
(cond [(<= n 0) xs]
[(empty? xs) '()]
[else (drop (sub1 n) (rest xs))]))
(define (take n xs)
(cond [(<= n 0) '()]
[(empty? xs) '()]
[else (cons (first xs) (take (sub1 n) (rest xs)))]))
(define (mult as bs)
(define (*- a b)
(list '* a b))
(define degree (length as))
(append
(for/list ([i (in-range 1 (+ 1 degree))])
(cons '+ (map *- (take i as) (reverse (take i bs)))))
(for/list ([i (in-range 1 degree)])
(cons '+ (map *- (drop i as) (reverse (drop i bs)))))))
for/list
只是映射数字列表并将结果收集到列表中的方式。如果需要,我可以将其重新设置为地图。