簡體   English   中英

如何在此列表中正確迭代

[英]How can I iterate properly through this list

以下示例是問題的簡化。 我有一個列表[Either Foo Bar]和另一個列表[Biz] 我們的想法是,我每次迭代Biz元素通過[Either Foo Bar] ,從一開始[Either Foo Bar] ,直至Biz為空。 結果是[Either Foo Bar]現在會有更多的Bar和更少的Foo

當需要使用[Biz]的下一個元素時,問題是能夠在[Either Foo Bar]的開頭開始。

我可以發布一個我想要做的例子,如果有幫助的話。

更新:好的,這里是我正在使用的實際類型,仍然試圖忽略我認為可能是無關的信息。 如果我遺漏了重要的事情,請告訴我

[Either UnFlaggedDay CalendarDay] [(CalFlag,Product, Day)]

 data CalFlag = FirstPass | SecondPass | ThirdPass deriving (Enum,Eq,Show) 

我要做的是在[Either UnFlaggedDay CalendarDay]檢查DayLeft值。 當我得到一個匹配時,我想創建一個新的列表,除了以下更改之外完全相同:我將更改UnFlaggedDay ,以及列表中接下來的兩個UnflaggedDayCalendarDay . At that point, I want to use the newly built list, that has the same number of elements still, and the . At that point, I want to use the newly built list, that has the same number of elements still, and the [(CalFlag,Product,Day)] minus the剛剛檢查minus the (CalFlag,Product,Day)。 下面是我在這個問題的不同方法之間的一些破碎的代碼。

 flagReserved :: [Either UnFlaggedDay CalendarDay] -> Handler [Either UnFlaggedDay CalendarDay] flagReserved ((Left (MkUFD day)):rest) = do reserved <- runDB $ selectList [TestQueue ==. Scheduled_Q, TestStatus /<-. [Passed,Failed]] [] case (L.null reserved) of True -> do processedDays <- ((Left $ MkUFD day) :) <$> flagReserved rest return processedDays False -> return $ flagReserved' (map prepList ((Left (MkUFD day)):rest)) (flagProductTuple reserved) flagReserved ((Right (MkCal day)):rest) = do processedDays <- ((Right $ MkCal day):) <$> flagReserved rest return processedDays flagReserved _ = return [] flagReserved' :: [Either (UnFlaggedDay) CalendarDay] -> [(CalFlag,Product,Maybe C.Day)] -> [Either UnFlaggedDay CalendarDay] flagReserved' ((Left (MkUFD day)):restD) ((calFlag,firmware,Just startDate):restF) = case (startDate == day || not (calFlag == FirstPass)) of True | (calFlag == ThirdPass) -> flagReserved' ((Right $ conScheduled day firmware Reserved) : restD) restF | otherwise -> flagReserved (Right $ consScheduled day firmware Reserved) : flagReserved' restD ((succ calFlag, firmware, Just startDate) : restF) False -> (Left (MkUFD day)) : flagReserved' restD ((calFlag, firmware, Just startDate) : restF) flagReserved' ((Right (MkCal (Left (MkAD (dayText,day))))):restD) ((calFlag,firmware,Just startDate):restF) = case (startDate == day || not (calFlag == FirstPass)) of True | (calFlag == ThirdPass) -> (Right $ consScheduled day firmware Reserved) : flagReserved' restD restF | otherwise -> (Right $ consScheduled day firmware Reserved) : flagReserved' restD ((succ calFlag, firmware, Just startDate):restF) False -> (Right (MkCal (Left (MkAD (dayText,day))))) : flagReserved' restD ((calFlag,firmware,Just startDate) : restF) flagReserved' ((Right (MkCal (Right unAvailable))):restD) ((calFlag,firmware,startDate):restF) = (Right $ MkCal $ Right unAvailable) : flagReserved' restD ((calFlag,firmware,startDate) : restF) flagReserved' unprocessed [] = unprocessed flagReserved' [] _ = [] 

更新:

我已經制作了一些測試代碼來解決我的想法。 這是我到目前為止所擁有的

 let reservedDays = [(FirstPass,IM,C.fromGregorian 2012 01 15), (FirstPass,WAF,C.fromGregorian 2012 01 14), (FirstPass,Backup,C.fromGregorian 2012 01 13) ] dummyFunc :: [Either UnFlaggedDay CalendarDay] -> (CalFlag,Product,C.Day) dummyFunc dayList (cFlag,product,day) = if day `elem` dayList then dummyFunc' dayList (cFlag,product,day) else dayList dummyFunc' dayList (cFlag,product,day) = if (cFlag == ThirdPass) then 

好的,這是我被困的地方。 我需要能夠將接下來的三個Left值更改為Right值。 我對dummyFunc'意圖是將列表拆分為第一個Left值,刪除它,添加新的Right值,加入先前拆分的列表,然后再重復兩次。 有沒有更好的辦法? 如果沒有,是否已經有一個功能可以根據我提到的標准將列表分成兩半? 我可以弄清楚如何手動完成它,但我不是要重新發明輪子。

我認為[Biz]中的每個元素都可以調整[Either Foo Bar]中的一個或多個元素,使其遠離左( Foo )類型和右( Bar )類型。 這只是一個折疊:

eitherList = [Left (), Left (), Right 5, Right 9, Left ()]
bizList = [4,5,6,7,1]

func eitherlst biz = if (Left ()) `elem` eitherlst
                       then Right biz : delete (Left ()) eitherlst
                       else eitherlst

eitherList' = foldl func eitherList bizList 

以上是未經測試的,但您可以看到在每次調用func如何通過考慮原始的eitherList和所有Biz元素直到該點來傳遞更新的eitherList 正如您所看到的,您對func的實現將使其變得有用。

暫無
暫無

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

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