"配置 haskell 的开发环境"
ghc 官方下载,然后安装,挺顺利的。 我用的是 osx 下的 ghc 8.0.2.
安装 emacs 下的 haskell-mode.el 也十分容易,按照 https://github.com/haskell/haskell-mode 的手册操作,也没有什么难度。
用 emacs 打开一个文件 hello_world.hs
,可以看到安装 haskell-mode 之后,emacs 能够用 haskell-mode 关联 hs
扩展名的文件。
module Main where
main :: IO ()
main =
putStrLn "hello world"
然后选择菜单,Haskell -> Start Interpreter
,然后选择,Haskell -> Load File
,这样进入一个 *haskell*
的 buffer 。运行指令
λ> :main
hello world
我们也可以手工加载这个程序,假设,我们的源文件叫做 Main.hs
,那么,我们可以使用 :load
指令加载这个文件。
λ> :load Main.hs
[1 of 1] Compiling Main ( Main.hs, interpreted )
Ok, modules loaded: Main.
Collecting type info for 1 module(s) ...
λ> main
hello world
Haskell 寻找模块的方式和 Java 类似,模块名称必须大写字母开头,模块名称和源文件名称一致。
-- Foo.hs
module Foo where
foo x y = x + y
运行这个程序
λ> :load Foo.hs
[1 of 1] Compiling Foo ( Foo.hs, interpreted )
Ok, modules loaded: Foo.
Collecting type info for 1 module(s) ...
λ> Foo.foo 1 2
3
修改 Foo.hs 如下
-- Foo.hs
module Foo where
foo x y = x + y
bar x y = x * y
我们可以用 :reload
指令重新加载模块。
λ> :reload
[1 of 1] Compiling Foo ( Foo.hs, interpreted )
Ok, modules loaded: Foo.
Collecting type info for 1 module(s) ...
λ> Foo.bar 2 4
8
每次都输入 Foo.
也挺麻烦的,我们可以用 :module
指令简化这个操作
λ> :module +Foo
λ> foo 2 3
5
λ> bar 3 3
9