1 下载stack
安装Haskell开发环境首先要安装管理工具stack
,在官网找到并下载,也可以直接下载下面的链接:
下载Haskell文件
然后执行安装,如果安装过程中出现“add to PATH”的选项,一定要勾选。
2 修改配置
安装stack之后要修改它的配置,配置文件路径一般是:C:\sr\config.yaml
。
用下面的文本替换原来的文本。
# This file contains default non-project-specific settings for 'stack', used
# in all projects. For more information about stack's configuration, see
# http://docs.haskellstack.org/en/stable/yaml_configuration/
# The following parameters are used by "stack new" to automatically fill fields
# in the cabal config. We recommend uncommenting them and filling them out if
# you intend to use 'stack new'.
# See https://docs.haskellstack.org/en/stable/yaml_configuration/#templates
templates:
params:
author-name: Your Name
author-email: youremail@example.com
category: Your Projects Category
copyright: 'Copyright (c) 2022 Your Name'
github-username: yourusername
# The following parameter specifies stack's output styles; STYLES is a
# colon-delimited sequence of key=value, where 'key' is a style name and
# 'value' is a semicolon-delimited list of 'ANSI' SGR (Select Graphic
# Rendition) control codes (in decimal). Use "stack ls stack-colors --basic"
# to see the current sequence.
# stack-colors: STYLES
setup-info-locations: ["http://mirrors.tuna.tsinghua.edu.cn/stackage/stack-setup.yaml"]
urls:
latest-snapshot: http://mirrors.tuna.tsinghua.edu.cn/stackage/snapshots.json
snapshot-location-base: https://mirrors.tuna.tsinghua.edu.cn/stackage/stackage-snapshots/
package-indices:
- download-prefix: http://mirrors.tuna.tsinghua.edu.cn/hackage/
hackage-security:
keyids:
- 0a5c7ea47cd1b15f01f5f51a33adda7e655bc0f0b0615baa8e271f4c3351e21d
- 1ea9ba32c526d1cc91ab5e5bd364ec5e9e8cb67179a471872f6e26f0ae773d42
- 280b10153a522681163658cb49f632cde3f38d768b736ddbc901d99a1a772833
- 2a96b1889dc221c17296fcc2bb34b908ca9734376f0f361660200935916ef201
- 2c6c3627bd6c982990239487f1abd02e08a02e6cf16edb105a8012d444d870c3
- 51f0161b906011b52c6613376b1ae937670da69322113a246a09f807c62f6921
- 772e9f4c7db33d251d5c6e357199c819e569d130857dc225549b40845ff0890d
- aa315286e6ad281ad61182235533c41e806e5a787e0b6d1e7eef3f09d137d2e9
- fe331502606802feac15e514d9b9ea83fee8b6ffef71335479a2e68d84adc6b0
key-threshold: 3 # number of keys required
# ignore expiration date, see https://github.com/commercialhaskell/stack/pull/4614
ignore-expiry: no
3 安装GHC
命令行输入:
stack setup
安装完成出现如下提示,即表示安装成功。
They will never be overwritten nor automatically updated.
'./.bashrc' -> '/home/w/.bashrc'
'./.bash_logout' -> '/home/w/.bash_logout'
'./.bash_profile' -> '/home/w/.bash_profile'
'./.inputrc' -> '/home/w/.inputrc'
'./.profile' -> '/home/w/.profile'
For more information on paths, see 'stack path' and 'st
ack exec env'
To use this GHC and packages outside of a project, cons
ider using:
stack ghc, stack ghci, stack runghc, or stack exec
4 stack命令
如果只使用交互式解释器的话,可以在命令行输入stack ghci
进入交互式解释器中。
全局命令
使用GHC stack ghc
交互式解释器 stack ghci
项目命令:
新建项目 stack new helloworld new-template
配置项目 stack init
编译项目 stack build
执行项目 stack exec helloworld-exe
调试项目 stack test
5 项目Hello World
5.1 创建项目
选择一个文件夹,打开命令行输入:
stack new helloWorld
将会下载模板,并提示
Downloading template "new-template" to create project "helloWorld" in helloWorld\ ...
Looking for .cabal or package.yaml files to use to init the project.
Using cabal packages:
- helloWorld\
Selecting the best among 21 snapshots...
* Matches https://mirrors.tuna.tsinghua.edu.cn/stackage/stackage-snapshots/lts/19/1.yaml
Selected resolver: https://mirrors.tuna.tsinghua.edu.cn/stackage/stackage-snapshots/lts/19/1.yaml
Initialising configuration using resolver: https://mirrors.tuna.tsinghua.edu.cn/stackage/stackage-snapshots/lts/19/1.yaml
Total number of user packages considered: 1
Writing configuration to file: helloWorld\stack.yaml
All done.
5.2 目录结构
进入项目文件夹
cd helloWorld
查看文件树如下
tree /F
E:.
│ .gitignore
│ ChangeLog.md
│ helloWorld.cabal
│ LICENSE
│ package.yaml
│ README.md
│ Setup.hs
│ stack.yaml
│
├─app
│ Main.hs
│
├─src
│ Lib.hs
│
└─test
Spec.hs
这里需要进行修改的主要是app/Main.hs
文件和src/
文件夹,其他文件在初学时可以忽略。
5.3 修改程序
这个时候就需要用vs code 进行编辑了,打开项目文件夹,可以看到在main
函数引用了模块Lib
中的someFunc
函数。
可以将Main.hs
的代码修改如下:
module Main where
main :: IO ()
main = do
putStrLn "你好,欢迎来的Haskell世界!"
putStrLn "你的名字是?"
name<-getLine
putStrLn ("欢迎"++ name ++ "!")
5.4 编译程序
打开vs code中的命令行,输入:
stack build
执行编译。
编译成功后的exe文件会出现在文件夹.\.stack-work\install\8c80d480\bin
中。
5.5 执行程序
在命令行输入
stack exec helloWorld-exe
即可执行程序
输出如下:
你好,欢迎来的Haskell世界!
你的名字是?
李华
欢迎李华!