簡體   English   中英

遞歸Haskell函數將一個列表分為一對列表,然后遍歷該對

[英]Recursive Haskell function that separates a list into a pair of lists, then goes through the pair

我有一個函數,將其稱為fct1 ,該函數可以接收任何列表,並在列表中獲得所有相等的內容,而在第二個列表中獲得所有其余內容,它們位於一個元組中。

data sale : ( sale string int) fct1 [sale,sale..sale]將僅返回在第一個列表上具有相同字符串而在第二個列表中具有所有其他字符串的銷售)

關於數字的ex(易於理解): fct1 [1,2,3,4,6,7,1,3,4]= ([1,1],[2,3,4,6,7,3,4])

我還有第二個函數,叫它fct2

fct2是對FST(元組)始終應用,我需要的結果累積成一個列表。 並且列表將是返回值

我需要應用fct2上FST(元組),則應用相同的fct1上SND(元組),所以它重新創建具有其余的元組,直到我達到fct1 = []

我知道我必須遞歸做,只是不知道從哪里開始... fct1fct2了。

這是我的偽代碼...

type Qty = Integer                                                        
type Product =string                                    
data Sales = Sales Product Qty`

as  is a list

fct1 = (fct1 (\(Sales product qty) -> product == product(head as)) as)
it return a tuple (list1,list2)

fct2:

fct1需要在snd(tuple)上運行,所以我以snd(tuple)的身份到達[],同時我需要在fst(tuple)fct2上運行,該fct2將列表匯總為一個元素需要保留並累積到一個列表中。

我希望這次我更加明確。

找到解決方案的地方是代碼:

 fct2 as = accSales [] as

   where accSales n as =

            if as == []

            then sortBy compareSale n

            else let x =sumQty(fst(fct1(as)))
                     xs = snd(fct1(as)) 
                 in  accSales (x:n) xs`
FOUND THE SOLUTION HERE IS THE CODE:

 fct2 as = accSales [] as

   where accSales n as =

            if as == []

            then sortBy compareSale n

            else let x =sumQty(fst(fct1(as)))
                     xs = snd(fct1(as)) 
                 in  accSales (x:n) xs`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM