[英]What does it mean “Not in scope: ‘trace” and how can i resolve this?
I'm trying to run a program but i get always this in the terminal fib3.hs:3:12: Not in scope: 'trace' and i don't know what does it mean or how can i resolve this. 我正在尝试运行程序,但是我总是在终端fib3.hs:3:12中得到它:不在范围内:'trace',我不知道它是什么意思,或者我怎么解决这个问题。 I'm a ver beginner and haskell... my code
我是Ver的初学者,并且Haskell ...我的代码
-- fib3.hs
fib :: Integer -> Integer -> Integer
fib d n | trace (shift d ++ "Call: fib " ++ show n)
False = 0
fib _ 0 = 0
fib _ 1 = 1
fib d n = fib (d+1) (n-1)
+ fib (d+1) (n-2)
shift :: Integer -> String
shift 0 = ""
shift n = " " ++ shift (n-1)
The error is telling you that Haskell cannot find the function trace
- the problem is, that it's not in the prelude but in the Debug.Trace
module so you have to import this. 该错误告诉您Haskell无法找到函数
trace
-问题是它不在序幕中,而是在Debug.Trace
模块中,因此您必须导入它。
The easiest way to do this would be to add 最简单的方法是添加
import Debug.Trace
below your module
definition (if you have one - if not just at the top of the file but blow {-# LANGUAGE ... #-}
pragmas (if there are any) 低于
module
定义(如果有的话-如果不仅位于文件顶部,还打击{-# LANGUAGE ... #-}
编译指示(如果有的话)
This will import everything from Debug.Trace
which I personally don't like (it's hard to find out where a function is defined this way) - that's why I usually prefer to import only the needed functions and definitions (so I can tell where they are coming from) like this: 这将从我个人不喜欢的
Debug.Trace
中导入所有内容 (很难找到以这种方式定义函数的位置)-这就是为什么我通常更喜欢仅导入所需的函数和定义(因此我可以告诉它们在哪里)来自)是这样的:
import Debug.Trace (trace)
So a solution to your problem could look like this: 因此,解决您的问题的方法可能如下所示:
module MyFibModule where
import Debug.Trace (trace)
-- fib3.hs
fib :: Integer -> Integer -> Integer
fib d n | trace (shift d ++ "Call: fib " ++ show n)
False = 0
fib _ 0 = 0
fib _ 1 = 1
fib d n = fib (d+1) (n-1)
+ fib (d+1) (n-2)
shift :: Integer -> String
shift 0 = ""
shift n = " " ++ shift (n-1)
...
voila : 瞧 :
λ> fib 1 4
Call: fib 4
Call: fib 2
Call: fib 0
Call: fib 1
Call: fib 3
Call: fib 1
Call: fib 2
Call: fib 0
Call: fib 1
3
The rest is off topic but it might be interesting to you: 其余的不在主题范围内,但您可能会感兴趣:
I would rewrite this into: 我将其重写为:
fib :: Integer -> Integer -> Integer
fib d 0 = traceIt d 0 $ 0
fib d 1 = traceIt d 1 $ 1
fib d n = traceIt d n $ fib (d+1) (n-1) + fib (d+1) (n-2)
traceIt :: Integer -> Integer -> a -> a
traceIt d n = trace (shift d ++ "Call: fib " ++ show n)
shift :: Integer -> String
shift 0 = ""
shift n = " " ++ shift (n-1)
the guard - trick there seems to be too much magic IMO (I obviously totally missed the point at first :( ... so you decide: am I to stupid or is the code to arcane to read?) 警卫 - 骗局似乎有太多神奇的 IMO(我一开始显然很想念这件事:(...所以您决定:我是愚蠢的还是奥秘的代码?)
In case you don't like that you have to prepend the traceIt dn
everywhere (which is nasty yes) you can make fix the recursion with the trace
: 如果您不喜欢,则必须在
traceIt dn
地方都添加traceIt dn
(这是很讨厌的),您可以使用trace
修复递归:
fib :: Integer -> Integer
fib = fib' 0
fib' :: Integer -> Integer -> Integer
fib' d n = traceIt d n $ fibF (fib' (d+1)) n
fibF :: (Integer -> Integer) -> Integer -> Integer
fibF _ 0 = 0
fibF _ 1 = 1
fibF f n = f (n-1) + f (n-2)
traceIt :: Integer -> Integer -> a -> a
traceIt d n = trace (shift d ++ "Call: fib " ++ show n)
λ> fib 4
Call: fib 4
Call: fib 2
Call: fib 0
Call: fib 1
Call: fib 3
Call: fib 1
Call: fib 2
Call: fib 0
Call: fib 1
3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.