2016-09-09 69 views
7

我有一個代碼:哈斯克爾:變量不在範圍內

main = interact $ show . maxsubseq . map read . words 

maxsubseq :: (Ord a,Num a) => [a] -> (a,[a]) 
maxsubseq = snd . foldl f ((0,[]),(0,[])) where 
f ((h1,h2),sofar) x = (a,b) where 
a = max (0,[]) (h1 + x ,h2 ++ [x]) 
b = max sofar a 

但我得到的錯誤:

maxSub.hs:6:17: error: Variable not in scope: h1 

maxSub.hs:6:22: error: Variable not in scope: x 

maxSub.hs:6:25: error: Variable not in scope: h2 :: [t1] 

maxSub.hs:6:32: error: Variable not in scope: x 

maxSub.hs:7:9: error: Variable not in scope: sofar :: (t, [t1]) 

無法弄清楚,爲什麼?

任何想法??

感謝。

回答

12
main = interact $ show . maxsubseq . map read . words 

maxsubseq :: (Ord a,Num a) => [a] -> (a,[a]) 
maxsubseq = snd . foldl f ((0,[]),(0,[])) where 
f ((h1,h2),sofar) x = (a,b) where 
    a = max (0,[]) (h1 + x ,h2 ++ [x]) 
    b = max sofar a 

格式在Haskell真正的問題...

也許這看起來更好:

main = interact $ show . maxsubseq . map read . words 

maxsubseq :: (Ord a,Num a) => [a] -> (a,[a]) 
maxsubseq = snd . foldl f ((0,[]),(0,[])) where 
    f ((h1,h2),sofar) x = (a,b) 
     where { 
     a = max (0,[]) (h1 + x ,h2 ++ [x]); 
     b = max sofar a; 
       } 
+0

我感覺像一個小白現在。非常感謝。有用!! –