An warware: shugaban lissafin

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.

Shafi posts:

Leave a Comment