List Scheme-列表中偶数值元素的平方和
我希望能够对列表中偶数元素的平方进行求和,但是我当前的代码只对元素求和,而不是平方。有人知道可以对列表中偶数值元素的平方和进行任何修改吗List Scheme-列表中偶数值元素的平方和,list,scheme,filtering,singly-linked-list,fold,List,Scheme,Filtering,Singly Linked List,Fold,我希望能够对列表中偶数元素的平方进行求和,但是我当前的代码只对元素求和,而不是平方。有人知道可以对列表中偶数值元素的平方和进行任何修改吗 (define (sum elemList) (if (null? elemList) 0 (+ (car elemList) (sum (cdr elemList))) ) ) 我的意见是: (sum-evens (list 1 2 3 4)) 20 产出将是: (sum-evens (list 1 2 3 4)) 2
(define (sum elemList)
(if
(null? elemList)
0
(+ (car elemList) (sum (cdr elemList)))
)
)
我的意见是:
(sum-evens (list 1 2 3 4))
20
产出将是:
(sum-evens (list 1 2 3 4))
20
这是(2*2)+(4*4)
如果可能,最好同时看到递归和迭代解决方案。
有什么想法吗
(define (sum ls)
(if (null? ls)
0
(if (even? (car ls))
(+ (square (car ls)) (sum (cdr ls)))
(sum (cdr ls)))))
在哪里
求偶数元素的平方和。如果你不做任何事情就对列表中的元素求和,那么答案当然不能是你问题的答案
此外,可以通过以下方式实施此程序:
(define (sum ls)
(reduce +
0
(map square
(filter even?
ls))))
其中,map
,filter
和reduce
是常见的含义(您可以在mit方案中尝试)。这也可以做同样的事情,但是更具可读性,而且cdr递归之类的事情也得到了优化。SICP中的第二章(结构与解释)
介绍了这种编程方法
在哪里
求偶数元素的平方和。如果你不做任何事情就对列表中的元素求和,那么答案当然不能是你问题的答案
此外,可以通过以下方式实施此程序:
(define (sum ls)
(reduce +
0
(map square
(filter even?
ls))))
其中,map
,filter
和reduce
是常见的含义(您可以在mit方案中尝试)。这也可以做同样的事情,但是更具可读性,而且cdr递归之类的事情也得到了优化。SICP中的第二章(结构与解释)
(计算机程序)介绍了这种编程方法。有两种可能性,或者我们从头开始实现递归:
(define (sum elemList)
(cond ((null? elemList) 0)
((even? (car elemList))
(+ (* (car elemList) (car elemList))
(sum (cdr elemList))))
(else (sum (cdr elemList)))))
或者我们使用内置的过程,根据需要定义助手。此策略称为使用:
在这个方案中,首选的方法是第二种,因为当我们有已经为我们完成工作的程序时,我们决不能重新发明轮子。无论哪种方式,它都能按预期工作:
(sum '(1 2 3 4 5 6 7 8 9 10))
=> 220
有两种可能,或者我们从头开始实现递归:
(define (sum elemList)
(cond ((null? elemList) 0)
((even? (car elemList))
(+ (* (car elemList) (car elemList))
(sum (cdr elemList))))
(else (sum (cdr elemList)))))
或者我们使用内置的过程,根据需要定义助手。此策略称为使用:
在这个方案中,首选的方法是第二种,因为当我们有已经为我们完成工作的程序时,我们决不能重新发明轮子。无论哪种方式,它都能按预期工作:
(sum '(1 2 3 4 5 6 7 8 9 10))
=> 220
利用球拍的左折功能
(define (foldl cons z ls)
(if (null? ls)
z
(foldl cons (cons (car ls) z) ; NB! args order
(cdr ls))))
我们可以很容易地实现列表的求和((foldl+0xs)
),或者分别获取平方或过滤
嵌套它们也很容易,这样一个可以处理另一个的结果(如其他答案所示),但这意味着要执行三次单独的列表遍历
(define (sqr x) (* x x))
(define (foo-3 xs)
(foldl + 0
(foldl (lambda (x acc) (cons (sqr x) acc)) '()
(foldl (lambda (x acc) (if (even? x) (cons x acc) acc)) '()
xs))))
此外
(define (foo-1 xs) ; one traversal, tail-recursive, iterative!
(foldl (lambda (x acc) (if (even? x) (+ (sqr x) acc) acc)) 0
xs))
从而得出所谓的“传感器”,即减速器功能的修饰符:
(define (((mapping f) kons) x acc) (kons (f x) acc)) ; "mapping" transducer
(define (((filtering p) kons) x acc) (if (p x) (kons x acc) acc)) ; "filtering" one
(define (foo xs)
(foldl + 0
(foldl ((mapping sqr) cons) '()
(foldl ((filtering even?) cons) '()
xs)))
=
(foldl ((mapping sqr) +) 0 ; replace the constructor!
(foldl ((filtering even?) cons) '() ; and the sentinel value
xs))
=
(foldl ((filtering even?) ((mapping sqr) +)) 0 ; and again!
; (lambda (x acc) (if (even? x) (+ (sqr x) acc) acc)) ; look, ma, no cons!
xs)
)
所以(f(gx))
就是((compose1fg)x)
。更通用的compose
可接受任意数量的函数进行组合
(define ((compose . fs) x)
(if (null? fs)
x
((car fs) ((apply compose (cdr fs)) x))))
我们可以用更通用的方式对其进行编码,将可能需要的多个传感器组合成一个组合传感器,对输入的每个参数执行组合操作(取自输入序列;此处为列表):
就这样
所以我们有
(define (sqr1 x) (+ 1 (* x x))) ; for clearer testing results
> (foldl ((mapping sqr1) cons) '() '(1 2 3 4))
'(17 10 5 2)
> (foldl ((mapping sqr1) +) 0 '(1 2 3 4))
> 34
((映射sqr1)cons)
,就像cons
本身一样,是两个参数的函数,因此可以用作foldl
的reducer函数参数
具有(定义g((映射sqr1)cons))
与
(define (g x acc)
(cons (sqr1 x) acc))
通过过滤
我们可以
> (foldl ((filtering even?) +) 0 '(1 2 3 4))
> 6
> (foldl ((mapping sqr1) ((filtering even?) cons)) '() '(1 2 3 4))
> '(10 2)
> (foldl ((filtering even?) ((mapping sqr1) cons)) 0 '(1 2 3 4))
> '(17 5 . 0)
所以,((映射sqr1)((过滤偶数?)cons))
是一个减缩器,其中(映射sqr1)
使用((过滤偶数?)cons)
作为其减缩器。也就是说,使用cons
作为其链中的最后一个减速机功能,即(甚至过滤?
):
(define g
((mapping sqr1) ((filtering even?) cons)))
=
(define (g x acc)
(let ((f ((filtering even?) cons)))
(f (sqr1 x) acc))) ; by definition of mapping
=
(define (g x acc)
(define (f y acc)
(if (even? y) (cons y acc) acc)) ; by definition of filtering
(f (sqr1 x) acc))
=
(define (g x acc)
(let ((y (sqr1 x)))
(if (even? y) (cons y acc) acc))) ; by application rule
嗯,映射、过滤和考虑都自动地汇总到一个reducer函数中,就像我们自己编写的一样!更好的是,foldl
是尾部递归的,整个函数是迭代的,只执行一次列表遍历,因为三个reducer函数组合成一个
还有一些测试:
(define (bar xs)
(foldl ((compose
(filtering even?) ; filtering is done first
(mapping sqr1))
cons)
0 xs))
(define (baz xs)
(foldl ((compose
(mapping sqr1) ; mapping is done first
(filtering even?))
cons)
'() xs))
所以
> (bar '(1 2 3 4 5))
'(17 5 . 0)
> (baz '(1 2 3 4 5))
'(26 10 2)
利用球拍的左折功能
(define (foldl cons z ls)
(if (null? ls)
z
(foldl cons (cons (car ls) z) ; NB! args order
(cdr ls))))
我们可以很容易地实现列表的求和((foldl+0xs)
),或者分别获取平方或过滤
嵌套它们也很容易,这样一个可以处理另一个的结果(如其他答案所示),但这意味着要执行三次单独的列表遍历
(define (sqr x) (* x x))
(define (foo-3 xs)
(foldl + 0
(foldl (lambda (x acc) (cons (sqr x) acc)) '()
(foldl (lambda (x acc) (if (even? x) (cons x acc) acc)) '()
xs))))
此外
(define (foo-1 xs) ; one traversal, tail-recursive, iterative!
(foldl (lambda (x acc) (if (even? x) (+ (sqr x) acc) acc)) 0
xs))
从而得出所谓的“传感器”,即减速器功能的修饰符:
(define (((mapping f) kons) x acc) (kons (f x) acc)) ; "mapping" transducer
(define (((filtering p) kons) x acc) (if (p x) (kons x acc) acc)) ; "filtering" one
(define (foo xs)
(foldl + 0
(foldl ((mapping sqr) cons) '()
(foldl ((filtering even?) cons) '()
xs)))
=
(foldl ((mapping sqr) +) 0 ; replace the constructor!
(foldl ((filtering even?) cons) '() ; and the sentinel value
xs))
=
(foldl ((filtering even?) ((mapping sqr) +)) 0 ; and again!
; (lambda (x acc) (if (even? x) (+ (sqr x) acc) acc)) ; look, ma, no cons!
xs)
)
所以(f(gx))
就是((compose1fg)x)
。更通用的compose
可接受任意数量的函数进行组合
(define ((compose . fs) x)
(if (null? fs)
x
((car fs) ((apply compose (cdr fs)) x))))
我们可以用更通用的方式对其进行编码,将可能需要的多个传感器组合成一个组合传感器,对输入的每个参数执行组合操作(取自输入序列;此处为列表):
就这样
所以我们有
(define (sqr1 x) (+ 1 (* x x))) ; for clearer testing results
> (foldl ((mapping sqr1) cons) '() '(1 2 3 4))
'(17 10 5 2)
> (foldl ((mapping sqr1) +) 0 '(1 2 3 4))
> 34
((映射sqr1)cons)
,就像cons
本身一样,是两个参数的函数,因此可以用作foldl
的reducer函数参数
具有(定义g((映射sqr1)cons))
与
(define (g x acc)
(cons (sqr1 x) acc))
通过过滤
我们可以
> (foldl ((filtering even?) +) 0 '(1 2 3 4))
> 6
> (foldl ((mapping sqr1) ((filtering even?) cons)) '() '(1 2 3 4))
> '(10 2)
> (foldl ((filtering even?) ((mapping sqr1) cons)) 0 '(1 2 3 4))
> '(17 5 . 0)
所以,((映射sqr1)((过滤偶数?)cons))
是一个减缩器,其中(映射sqr1)
使用((过滤偶数?)cons)
作为其减缩器。也就是说,使用cons
作为其链中的最后一个减速机功能,即(甚至过滤?
):
(define g
((mapping sqr1) ((filtering even?) cons)))
=
(define (g x acc)
(let ((f ((filtering even?) cons)))
(f (sqr1 x) acc))) ; by definition of mapping
=
(define (g x acc)
(define (f y acc)
(if (even? y) (cons y acc) acc)) ; by definition of filtering
(f (sqr1 x) acc))
=
(define (g x acc)
(let ((y (sqr1 x)))
(if (even? y) (cons y acc) acc))) ; by application rule
Mmm、映射、过滤和考虑都自动汇总到一个reducer函数中,就像我们自己编写的一样