Movatterモバイル変換


[0]ホーム

URL:


[LISPWORKS][Common Lisp HyperSpec (TM)][Previous][Up][Next]


FunctionREDUCE

Syntax:

reducefunction sequence&key key from-end start end initial-value =>result

Arguments and Values:

function---adesignator for afunction that might be called with either zero or twoarguments.

sequence---aproper sequence.

key---adesignator for afunction of one argument, ornil.

from-end---ageneralized boolean. The default isfalse.

start,end---bounding index designators ofsequence. The defaults forstart andend are0 andnil, respectively.

initial-value---anobject.

result---anobject.

Description:

reduce uses a binary operation,function, to combine theelements ofsequencebounded bystart andend.

Thefunction must accept asarguments twoelements ofsequence or the results from combining thoseelements. Thefunction must also be able to accept no arguments.

Ifkey is supplied, it is used is used to extract the values to reduce. Thekey function is applied exactly once to each element ofsequence in the order implied by the reduction order but not to the value ofinitial-value, if supplied. Thekey function typically returns part of theelement ofsequence. Ifkey is not supplied or isnil, thesequenceelement itself is used.

The reduction is left-associative, unlessfrom-end istrue in which case it is right-associative.

Ifinitial-value is supplied, it is logically placed before the subsequence (or after it iffrom-end istrue) and included in the reduction operation.

In the normal case, the result ofreduce is the combined result offunction's being applied to successive pairs ofelements ofsequence. If the subsequence contains exactly oneelement and noinitial-value is given, then thatelement is returned andfunction is not called. If the subsequence is empty and aninitial-value is given, then theinitial-value is returned andfunction is not called. If the subsequence is empty and noinitial-value is given, then thefunction is called with zero arguments, andreduce returns whateverfunction does. This is the only case where thefunction is called with other than two arguments.

Examples:

 (reduce #'* '(1 2 3 4 5)) =>  120 (reduce #'append '((1) (2)) :initial-value '(i n i t)) =>  (I N I T 1 2) (reduce #'append '((1) (2)) :from-end t                                               :initial-value '(i n i t)) =>  (1 2 I N I T)  (reduce #'- '(1 2 3 4)) ==  (- (- (- 1 2) 3) 4) =>  -8 (reduce #'- '(1 2 3 4) :from-end t)    ;Alternating sum.==  (- 1 (- 2 (- 3 4))) =>  -2 (reduce #'+ '()) =>  0 (reduce #'+ '(3)) =>  3 (reduce #'+ '(foo)) =>  FOO (reduce #'list '(1 2 3 4)) =>  (((1 2) 3) 4) (reduce #'list '(1 2 3 4) :from-end t) =>  (1 (2 (3 4))) (reduce #'list '(1 2 3 4) :initial-value 'foo) =>  ((((foo 1) 2) 3) 4) (reduce #'list '(1 2 3 4)        :from-end t :initial-value 'foo) =>  (1 (2 (3 (4 foo))))

Side Effects: None.

Affected By: None.

Exceptional Situations:

Should be prepared to signal an error oftypetype-error ifsequence is not aproper sequence.

See Also:

Section 3.6 (Traversal Rules and Side Effects)

Notes: None.


The followingX3J13 cleanup issues,not part of the specification, apply to this section:


[Starting Points][Contents][Index][Symbols][Glossary][Issues]
Copyright 1996-2005, LispWorks Ltd. All rights reserved.


[8]ページ先頭

©2009-2025 Movatter.jp