An warware: cire kwafi a jere

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.

Shafi posts:

Leave a Comment