let's use the monad to solve some problems. here is a famous question,
Say you have a chess board and only one knight piece on it. We want to find out if the knight can reach a certain position in three moves. We'll just use a pair of numbers to represent the knight's position on the chess board. The first number will determine the column he's in and the second number will determine the row.
let's make the type synonyms for the knight currrent position on the chess board:
type KnightPos = (Int,Int)
so let's make a method that can calculate the position after one move after the knight moves.
we can write this :
moveKnight :: KnightPos -> [KnightPos] moveKnight (c,r) = do (c',r') <- [(c+2,r-1),(c+2,r+1),(c-2,r-1),(c-2,r+1) ,(c+1,r-2),(c+1,r+2),(c-1,r-2),(c-1,r+2) ] guard (c' `elem` [1..8] && r' `elem` [1..8]) return (c',r')
this can also be written as this, if you writre with "filter"
moveKnight :: KnightPos -> [KnightPos] moveKnight (c,r) = filter onBoard [(c+2,r-1),(c+2,r+1),(c-2,r-1),(c-2,r+1) ,(c+1,r-2),(c+1,r+2),(c-1,r-2),(c-1,r+2) ] where onBoard (c,r) = c `elem` [1..8] && r `elem` [1..8]
with that , we can do in3 which get the all the possible location as such :
in3 :: KnightPos -> [KnightPos] in3 start = do first <- moveKnight start second <- moveKnight first moveKnight second
and then you can chain that by cropping up in several times, the above code can be wite as this withou tthe do notation.
in3 start = return start >>= moveKnight >>= moveKnight >>= moveKnight
now, let's take a function takes two positions and tell if we can reach that is like this:
canReachIn3 :: KnightPos -> KnightPos -> Bool canReachIn3 start end = end `elem` in3 start
now, let's test it .
ghci> (6,2) `canReachIn3` (7,3) False
相关推荐
haskell-for-typescript-devs:入门材料:TypeScript开发人员的Haskell
Haskell Monads小书 :warning: 初稿! –请仔细阅读 1. do块 2.解释嵌套的Lambda和绑定 =<<作为序列运算符 2C 。 a- a -> M b背后的直觉 2D合成do块 2E 。 嵌套do块 3.实现Monad和封装 3A 。 新型和副...
haskell-book-readers-exercises:Haskell图书读者的练习
Haskell-Lens-Tutorial-Library:镜头库缺少的教程模块
haskell-cpu-instruction-counter:使用Linux性能计数器在Haskell中测量CPU指令
haskell-do::pencil2:-Haskell代码编辑器专注于交互式开发
yesod-haskell-json-api-starter:使用Postgres的Yesod JSON API模板
web-haskell-graphql-postgres-boilerplate:Haskell中的现代Web服务器:Graphql + Postgresql +身份验证+数据库迁移+ Dotenv等
Atom-atom-haskell-pointfree.zip,atom包:将选择转换为无点或有点表示Haskell无点包,atom是一个用web技术构建的开源文本编辑器。
aws-lambda-haskell-runtime::high_voltage:AWSLambda的Haskell运行时
用于 haskell-relational-record 的 MySQL 驱动程序 这个项目被合并到 。 准备 $ git clone git@github.com:khibino/haskell-relational-record.git $ git clone git@github.com:bos/hdbc-mysql.git $ git clone ...
指示: $ git clone https://github.com/haskell-servant/haskell-servant.github.io.git$ cd haskell-servant.github.io/$ git checkout hakyll$ cabal sandbox init && cabal install --dep# go for a walk$ cabal...
haskell-scotty-realworld-example-app:使用HaskellScotty https:realworld.io构建的示例性后端API
haskell-jp官方网页 服务方式 $ stack install wai-app-static $ stack exec warp -- --host 0.0.0.0 --docroot . 这只是示例方式。 但对于Haskellers来说,这可能是最简单的!
从1.0.0开始,haskell-ghc-mod提供haskell-completion-backend服务。 注意:在1.0.0之前,提供了ide-backend服务。 它已被废弃以支持ide-haskell的UPI。 您可以在找到描述 执照 版权所有:copyright:2015 Atom-...
haskell-elm-todo-app 2019年9月24日更新:支持GHC 8.6.5和Elm 0.19 Todo应用在服务器端使用 ( , )构建,在客户端使用构建。 随机笔记(2016) Elm应用程序的模块化结构基于的文章“”。 它还使用的elm-return...
这是用Haskell编写的用于解决SAT的经典DPLL算法的简单实现它使用Happy解析器来解析公式。 要运行它只需 cabal install alexcabal install happycabal configurecabal run < cnf
haskell-neo4j-rest-client Haskell neo4j REST客户端 有关更多文档: : 有关更多用法示例,请查看文件tests / IntegrationTests.hs中的集成测试。
Haskell型Kong教程学习如何在Haskell中使用带类型的Kong进行编程的练习。... git clone https://github.com/chrisbarrett/haskell-typed-holes-tutorialcd haskell-typed-holes-tutorialstack build
向你学习 Haskell-for-good-good 我的工作通过 Learn You a Haskell for Great Good! 完全的: 介绍 出发 类型和类型类 函数中的语法 递归 高阶函数 模块 制作我们自己的类型和类型类 输入和输出 函数式解决问题...