2015-12-03 58 views
4

目前我擺弄着關於2D ST數組和遞歸的haskell問題。Haskell:如何結合標量和monadic值?

由於二維位置和方向的陣列,我寫了一行返回所有產生的陣內點的列表:

let cellsAround = [resPt | dir <- directions, 
         let resPt = (fst dir + fst point, snd dir + snd point), 
         fst resPt >= 0 && fst resPt <= fst maxIdx && 
         snd resPt >= 0 && snd resPt <= snd maxIdx] 

現在的目標是豐富與內容生成的列表項陣列和我試過這一個:

cellsAround <- sequence [readArray board resPt | dir <- directions, 
         let resPt = (fst dir + fst point, snd dir + snd point), 
         fst resPt >= 0 && fst resPt <= fst maxIdx && 
         snd resPt >= 0 && snd resPt <= snd maxIdx] 

這也很好。但目標是獲得[(Point,Int)]的組合,因爲我必須過濾數組內容。

任何想法如何這種結合,說

(resPt, readArray board resPt) 

回答

2

變化最小:

sequence [(,) resPt <$> readArray board resPt 
     | ... 
+0

非常感謝。我沒有通過Hoogle找到這個(,):-( – Hennes

+0

@Hennes'(,)'是配對構造函數,'(,)a'是編寫'(\ b - >(a,b) )''。 – chi

+1

對你來說,我更喜歡在這裏使用'-XTupleSections',即'sequence [(resPt,)<$> readArray board resPt ...',這比原來的答案更加方便和可讀。 –

相關問題