An warware: jeri a fihirisa

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.

Shafi posts:

Leave a Comment