PRACTICAL HASKELL - GETTING STARTED WITH STACK

Haskell初学者指南
本文为Haskell初学者提供了一套实用教程,包括安装配置、编写和运行代码的基础步骤,并介绍了核心工具如GHC、Cabal及Stack的作用。

Haskell is famous for having a steep learning curve. As a web developer we’re used to clear tutorials that we can understand and complete within an hour or two. Haskell introduces many new concepts not found in other languages, but we can learn it faster by spending as much time coding as we do reading.

This is the first of a tutorial series intended to introduce Haskell by coding things that work.

  1. Getting Started with Stack
  2. Importing Code
  3. Using Monads
  4. Build a JSON API

In this article we will show you how to get Haskell installed, how to set up a new project, and run your code.

Tools and Names

GHC is the compiler for Haskell. It takes Haskell source code and turns it into an executable.

Cabal is the package description format. You’ll have a file called my-project.cabal with information about your project. There’s an executable called cabal too, but we are going to use stack instead.

Stack is a package manager. It reads my-project.cabal and stack.yaml to link in third party code. It will install GHC for you too.

Installing Stack

The only thing you need to download is Stack. It will install everything else for you.

Then, follow the instructions on the download page for your operating system. Here’s what I did on a mac:

  1. Unzip the file by double clicking it
  2. Move it to /usr/local/bin

    $ mv stack-0.1.3.1-x86_64-osx /usr/local/bin/stack
    
  3. Give it executable permissions

    $ chmod +x /usr/local/bin/stack
    

Alternatively on OSX you can install via homebrew:

brew update
brew install haskell-stack

Check to make sure it is working

$ stack --version
Version 0.1.3.1, Git revision 908b04205e6f436d4a5f420b1c6c646ed2b804d7

Setting up a new project

We’re going to need a few files to get our first project going: some haskell source code, a stack.yaml and a .cabal file. We can create these files by hand, but stack has a template feature we can use instead. Let’s call our project “my-project” and use the simple template.

$ stack new my-project simple
...

This creates a directory named my-project. Let’s see what’s inside:

LICENSE
Setup.hs
stack.yaml
my-project.cabal
src/
  Main.hs

The stack.yaml config file tells stack which version of GHC and your dependencies to use.

flags: {}
packages:
- '.'
extra-deps: []
resolver: lts-3.1

We use the my-project.cabal config file to store settings like the project name, and license. In the next article, Importing Code, we’ll edit this file to add dependencies.

name:                my-project
version:             0.1.0.0
synopsis:            Simple project template from stack
description:         Please see README.md
homepage:            http://github.com/githubuser/my-project#readme
license:             BSD3
license-file:        LICENSE
author:              Sean Hess
maintainer:          seanhess@gmail.com
copyright:           2010 Author Here
category:            Web
build-type:          Simple
cabal-version:       >=1.10

executable my-project
  hs-source-dirs:      src
  main-is:             Main.hs
  default-language:    Haskell2010
  build-depends:       base >= 4.7 && < 5

Last but not least, we have some source code. src/Main.hs is the main module for our program. This is where the Haskell happens.

module Main where

main :: IO ()
main = do
  putStrLn "hello world"

Installing GHC

Stack will install the correct version of GHC for our project. This is cool because everyone working on your project will be on the same version. Let’s give it a shot: run this in your project folder

$ stack setup

Which results in:

Downloaded lts-3.1 build plan.    
Caching build plan
Fetched package index.
Populated index cache.
Downloaded ghc-7.10.2.
Installed GHC.
stack will use a locally installed GHC
For more information on paths, see 'stack path' and 'stack exec env'
To use this GHC and packages outside of a project, consider using:
stack ghc, stack ghci, stack runghc, or stack exec

Run the Code!

Now we’re ready to run some code! Let’s use stack to fire up GHCI: the Haskell REPL.

$ stack ghci
Configuring GHCi with the following packages: my-project
GHCi, version 7.10.2: http://www.haskell.org/ghc/  :? for help
Prelude> 

There are other great tutorials that will teach you how to use ghci, but here are a few examples:

Prelude> 2 + 15
17

Prelude> 5 == 5
True

We can Use the :load command to load our code. Note that you can tab-complete module names.

Prelude> :load Main
[1 of 1] Compiling Main             ( src/Main.hs, interpreted )
Ok, modules loaded: Main.

Then we can run our program by typing main

Main> main
hello world

Making Changes

We can use ghci to test our changes as we go. Let’s make a greet function! Add this to src/Main.hs

greet name = "Hello " ++ name ++ "!"

Now go back to ghci and type :reload or :r

Main> :r
[1 of 1] Compiling Main   (src/Main.hs, interpreted)
Ok, modules loaded: Main.

We can test greet without it being used in main

Main> greet "bobby"
"Hello bobby!"

Let’s add it to our main program! Edit src/Main.hs

main = do
  putStrLn (greet "bobby")
  putStrLn (greet "World")

Reload again with :r and then run main

Main> :r
Main> main
Hello bobby!
Hello World!

Building an Executable

When you are ready to ship, you can build an executable with stack build

$ stack build

It will tell you where the executable is, but it’s easier to run it with stack exec. It will run anything in your main function.

$ stack exec my-project
Hello bobby!
Hello World!

Other Resources

Assignment

  1. Read chapter 3 of Learn You a Haskell and add a type declaration to greet

  2. Use the getLine function to read a name from the command-line, and print out a greeting to that name. Will require using the Prelude Documentation, and probably some googling.

Answers

What’s Next

In the next article, Importing Code, we show you how to use other built-in modules and 3rd party code.


【无人机】基于改进粒子群算法的无人机路径规划研究[和遗传算法、粒子群算法进行比较](Matlab代码实现)内容概要:本文围绕基于改进粒子群算法的无人机路径规划展开研究,重点探讨了在复杂环境中利用改进粒子群算法(PSO)实现无人机三维路径规划的方法,并将其与遗传算法(GA)、标准粒子群算法等传统优化算法进行对比分析。研究内容涵盖路径规划的多目标优化、避障策略、航路点约束以及算法收敛性和寻优能力的评估,所有实验均通过Matlab代码实现,提供了完整的仿真验证流程。文章还提到了多种智能优化算法在无人机路径规划中的应用比较,突出了改进PSO在收敛速度和全局寻优方面的优势。; 适合人群:具备一定Matlab编程基础和优化算法知识的研究生、科研人员及从事无人机路径规划、智能优化算法研究的相关技术人员。; 使用场景及目标:①用于无人机在复杂地形或动态环境下的三维路径规划仿真研究;②比较不同智能优化算法(如PSO、GA、蚁群算法、RRT等)在路径规划中的性能差异;③为多目标优化问题提供算法选型和改进思路。; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注算法的参数设置、适应度函数设计及路径约束处理方式,同时可参考文中提到的多种算法对比思路,拓展到其他智能优化算法的研究与改进中。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值