ampleis
if(i:=find(\"or\",sentence))>5thenwrite(i)
Herethefirstresultproducedbythegenerator,3,isassignedtoi,butthisvalueisnotgreaterthan5andthecomparisonoperationfails.Atthispoint,thegeneratorisresumedandproducesthesecondposition,23,whichisgreaterthan5.Thecomparisonoperationthensucceedsandthevalue23iswritten.Becauseoftheinheritanceoffailureandthefactthatcomparisonoperationsreturnthevalueoftheirrightargument,thisexpressioncanbewritteninthefollowingmorecompactform:
write(5 Goal-directedevaluationisinherentintheexpressionevaluationmechanismofIconandcanbeusedinarbitrarilycomplicatedsituations.Forexample, find(\"or\",sentence1)=find(\"and\",sentence2) succeedsif\"or\"occursinsentence1atthesamepositionas\"and\"occursinsentence2. Ageneratorcanberesumedrepeatedlytoproduceallitsresultsbyusingtheevery-docontrolstructure.Anexampleis everyi:=find(\"or\",sentence) dowrite(i) whichwritesallthepositionsatwhich\"or\"occursinsentence.Fortheexampleabove,theseare3,23,and33. Generationisinheritedlikefailure,andthisexpressioncanbewrittenmoreconciselybyomittingtheoptionaldoclause: everywrite(find(\"or\",sentence)) Thereareseveralbuilt-ingeneratorsinIcon.Oneofthemostfrequentlyusedoftheseis itoj whichgeneratestheintegersfromitoj.Thisgeneratorcanbecombinedwithevery-dotoformulatethetraditionalfor-stylecontrolstructure: everyk:=itojdo f(k) Notethatthisexpressioncanbewrittenmorecompactlyas everyf(itoj) Thereareseveralothercontrolstructuresrelatedtogeneration.Oneisalternation, expr1|expr2 whichgeneratestheresultsofexpr1followedbytheresultsofexpr2.Thus everywrite(find(\"or\",sentence1)| find(\"or\",sentence2)) :)(dniffotnemugradnocesehtninoitanretlagnisuybyltcapmoceromnettirwebnacecnetnessiht,niagA.2ecnetnesni"\ro"\fosnoitisopehtybdewollof1ecnetnesni"\ro"\fosnoitisopehtsetirw wt.poo到請,節章新最書本看 ))2ecnetnes|1ecnetnes,"\ro"\(dnif(etirwyreve ybdetartsullisinoitanretlafoesurehtonA )1|0(=)k|j|i( .1ro0eulavehtsahkro,j,ifoynafisdeeccushcihw ,elpmaxeroF.eriotreperni-tliubs'nocIotsrotarenegddaotdesuebnacserudecorP )2s,1s(ddodniferudecorp od)2s,1s(dnif=:iyreve idnepsusneht1=2%ifi dne ulavrehtonarofdemuserebnactitahtosnoisnepsusnitisevaeltub,erudecorpehtmorfeulavasnrutererutcurtslortnocdnepsusehT.2snisrucco1shcihwtasnoitisopdeulav-ddoehtsetarenegtahterudecorpasi