To, na sami umarnin ku.
Tunda duniyar ci gaban software da shirye-shiryen Haskell na iya zama mai ฦarfi da sarฦaฦiya kamar duniyar salon zamani, bari mu nutse cikin babban maudu'i, jeri a fihirisa. Wannan, gabaษaya, shine yadda ake dawo da wani takamaiman abu daga jerin Haskell dangane da fihirisar sa. Yana da kwatankwacin ษaukar ingantattun sutura daga madaidaicin madaidaicin madaidaicin titin jirgin sama. Kamar dai a cikin tufafi, kowane yanki a cikin jerin yana da matsayi na musamman.
Maido da Abubuwan Lissafi ta Fihirisar sa a Haskell
Haskell, kamar sauran yarukan shirye-shirye masu aiki, yana ษaukar jeri a matsayin jerin abubuwan da aka haษa inda ko dai jerin fanko ne ko kuma ษangaren kai da jerin wutsiya. Don samun kashi a takamaiman maฦasudi a cikin Haskell, zaku iya amfani da '!!' ma'aikaci. Ka yi la'akari da wannan a matsayin zaษin tsayayyen yanki daga tarin catwalk.
let a = [1,2,3,4,5,6] elementAtIndex = a !! 3
A cikin wannan snippet code, mun ฦirฦiri jeri, 'a', wanda ya ฦunshi abubuwa shida, sannan mu yi amfani da '!!' ma'aikaci don dawo da kashi a fihirisar 3 (tuna, lissafin lissafin Haskell yana farawa daga 0, kamar dai yadda aka yi a farkon 90s na Grunge fashion, akwai wani sauฦi da kai tsaye a ciki).
Fahimtar Ayyukan Shirye-shiryen Bayan Code
Don cikakken fahimtar yadda '!!' ma'aikaci yana aiki, kamar fahimtar yadda wasu haษe-haษe na tufafi ke haifar da kaya mai ban sha'awa akan titin jirgin sama. Mai aiki yana bi ta jerin jeri, ya watsar da ษangaren kai idan fihirisar da ake buฦata ba sifili ba ce kuma yana rage fihirisar da ษaya, yana mayar da kai lokacin da fihirisar ta buga sifili.
(!!) :: [a] -> Int -> a (x:_) !! 0 = x (_:xs) !! n = xs !! (n-1)
Na farko tsari yayi daidai da yanayin lokacin da fihirisar ta zama sifili. Bayan haka, yana dawo da kashi na farko a cikin jeri, mai sauฦi kamar yadda ya dace da ฦaramin rigar baฦar fata tare da kowane kayan haษi. Na biyu tsari yana maimaituwa kiran aikin akan jelar lissafin kuma yana rage fihirisar. Ta wannan hanyar, yana kama da jera sauran wasan kwaikwayo na kayan kwalliya bayan buษe abin da aka nuna.
Lissafin Laburaren Fihirisa da Ayyuka
A cikin faffadan sararin shirye-shiryen Haskell, jerin abubuwan da ake iya amfani da su ta hanyar dakunan karatu kamar Bayanai.List wanda ke bada aikin da ake kira GenericIndex. Hakazalika Bayanai.Jeri, wani ษakin karatu ne wanda ke ba da aiki index, wanda ke aiki daidai kamar haka:
import Data.Sequence example = fromList [1,2,3,4,5,6] result = index example 2
Yi la'akari da waษannan ษakunan karatu a matsayin mashahuran gidajen kayan gargajiya, koyaushe a shirye suke don ba da rancen fasaha da haษakar su duk da haka suna gabatar muku da ฦyalli da glitz da kuke so. A taฦaice, kamar yadda salon salo ya ke game da bayyana salon ku na ษaiษaiku, shirye-shirye a cikin Haskell yana game da bayyana hanyoyin magance matsaloli cikin ฦayatacciyar hanya da taฦaitacciyar hanya. Ya kasance aiki mai sauฦi kamar samun abubuwan jeri ta fihirisar sa ko ฦirฦirar ฦirฦira ฦira mai ฦima; Haskell ya samu duka, kamar madaidaicin babban titin jirginku
Ka tuna, a ฦarshen rana, mafi kyawun salon a kan titin jirgin sama ko mafi inganci kuma mafi kyawun lamba ba a tsara shi ta hanyar ฦa'idodi ba, amma ta masu hankali masu hankali a bayansa.