Na farko, fahimtar matsalar yana da matukar muhimmanci. Kwafi a jere a cikin jeri yana faruwa ne lokacin da ma'aunin bayanai iri ษaya ya faru a wurare biyu ko fiye a cikin tsararru, amma sai idan waษannan kwafin sun faru a jere. Misali, a cikin jeri [1, 2, 2, 3, 2], ana kwafi ma'aunin bayanan '2', amma ba a la'akari da shi a matsayin kwafi a jere saboda baya cikin jere da '2' na farko. Koyaya, lissafin [1, 2, 2, 3] ya ฦunshi nau'i biyu na '2' a jere.
Don wannan fitowar, Haskell yana ba da sauฦi mai sauฦi, ingantaccen ingantaccen bayani tare da ginanniyar ayyukan ษakin karatu. Musamman, aikin 'ฦungiyar' daga ษakin karatu na 'Data.List` za a yi amfani da shi.
import Data.List (group) removeDuplicates :: Eq a => [a] -> [a] removeDuplicates = map head . group
Wannan gajeriyar gajeriyar lambar snippet tana kawar da kwafi a jere ta hanyar rarraba jerin asali zuwa jerin abubuwan da aka kwafin (rukuni), sannan a maye gurbin kowane jerin jerin da kashi na farko (kan taswira).
Fahimtar aikin cire Duplicates
Don ฦarin fahimtar yadda lambar ke aiki, bari mu karya shi.
Ana shigo da ษakin karatu mai mahimmanci: Lambar tana farawa da 'shigo da Data.List (ฦungiyar)'. Anan, muna shigo da aikin 'ฦungiyar' daga ษakin karatu na 'Data.List'. 'ฦungiya' aiki ne mai matuฦar amfani a cikin Haskell wanda ke raba jeri zuwa jerin ฦananan abubuwa masu kama da juna.
ฦayyadaddun aikin: Sai mu matsa zuwa `cire Duplicates :: Eq a => [a] -> [a]`. Wannan shine nau'in sanarwar don aikinmu. Yana faษi kawai cewa aikinmu yana ษaukar jerin kowane nau'in bayanai 'a' (inda 'a' memba ne na ajin 'Eq', ko kuma ana iya bincika don daidaito), kuma zai dawo da jerin nau'in bayanai iri ษaya.
Cikakken Bayanin Matakai
Yanzu, mai da hankali kan ษangaren `removeDuplicates = shugaban taswira. group`, ita ce ma'anar aikinmu wanda ke kimanta kyawun fasalulluka na shirye-shiryen Haskell.
Haษin aiki: Kalmar tana amfani da '.' ma'aikaci wanda a cikin Haskell, yana nufin tsarin aiki. Ainihin muna ฦirฦirar sabon aiki ta hanyar haษa 'ฦungiyar' da 'madarar taswira'.
aikin 'group': Aikin 'rukuni' yana ษaukar jerin abubuwan da muka shigar kuma su rarraba shi cikin jerin ฦananan abubuwa inda aka haษa duk abubuwa iri ษaya masu kama da juna.
Aikin 'map head': Sa'an nan kuma 'map head' ya ci gaba kuma ya maye gurbin kowane ฦananan lissafin tare da kashi na farko na wannan ฦananan lissafin - yana cire duk kwafi a jere. Ana yin shi ta hanya mai inganci kuma ana iya karanta shi ma.
Wannan lambar tana misalta yadda za mu iya amfani da ingantaccen ayyukan laburare masu wadatar Haskell da fasalulluka na shirye-shirye don yin abubuwa da yawa da ษan kaษan.
Haskell Library da Ayyuka
Girman Haskell ya ta'allaka ne a cikin tallafin ษakin karatu da ayyukan ginannen ayyuka. Haskell's Data.List library wata taska ce ta ayyuka masu amfani don sarrafa jeri, 'kungiyar' tana ษaya daga cikinsu. Hakanan za'a iya amfani da wani aiki, 'nub', don cire duk kwafi ba tare da la'akari da matsayinsu ba.
ฦaukakar ษakunan karatu da ฦwaฦฦwaran bugawa, tsarin tsara shirye-shirye suna sa Haskell ya zama yare mai sassauฦa kuma abin dogaro don magance matsalolin duniya na gaske. Bayan haka, ฦayyadaddun ฦayyadaddun fahimta yana sanya kuskuren lamba da fahimta madaidaiciya.