【问题标题】:Haskell: Variable not in scopeHaskell:变量不在范围内
【发布时间】:2016-09-09 04:17:58
【问题描述】:

我有一个代码:

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])

不知道为什么??

有什么想法吗??

谢谢。

【问题讨论】:

    标签: haskell


    【解决方案1】:
    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;
                  }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      • 2021-01-05
      • 2017-09-30
      • 1970-01-01
      • 2022-10-08
      • 2019-10-13
      相关资源
      最近更新 更多