2
import Data.List
genkstrings :: Int -> [String] -> [String]
genkstrings k [] = []
genkstrings 1 (s:ss) = [ [c] | c <- s ] ++ genkstrings 1 ss
genkstrings k (s:ss)
| length (s:ss) < k = []
| otherwise = concat [kStartWith k c ss | c <- s ]
++
genkstrings k ss
kStartWith k c ss =
map (c :) $ genkstringsNogap (k-1) ss
genkstringsNogap 0 _ = []
genkstringsNogap 1 (s:ss) = [ [c] | c <- s ]
genkstringsNogap k (s:ss) = concat $ [kStartWithNoGap k c ss | c <- s ]
kStartWithNoGap k c ss = map (c:) (genkstringsNogap (k-1) ss)
輸入:genkstrings 2 ["sds","ghghg"]
哈斯克爾並行編程
輸出:
["sg","sh","sg","sh","sg","dg","dh","dg","dh","dg","sg","sh","sg","sh","sg"]
我學習Haskell,我發現我可以運行我的代碼並行。我在書中發現了一些我正在閱讀的示例,但我不明白我該如何應用並行編程。
,如果我是正確的,我應該把它在這條線
| otherwise = concat [kStartWith k c ss | c <- s ]
++
genkstrings k ss
我該怎麼辦呢?
我懷疑你可以通過並行化這個功能贏得很多。這裏沒有太多有趣的計算,性能受到所有這些列表的高速緩存性能的限制。至少切換到['Text'](http://hackage.haskell.org/package/text)或['Bytestring'](http://hackage.haskell.org/package/bytestring),然後再考慮更特殊的最佳化。 – leftaroundabout