以下代码命名 fib.hs
import Criterion.Main (defaultMain)
fibZ = 1:1:zipWith (+) fibZ (tail fibZ)
main = defaultMain [
bench "fibZ 10" $ \n -> fibZ (10+n-n)
]
Run Code Online (Sandbox Code Playgroud)
错误
fib.hs:45:10: Not in scope: `bench'
Run Code Online (Sandbox Code Playgroud)
怎么了?我从这里借了这个例子.
我一直在使用的最新cabal-install(0.13.3,来自darcs repo)很不错; 它会告诉您何时可以通过重新安装来破坏GHC安装.我想知道的是:假设一个人cabal install foo 会重新安装会破坏我的GHC.如果我cabal-dev install foo反而会怎么样?我能避免打破GHC吗?我真的可以foo在cabal-dev沙箱中使用该软件包,还是只是一个破碎的沙箱?
示例:yesod,GHC 7.4.1,cabal-dev 0.9由github源码构建,Cabal 1.14.0库.
似乎无法在Haskell中正确排列(缩进)此代码.得到错误:
parse error on input `<-'
Run Code Online (Sandbox Code Playgroud)
任何人都可以找到错误所在的位置:
evalListSplitAt n stratPref stratSuff [] = return []
evalListSplitAt n stratPref stratSuff xs = do ys` <- stratPref ys
zs` <- stratSuff zs
return (ys` ++ zs`)
where (ys,zs) = splitAt n xs
Run Code Online (Sandbox Code Playgroud)
干杯.
compiler-construction haskell functional-programming compiler-errors ghc
假设我有一个文件NecessaryModule.hs,它有以下内部:
module NecessaryModule where
addNumber1 :: Int -> Int -> Int
addNumber1 a b = a + b
addNumber2 :: Int -> Int -> Int
addNumber2 a b = a + b
Run Code Online (Sandbox Code Playgroud)
当我做 :
:load NecessaryModule
Run Code Online (Sandbox Code Playgroud)
addNumber1和addNumber2都在当前范围内可用.有没有办法隐藏函数addNumber2,以便它可用于同一模块中的其他函数但在我以上述方式加载模块时不加载?谢谢
[对nanothief的回应]
我按照以下方式尝试了你的建议,但它对我不起作用.我有一个名为test2.hs的文件如下:
--test2.hs
module Test2 (addNumber1) where
addNumber1 :: Int -> Int -> Int
addNumber1 a b = a + b
addNumber2 :: Int -> Int -> Int
addNumber2 a b = a + b
Run Code Online (Sandbox Code Playgroud)
但是当我这样做的时候
:load test2
Run Code Online (Sandbox Code Playgroud)
然后我可以调用addNumber1和addNumber2.我做错了什么吗?谢谢
您好我不明白这一点我自己,我也不能在网上找到一个例子
我试图用一个也许还是一个后卫,我发现的例子只有两个变量,当我编辑或下面的例子中有两个以上我得到继承人什么,我试图做一个错误
--maybe example
Fun :: Double -> Double -> Double -> Maybe Double
Fun a b c
| a >= -1.0 || a <= 1.0 || b >= -1.0 || b <=1.0 || c /=0 || c >= -1.0 = Nothing
| otherwise = Just max( ((c^ (-c)) + (a^(-c)-1.0) ^ (a+ (-1.0/a))) 0.0)
Run Code Online (Sandbox Code Playgroud)
与gchi的错误
The function `Just' is applied to two arguments,
but its type `a0 -> Maybe a0' has only one
Run Code Online (Sandbox Code Playgroud)
而hlint给出了
No suggestions
Run Code Online (Sandbox Code Playgroud)
试图使用警卫而我得到一个不同的错误
--guard example …Run Code Online (Sandbox Code Playgroud) 我想编写一个Haskell程序,它在GHCi不支持的平台上交互使用GADT(即mipsel上的GNU/Linux).问题是,可用于在GHC中定义GADT的构造,例如:
data Term a where
Lit :: Int -> Term Int
Pair :: Term a -> Term b -> Term (a,b)
...
Run Code Online (Sandbox Code Playgroud)
似乎没有在Hugs上工作.
为什么Prog A编译运行正常,而Prog B无法编译?谢谢
Prog A.
func :: String -> String
func a = a
mydofn a = do
x <- func a
return x
main = print "Finished"
Run Code Online (Sandbox Code Playgroud)
Prog B.
func :: Int -> Int
func a = a
mydofn a = do
x <- func a
return x
main = print "Finished"
Run Code Online (Sandbox Code Playgroud)
Prog B编译错误:
Couldn't match expected type `m0 t0' with actual type `Int'
In the return type of a call of `func'
In a stmt of a 'do' …Run Code Online (Sandbox Code Playgroud) 我试图在Mac OS 10.5.8上安装json-0.4.4软件包,并收到此错误:
$ sudo cabal install "json-0.4.4"
Building json-0.4.4...
[1 of 7] Compiling Text.JSON.Types ( Text/JSON/Types.hs,
dist/build/Text/JSON/Types.o )
[2 of 7] Compiling Text.JSON.Pretty ( Text/JSON/Pretty.hs,
dist/build/Text/JSON/Pretty.o )
[3 of 7] Compiling Text.JSON.ReadP ( Text/JSON/ReadP.hs,
dist/build/Text/JSON/ReadP.o )
Text/JSON/ReadP.hs:104:21:
Warning: A do-notation statement discarded a result of type b.
Suppress this warning by saying "_ <- n",
or by using the flag -fno-warn-unused-do-bind
[4 of 7] Compiling Text.JSON.Parsec ( Text/JSON/Parsec.hs,
dist/build/Text/JSON/Parsec.o )
ghc: memory allocation failed (requested 2097152 bytes)
cabal: Error: …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个非常简单的编辑器,如"ed".在这个程序中,我正在尝试使用映射来构建控件,该控件在要执行的操作中转换string-command.这是一段代码:
commands :: Map String ([Handle] -> IO ())
commands = fromAscList [
("o",\list -> print "Insert name of the file to be opened" >> getLine >>= \nomefile ->
openFile nomefile ReadWriteMode >>= \handle -> editor (handle:list)),
("i",\list -> case list of { [] -> print "No buffer open" ; handle:res -> write handle } >> editor list),
("q",\list -> if list == [] then return () else mapM_ hClose list >> return ())
]
editor :: [Handle] -> IO() …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Solaris上编译最新版本的GHC(例如,7.4或7.6),使用6.12.1二进制文件进行编译.但是,我注意到GHC需要精确的GCC版本4.1.2来编译自己,以及许多其他要求.
为什么建立GHC的先决条件如此严格?Haskell是如此强大,我无法想象很多不能用纯Haskell编写的,那么为什么GHC需要一个C编译器来编译自己呢?有没有我只能用Haskell编译器编译的GHC版本?请注意,我不需要它来生成高度优化的代码,即使只是让GHCi工作也没问题.