gpt4 book ai didi

Haskell:变量不在范围内

转载 作者:行者123 更新时间:2023-12-03 15:18:11 24 4
gpt4 key购买 nike

我有一个代码:

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

无法弄清楚为什么?

有任何想法吗??

谢谢。

最佳答案

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;
}

关于Haskell:变量不在范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39403798/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com