At the first,I will put some code that would be used in the exercise.At this section,I skip some exercises.
2.33
(defun mapv2(p sequence) (accumulate (lambda(x y)(cons (funcall x) y))
nil
sequence))
(defun appendv2(append seq1 seq2)
(accumulate #'cons
seq1
seq2))
(defun lengthv2(sequence)
(accumulate (lambda(x y)(+ 1 y))
0
sequence))
2.34
(defun horner-eval(x coff-seq)
(accumulate (lambda(this-coeff higher-terms)
(+ this-coeff (* x higher-terms)))
0
coff-seq))
2.35
(defun count-leaves(tree) (accumulate #'+
0
(mapcar (lambda(x) (if (listp x) (length x) 1))
tree)))
2.36
(defun accumulate-n(op init seqs)
(if (null (car seqs))
init
(cons (accumulate op init (mapcar #'car seqs))
(accumulate-n op init (mapcar #'cdr seqs)))))
(if (null (car seqs))
init
(cons (accumulate op init (mapcar #'car seqs))
(accumulate-n op init (mapcar #'cdr seqs)))))
No comments:
Post a Comment