https://github.com/jrincayc/ucblogo-code
Raw File
Tip revision: 007bb0ba3f02a27ba0b6dbe4ca6a5222bf8110f0 authored by Joshua Cogliati on 17 January 2023, 03:00:41 UTC
Merge pull request #146 from jrincayc/release_623_changes
Tip revision: 007bb0b
reduce
REDUCE template data					(library procedure)

	outputs the result of applying the template to accumulate the
	members of the data input.  The template must be a two-slot
	function.  Typically it is an associative function name like SUM.
	If the data input has only one constituent (member in a list or
	character in a word), the output is that consituent.  Otherwise,
	the template is first applied with ?1 filled with the next-to-last
	consitient and ?2 with the last constituent.  Then, if there are
	more constituents, the template is applied with ?1 filled with the
	next constituent to the left and ?2 with the result from the
	previous evaluation.  This process continues until all constituents
	have been used.  The data input may not be empty.

	Note: If the template is, like SUM, the name of a procedure that is
	capable of accepting arbitrarily many inputs, it is more efficient
	to use APPLY instead of REDUCE.  The latter is good for associative
	procedures that have been written to accept exactly two inputs:

		to max :a :b
		output ifelse :a > :b [:a] [:b]
		end

		print reduce "max [...]

	Alternatively, REDUCE can be used to write MAX as a procedure
	that accepts any number of inputs, as SUM does:

		to max [:inputs] 2
		if emptyp :inputs ~
		   [(throw "error [not enough inputs to max])]
		output reduce [ifelse ?1 > ?2 [?1] [?2]] :inputs
		end

back to top