An warware: kalkuleta

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!

Shafi posts:

Leave a Comment