art*_*lla 4 haskell ghc ghci winghci
我加载了两个模块(NecessaryModule1.hs和NecessaryModule2.hs在Haskell中链接:在当前目录路径中加载所有文件).现在我要卸载NecessaryModule2.hs.我在System.Plugins.Load中找到了一个'unload'函数但是它在WinGHCi中不起作用.我得到的错误信息是:
>unload NecessaryModule2
<interactive>:1:1: Not in scope: `unload'
<interactive>:1:8:
Not in scope: data constructor `NecessaryModule2'
Run Code Online (Sandbox Code Playgroud)
我试过了
import System.Plugins.Load
Run Code Online (Sandbox Code Playgroud)
但那没用.有没有办法以上述方式卸载模块?
[对Riccardo的回应]
嗨Riccardo,我尝试了你的建议,但我无法让它在WinGHCi中工作.我有一个文件NecessaryModule1.hs如下:
module NecessaryModule1 where
addNumber1 :: Int -> Int -> Int
addNumber1 a b = a + b
Run Code Online (Sandbox Code Playgroud)
我通过':cd'命令转到文件的位置,然后执行:
> :module +NecessaryModule1
<no location info>:
Could not find module `NecessaryModule1':
it is not a module in the current program, or in any known package.
Run Code Online (Sandbox Code Playgroud)
它是否正确?谢谢[编辑:见下文更正]
[更正上述]
只是为了解释为什么上述不正确(如Riccardo所解释的),需要做的是以下内容:
如果我们有一个文件NecessaryModule1.hs如下:
--NecessaryModule1.hs
module NecessaryModule1 where
addNumber1 :: Int -> Int -> Int
addNumber1 a b = a + b
Run Code Online (Sandbox Code Playgroud)
那我们做:
> :load NecessaryModule1
[1 of 1] Compiling NecessaryModule1 ( NecessaryModule1.hs, interpreted )
Ok, modules loaded: NecessaryModule1.
> addNumber1 4 5
9
> :module -NecessaryModule1
> addNumber1 4 5
<interactive>:1:1: Not in scope: `addNumber1'
Run Code Online (Sandbox Code Playgroud)
Ric*_* T. 14
已安装的模块
您必须使用ghci的命令才能加载(:module +My.Module)和卸载(:module -My.Module)已安装的模块.您也可以使用:m而不是:module为了少写,如下所示:
Prelude> :m +Data.List
Prelude Data.List> sort [3,1,2]
[1,2,3]
Prelude Data.List> :m -Data.List
Prelude> sort [3,1,2]
<interactive>:1:1: Not in scope: `sort'
Run Code Online (Sandbox Code Playgroud)
请记住,ghci提示始终会提醒您当前导入的模块:您可以查看该模块以了解要卸载的内容 :m -Module.To.Unload.
具体文件
如果您尝试加载的模块未安装在系统中(例如,您编写了源代码并且只是将文件保存在某处),则需要使用其他命令:load filename.hs.更快捷的方法是将路径直接作为命令行参数传递给文件ghci,例如ghci filename.hs.如果您运行winghci并将其与.hs扩展名关联,只需双击该文件即可.
在这两种情况下,您将获得一个ghci提示符,指定的模块正确加载并在范围内导入(前提是您没有获得编译错误).和以前一样,您现在可以使用:m [+/-] My.Module加载和卸载模块,但请注意,这不同于:load因为:module假设您已经:load编辑了要进入/超出范围的内容.
例如,如果你有 test.hs
module MyModule where
import Data.List
f x = sort x
Run Code Online (Sandbox Code Playgroud)
您可以通过双击它(在带有winghci的窗口上),通过键入ghci test.hs控制台,或通过加载ghci和键入:load test.hs(小心相对/绝对路径)来加载它.
另一个有用的ghci命令是:reload,它将重新编译之前加载的模块.更改源文件时使用它,并且想要快速更新ghci中加载的模块.
Prelude> :load test.hs
[1 of 1] Compiling MyModule ( test.hs, interpreted )
Ok, modules loaded: MyModule.
*MyModule> let xs = [1,2,3] in sort xs == f xs
True
*MyModule> :reload
Ok, modules loaded: MyModule.
Run Code Online (Sandbox Code Playgroud)
:help 将为您提供所有可用命令的完整列表.