目標是在 Haskell 中找到串列的最長遞增子序列 (LIS)。我嘗試運行以下代碼,但出現了找不到模塊的錯誤。我看到了 這個問題的答案,我知道訂購的包裹很舊,不再使用了。
import Data.Ord ( comparing )
import Data.List ( maximumBy, subsequences )
import Data.List.Ordered ( isSorted, nub )
lis :: Ord a => [a] -> [a]
lis = maximumBy (comparing length) . map nub . filter isSorted . subsequences
-- longest <-- unique <-- increasing <-- all
main = do
print $ lis [3,2,6,4,5,1]
print $ lis [0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15]
print $ lis [1,1,1,1]
因此,我嘗試僅使用:
import Data.List
但我收到以下錯誤:
main.hs:3:18: error:
Variable not in scope:
comparing :: (t0 a0 -> Int) -> [a] -> [a] -> Ordering
|
3 | lis = maximumBy (comparing length) . map nub . filter isSorted . subsequences
| ^^^^^^^^^
main.hs:3:56: error: Variable not in scope: isSorted :: [a] -> Bool
|
3 | lis = maximumBy (comparing length) . map nub . filter isSorted . subsequences
| ^^^^^^^^
exit status 1
uj5u.com熱心網友回復:
nub現在在Data.List. 如果一個isSorted函式在任何普通庫中可用,Hoogle 不會顯示它。您可以輕松地自己撰寫一個,盡管我沒有過多考慮以下建議是否是最有效的實作 - 它可能不適用于無限串列(我認為這兩個問題的答案都是no):
isSorted :: Ord a => [a] -> Bool
isSorted l = sort l == l
(使用排序來自Data.List。)
通過這些進口:
import Data.Ord (comparing)
import Data.List (maximumBy, subsequences, nub, sort)
該lis函式現在編譯。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/462826.html
上一篇:無法在Windows上安裝我的Hakyll博客(網路庫問題)
下一篇:此函式中的非詳盡模式
