5thenwrite(i)Herethefirstresultproducedbythegenerator,3,isassignedtoi,butthisvalueisnotgreaterthan5andthecomparisonoperationfails.Atthispoint,thegeneratorisresumeda"> 5thenwrite(i)Herethefirstresultproducedbythegenerator,3,isassignedtoi,butthisvalueisnotgreaterthan5andthecomparisonoperationfails.Atthispoint,thegeneratorisresumeda">
武兵 7933 字 5個月前

ICON語言

(3 / 7)
⚡ 登入後報錯可獲 3天VIP 免廣告——立即登入

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


✅ 付款功能已修復,現在可以正常購買了 — 支援信用卡 · Apple Pay · Google Pay · WebATM · ATM 轉帳
😤 廣告總在最入戲的時候跳出來?
月付 $5 USD,升級 VIP 後全站所有頁面廣告立即全關——
不是只有這本書,是整個 oop.tw 每一頁、每一章,從此一路讀到底不被打斷。 $5 USD ≈ 一杯珍奶的錢,換一整個月零廣告清爽閱讀 · 隨時可取消
✅ 全站廣告全關 ✅ 工口專區全本解鎖 ✅ 月卡 $5 USD · 季卡 $13 USD · 年卡 $45 USD
⭐ 登入 / 免費註冊後升級
加我 LINE 好友,分享好書不錯過
第一時間獲得新書推薦、書單更新通知
立即加入
上一頁 書頁/目錄
/ 7 頁
下一頁
為本書評分(每位讀者可評一次,提交後無法修改)
發表評論
以 匿名讀者 身份發表
讀者評論