Haskell babban yaren shirye-shirye ne kawai, wanda ya shahara saboda sauฦaฦansa da kyawunsa.. Madaidaicin ษakin karatu yana ba da ษimbin ayyuka da nau'ikan bayanai, gami da jeri, waษanda ke da mahimmanci ga yawancin ayyukan shirye-shirye a Haskell. Matsala ษaya da aka saba fuskanta a cikin shirye-shiryen Haskell ta haษa da samun dama ga shugaban lissafi, watau kashi na farko a lissafin. Wannan aiki da alama mai sauฦi zai iya haifar da kurakurai na lokacin aiki idan ba a kula da su daidai ba. A cikin wannan cikakkiyar jagorar, za mu zurfafa cikin matsalar samun damar kan lissafin, mu bayyana ingantacciyar hanyar warwarewa, sannan mu ษauke ku ta hanyar lambar da ke da alaฦa, mataki-mataki.
Matsalar da muke son warwarewa ita ce yadda za mu sami shugaban lissafin a Haskell lafiya. Wannan batu ya taso ne saboda daidaitaccen aikin kai na Haskell yana da ban sha'awa, wanda ke nufin ba a bayyana shi ga kowane shigarwar da zai yiwu ba. Musamman, yana kasawa akan lissafin fanko. Manufarmu ita ce ฦirฦirar aikin da yake gabaษaya, ma'ana yana ษaukar kowane shigarwar da zai yiwu, gami da lissafin fanko.
safeHead :: [a] -> Maybe a safeHead [] = Nothing safeHead (x:xs) = Just x
Wannan aikin safeHead ba zai dawo da komai ba idan lissafin fanko ne, kuma kawai x (tare da x shine abu na farko) idan lissafin ba komai bane. Anan, Watakila a nau'i ne da ke wakiltar ฦimar zaษi: kowane Watakila ฦima ba komai ba ce ko kuma tana ษauke da ฦimar nau'in a kawai.
Fahimtar Code: Mataki-mataki
Mun tsara aikin mu na safeHead don sarrafa duk abubuwan da za a iya shigar da su, bin ฦa'idodin Haskell da shirye-shirye masu aiki: sarrafa duk dama kai tsaye, guje wa illa, da haษaka iya karanta lambar.
Da farko, muna ayyana sa hannun nau'in aikin, safeHead :: [a] -> Wataฦila a. Wannan yana nufin aikin yana ษaukar jerin kowane nau'i ([a]) kuma yana dawo da nau'in Wataฦila. Yana da mahimmanci a tuna da tsarin nau'in ฦarfi na Haskell, inda kowane magana a cikin Haskell yana da nau'i wanda aka ฦaddara a lokacin tattarawa.
Ana aiwatar da aikin mu ta amfani da daidaitawar ฦirar, mahimmin fasalin Haskell.
safeHead [] = Nothing safeHead (x:xs) = Just x
Anan, ''[]' yayi daidai da lissafin fanko, don haka ba mu dawo da komai ba. '(x:xs)' yayi daidai da jerin marasa fanko tare da kai 'x' da wutsiya'xs' kuma mu dawo 'Just x'.
Tallafin Laburare da Sauran Ayyuka
Nau'in Wataฦila wani ษangare ne na daidaitaccen ษakin karatu na Haskell kuma yana ba masu shirye-shirye damar magance ฦimar da ba a bayyana ba ko kuma kula da lamuran irin namu inda aikin ฦila ba shi da ฦayyadaddun ฦimar dawowa ga duk abubuwan da za a iya samu. A halin yanzu, ana iya tsawaita aikin safeHead ta halitta don gudanar da ฦarin ayyukan sarrafa jeri cikin aminci.
Misali, ana iya bayyana aikin safeTail kamar haka:
safeTail :: [a] -> Maybe [a] safeTail [] = Nothing safeTail (x:xs) = Just xs
Kamar aikin mu na safeHead, safeTail ba zai dawo da komai ba don jerin fanko kuma kawai xs (jerin ya rage kashinsa na farko) in ba haka ba. Bayan mun rufe manufar samun damar jeri mai aminci a cikin Haskell, bari mu matsa kayan aiki kuma mu zurfafa cikin fagen salon salo inda zaษi da haษe-haษe za su iya zama kamar hadaddun da wadata kamar a cikin shirye-shirye masu aiki.