Ask Ben: Recursive Numeric Pyramid

<cffunction
	name="OutputPyramid"
	access="public"
	returntype="void"
	output="true"
	hint="Outputs the numeric pyramid.">
 
	<!--- Define arguments. --->
	<cfargument
		name="TargetHeight"
		type="numeric"
		required="true"
		hint="The max height of the numeric pyramid."
		/>
 
	<cfargument
		name="CurrentHeight"
		type="numeric"
		required="false"
		default="0"
		hint="The current height of the output pyramid."
		/>
 
	<!--- Define the local scope. --->
	<cfset var LOCAL = StructNew() />
 
 
	<!---
		Check to see if the current height is equal to the
		target height. In that case, we are going to output
		just one row.
	--->
	<cfif (ARGUMENTS.TargetHeight EQ ARGUMENTS.CurrentHeight)>
 
		<!--- Simply output the row to max height. --->
		<cfloop
			index="LOCAL.Column"
			from="0"
			to="#ARGUMENTS.TargetHeight#"
			step="1">
 
			#LOCAL.Column#
 
		</cfloop>
 
		<br />
 
	<cfelse>
 
		<!---
			Since we are not outputting the max height row, we
			want to output TWO of the same column sets with a
			recursive call in the middle.
		--->
 
		<!--- Output first row. --->
		<cfloop
			index="LOCAL.Column"
			from="0"
			to="#ARGUMENTS.CurrentHeight#"
			step="1">
 
			#LOCAL.Column#
 
		</cfloop>
 
		<br />
 
		<!---
			In between our two mirrow columns, we want to output
			the middle content. These are the rows with heigher
			columns (and eventually the one max row). To make
			this recursive, make sure to re-call this method
			with a larget current row.
		--->
		<cfset OutputPyramid(
			TargetHeight = ARGUMENTS.TargetHeight,
			CurrentHeight = (ARGUMENTS.CurrentHeight + 1)
			) />
 
		<!--- Output second row (same as first). --->
		<cfloop
			index="LOCAL.Column"
			from="0"
			to="#ARGUMENTS.CurrentHeight#"
			step="1">
 
			#LOCAL.Column#
 
		</cfloop>
 
		<br />
 
	</cfif>
 
 
	<!--- Return out. --->
	<cfreturn />
</cffunction>

For Cut-and-Paste