gpt4 book ai didi

debugging - 将 where 子句纳入 GHCi 调试器的范围

转载 作者:行者123 更新时间:2023-12-03 08:07:12 28 4
gpt4 key购买 nike

今天早些时候,我试图调试以下版本 solve给我带来问题的功能:

newtype Audience = Audience { byShyness :: [Int] }

solve :: Audience -> Int
solve (Audience originalCounts) = numFriendsAdded
where numFriendsAdded = length $ filter id friendAdded
friendAdded = zipWith3 (\i c t -> i >= c + t) [0..] originalCounts alreadyStanding
alreadyStanding = scanl (+) 0 modifiedCounts
modifiedCounts = zipWith (\a c -> if a then 1 else c) friendAdded originalCounts

在 GHCi (7.8.2) 中,我试图打破 solve按名称,然后按行/列,但似乎没有将名称绑定(bind)到 where子句纳入范围:
λ :b solve 
Breakpoint 0 activated at StandingOvation.hs:(20,1)-(24,89)
λ :main StandingOvation.example-input
Case #1: Stopped at StandingOvation.hs:(20,1)-(24,89)
_result :: Int = _
λ numFriendsAdded
<interactive>:5:1: Not in scope: ‘numFriendsAdded’
λ :delete 0
λ :b 20 35
Breakpoint 1 activated at StandingOvation.hs:20:35-49
λ :main StandingOvation.example-input
Case #1: Stopped at StandingOvation.hs:20:35-49
_result :: Int = _
numFriendsAdded :: Int = _
λ numFriendsAdded
0
λ friendAdded
<interactive>:10:1: Not in scope: ‘friendAdded’

显然,就 Haskell 而言,它们是相互影响的,但是在调试时我需要做什么才能使它们可见?

最佳答案

不幸的是,GHCi 调试器不能在断点处使范围内的所有内容都可用。报价 from the user manual (文本在 7.8.2 和 7.10.1(最新)中相同):

GHCi has provided bindings for the free variables[6] of the expression on which the breakpoint was placed (a, left, right), and additionally a binding for the result of the expression (_result). [...]



和脚注:

[6] We originally provided bindings for all variables in scope, rather than just the free variables of the expression, but found that this affected performance considerably, hence the current restriction to just the free variables.



本质上,只有在 GHCi 当前停止的表达式中直接提及局部变量时,您才能看到它们。这让我想到了一个解决方法,虽然很傻确实有效。将函数的主线替换为:
solve (Audience originalCounts) =
(friendAdded,alreadyStanding,modifiedCounts) `seq` numFriendsAdded

现在,表达式中提到了您感兴趣的所有变量,因此您可以停下来查看所有变量。

关于debugging - 将 where 子句纳入 GHCi 调试器的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29585756/

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