列表理解我想寫的東西是這樣的:如何可以有條件我的模式匹配的哈斯克爾
> [(i, j) | i <- [1..10],
Just j <- [if (even i) then Just (i `div` 2) else Nothing]]
[(2,1),(4,2),(6,3),(8,4),(10,5)]
把條件列表,並使用<-
提取結果似乎特設。
我想:
> [(i, j) | i <- [1..10],
let Just j = if (even i) then Just (i `div` 2) else Nothing]
但是失敗了。
下面的作品,但似乎尷尬:
> [(i, j) | i <- [1..10],
let x = if (even i) then Just (i `div` 2) else Nothing,
isJust x,
let Just j = x]
[(2,1),(4,2),(6,3),(8,4),(10,5)]
有沒有做到這一點的優選方式。
我知道這個問題可以用其他方式解決,但我們假設我想在條件列表理解中模式匹配。 有沒有一個很好的方法來做到這一點?
謝謝。
從[我可以問什麼主題?](http://stackoverflow.com/help/on-topic):「...一個實用的*,可回答的問題,這是軟件開發所特有的」 。你有什麼實際的理由需要在列表理解中進行這種類型的模式匹配? – chepner
這裏你不需要模式匹配。 '[(我,div我2)|我< - [2,4..10]]'就足夠了。 – lovasoa
「將條件放在列表中,並使用'<-'來提取結果似乎是臨時的」,如果不是'Just j < - [somethingThatReturnsMaybe i]'您寫了'Just j < - return $ somethingThatReturnsMaybe i'? – Cactus