A cikin duniyar kwamfuta mai ban sha'awa, ฦididdiga suna taka muhimmiyar rawa. Duk da sauฦin abin da zai fara zuwa a hankali a matsayin masu ฦididdigewa - na'urori don aiwatar da ayyukan ฦididdiga - suna iya zama wasu na'urori masu sarฦaฦฦiya da sarฦaฦฦiya. Musamman, haษakar ฦididdiga ya ฦunshi la'akari da hankali na algorithms, ฦirar mai amfani da haษaka aiki. ฦaya daga cikin irin wannan misalin shine a cikin harshen shirye-shirye na Haskell, inda ake amfani da dabarun shirye-shirye na aiki don magance matsalolin lissafi da kyau.
A cikin wannan labarin, za mu bincika yin ainihin ฦididdiga ta amfani da Haskell. Za mu ษata rikitattun shirye-shirye masu aiki, dalla dalla-dalla mahimman ษakunan karatu na Haskell da ayyukan da ke tattare da su, kuma za mu buษe hanyar zuwa mafita a cikin tsari-mataki-mataki.
Shirye-shiryen Aiki a Tsarin Kalkuleta
Shirye-shiryen aiki yana ษaukar hanya daban-daban ga shirye-shirye fiye da yarukan mahimmanci na gargajiya. Ma'anar shirye-shirye na aiki shine cewa shirye-shirye an gina su ta hanyar amfani da tsara ayyuka, an bambanta da shirye-shirye masu mahimmanci, wanda ya dogara sosai ga canje-canje a cikin yanayi. Haskell, a matsayin harshe mai aiki zalla, yana ฦarfafa masu shirye-shirye don aiwatar da tsarin a cikin salon shela da babban matakin. Ya dace sosai don ฦirฦirar ฦididdiga saboda goyan bayan sa don sarrafa lambobi masu rikitarwa, ฦididdigar lissafi, da manyan ayyuka.
Yi la'akari da aikin lissafi na ฦari. A cikin Haskell, mutum zai iya rubuta aiki don yin wannan aikin kamar haka:
addFunction :: Num a => a -> a -> a addFunction x y = x + y
Ana kiran layin farko a matsayin nau'in sa hannu. Yana ba da babban matakin tabbacin cewa ayyuka suna yin yadda aka yi niyya, kama mafi yawan kurakurai a lokacin tattarawa.
Haskell Library don Ayyukan Kalkuleta
ฦayan ฦarfin Haskell shine tarin tarin ษakunan karatu waษanda kayan aiki ne masu ฦarfi don kera ingantattun mafita da inganci. Don ฦirฦirar ฦididdiga mai sauฦi, fahimtar manyan ษakunan karatu biyu yana da mahimmanci: Gabatarwa da Rubutu. Karanta ษakunan karatu.
Prelude a Haskell shine babban ษakin karatu wanda ake shigo da shi cikin shirye-shiryenku na Haskell. Yana ba da tsarin ayyuka masu karimci don sarrafa lissafin, sarrafa haruffa, da aiki tare da nau'ikan asali kamar su lamba da lambobi masu iyo.
A gefe guda, ษakin karatu na Text.Read yana ba da aikin "readMaybe", wanda ke da amfani musamman wajen karanta bayanan mai amfani a cikin shirin ฦididdiga. Yana ba da amintaccen ayyuka na ษangarori don canza kirtani zuwa masu canjin Haskell.
Tare da waษannan ษakunan karatu, ana iya rubuta ainihin aikin ฦididdiga cikin sauฦi.
Gina Kalkuleta: Rushewar Code
Sauka zuwa ฦwanฦwasa tagulla, bari mu shiga cikin ฦaฦฦarfan ฦaฦฦarfan ฦididdige ฦididdiga na asali. Rubutun Haskell mai zuwa yana bawa mai amfani damar shigar da zaren aikin lissafi mai sauฦi, kuma ya mayar da sakamakon idan aikin yana aiki:
import Text.Read import Data.Maybe calculate :: String -> Maybe Float calculate exprString = case words exprString of [num1, "+", num2] -> liftA2 (+) (readMaybe num1) (readMaybe num2) [num1, "-", num2] -> liftA2 (-) (readMaybe num1) (readMaybe num2) [num1, "*", num2] -> liftA2 (*) (readMaybe num1) (readMaybe num2) [num1, "/", num2] -> liftA2 (/) (readMaybe num1) (readMaybe num2) _ -> Nothing
Rubutun yana farawa ta hanyar shigo da ษakin karatu na Text.Read (don shigarwar mai amfani) da kuma Data.Maybe ษakin karatu, wanda ke ba da nau'in "Wataฦila" wanda ake amfani da shi don nuna yiwuwar ฦididdiga marasa nasara.
Aikin lissafin yana canza kirtan shigarwa zuwa 'Wataฦila Float', ta amfani da madaidaicin tsari don gano aiki da aikin 'liftA2' daga daidaitaccen ษakin karatu na Prelude don amfani da aiki zuwa lambobi da aka tantance. Wannan aikin yana ฦaddamar da ainihin fasalulluka na kalkuleta kuma yana nuna kyawun shirye-shiryen aiki wajen samun mafita.
Fadada Ayyukan Kalkuleta
Za a iya faษaษa ainihin ฦididdiga da muka ฦirฦira tare da ฦarin fasalulluka kamar sarrafa baka, ayyukan ci-gaban lissafi, ko ma adana masu canji. ฦarin bincike a cikin Haskell da fakitin sa zai bayyana ษimbin hanyoyin da za a sa na'urar ฦira ta fi ฦwarewa, aiki da mai amfani.
Ta hanyar fahimtar ainihin ra'ayoyi da dakunan karatu a cikin Haskell da kuma matakin-mataki na ginin ฦididdiga mai sauฦi, mutum zai iya jin daษin kyawun shirye-shiryen aiki wajen magance matsalolin lissafi. Wannan duality na sauฦi da rikitarwa shine abin da ke sa haษakar ฦididdiga a Haskell ba kawai matsala mai ban sha'awa ba, har ma da tafiya mai dacewa.
Happy codeing da bincike!