Listng 1

CFML version:

<cfscript>
	function ArrayFind_2D(arrayToSearch,valueToFind)
	{
		var ii = 0;
		
		for(ii = 1; ii LTE arrayLen(arrayToSearch); ii = ii + 1)
		{
			if(NOT compare(arrayToSearch[ii][1],valueToFind))
				return ii;
		}
		
		return 0;
	}

	testStr = "OR,CA,WA,ID";  

	matchingBucketList = "18,38,96,118";  
	testStr = ListAppend(testStr, "ZZ");
	cntBatchNoChange = 0; 

	while( (cntBatchNoChange lt 2) and (listLen(testStr gt 1)) )
	{
		tempState = listGetAt(testStr, 1);
		testStr = listDeleteAt(testStr, 1);   
	
		if(tempState neq "ZZ") 
		{
			firstIndexOfTempStateInAll = arrayFind_2D(statesNeighbors, tempState);
			tempStatesContigList = statesNeighbors[firstIndexOfTempStateInAll][2];
			lengthOfTempStatesContigList = listLen(tempStatesContigList);
			
			for(jdx = 1; jdx lt lengthOfTempStatesContigList; jdx = jdx + 1)
			{
				if(listFindNoCase(matchingBucketList, jdx) neq 0)
				{
					cntBatchNoChange = 0;
					MatchThisBoundary = true;
					break;
				}
				else 	 
				{	
					MatchThisBoundary = false;
				}
			} 
			
			if(MatchThisBoundary eq true)
			{	
				matchingBucketList = listAppend(matchingBucketList, tempStatesContigList); 
			}
			else
			{	
				testStr = listAppend(testStr, tempState); 
			}
		}	
		else 
		{
			testStr = listAppend(testStr, tempState);
			cntBatchNoChange = cntBatchNoChange + 1;
		}
	} 

	if( ListLen(testStr) gt 1)
	{
		ZZidx = ListContains(testStr, "ZZ");
		testStr = listDeleteAt(testStr, #ZZidx#);
		result = "not contiguous because of " & testStr;
	}
	else
	{
		result = "contiguous!";
	}

</cfscript>
 

newLisp version:

(push "ZZ" testStr -1) 	

(set 'matchingBucket '(18 38 96 118))

(set 'cntBatchNoChange 0)

(while (and (< cntBatchNoChange 2 ) (> (length testStr) 1)) 
	(set 'tempState (pop testStr))
	
	(if (!= tempState "ZZ")
		(begin
			(set 'tempStatesBoundaries (rest (assoc tempState statesNeighbors)))
			
			(if (!= 0 (length (intersect tempStatesBoundaries matchingBucket))) 
				(begin 
					(push tempStatesBoundaries matchingBucket)
					(set 'matchingBucket (unique (flat matchingBucket)))
					(set 'cntBatchNoChange 0))
				(begin
					(push tempState testStr)
					(rotate testStr -1))))
		(begin 
			(push tempState testStr)
			(rotate testStr -1)
			(inc 'cntBatchNoChange))))

(if (> (length testStr) 1)
	(begin
		(replace "ZZ" testStr)
		(println "not contiguous because of " testStr))
	(println "contiguous!"))

(exit)