`

haskell - Functionally solving problems - Heathrow to London

阅读更多

Ensuing to the discussion that we had on the functionally solving problems that we had before. Where we have Reverse Polish notation calculator, we have yet another problem to solve, which is called Heathrow to London

 

basically the problem is that there are certains means to take from Heathrow to Londo, they start at the initial locatoin at A lane, at node 1, and then it will progress either forward at the same lane, or take a route to another lane. and we know each route has certain weight, the solution is to find the most effective way to travel from Heathrow to London in the shortest path.

 

There is some general guide on how to solve problems with Haskell.

 

 

  • Forget Haskell for a minute and think about how we’d solve the problem by hand
  • Think about how we’re going to represent our data in Haskell
  • Fig Figure out how to operate on that data in Haskell so that we produce at a solution

 

First we will define some data to helps us tackle the problem. 

 

data Section = Section { getA :: Int, getB :: Int, getC :: Int } deriving (Show) 

type RoadSystem = [Section] 

we repsent a group called section, which has three route, one is to A, another to B, and the last is to C (which means goes another route to another lane). 

 

 before this solution, we might have come up some solution which may define data structure as below. 

 

data Node = Node Road Road | EndNode Road

data Road = Road Int Node

 

or even like this , if you prefer to use Maybe data.

 

data Node = Node Road ( Maybe Road )

data Road = Road Int Node

 

Since we have define the datascture to represent the path and the the RoadSystem, we might start to work out some solution, to aid us to do that ,we might define some data that enables us to track the current state and the path we will take along the way. 

 

here are the datas. 

data Label = A | B | C deriving ( Show )

type Path = [( Label , Int )]

 

with that, we may represent the solution by the following. 

 

[(B ,10) ,(C ,30) ,(A ,5) ,(C ,20) ,(B ,2) ,(B ,8)]

 

we might has some helper function, which keeps track the optimal path to A node at section N or the optimal path to B nodes at section N...  Here is the code that helps to find what it is . 

 

roadStep :: (Path, Path) -> Section -> (Path, Path) 

roadStep (pathA, pathB) (Section a b c) =  

    let priceA = sum $ map snd pathA 

        priceB = sum $ map snd pathB 

        forwardPriceToA = priceA + a 

        crossPriceToA = priceB + b + c 

        forwardPriceToB = priceB + b 

        crossPriceToB = priceA + a + c 

        newPathToA = if forwardPriceToA <= crossPriceToA 

                        then (A,a):pathA 

                        else (C,c):(B,b):pathB 

        newPathToB = if forwardPriceToB <= crossPriceToB 

                        then (B,b):pathB 

                        else (C,c):(A,a):pathA 

    in  (newPathToA, newPathToB)

 

with that, we can find the optimal solution by the following.  


<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->

optimalPath :: RoadSystem -> Path 

optimalPath roadSystem =

    let (bestAPath, bestBPath) = foldl roadStep ([],[]) roadSystem 

    in  if sum (map snd bestAPath) <= sum (map snd bestBPath) 

            then reverse bestAPath 

            else reverse bestBPath 

 

and we now need to parse the input and feed that into the optimalPath solution so that it can solve that partical problem.  we create a function called groupOf, which can split the list into groups of the same size.  here is the code that does it. 

 

groupsOf :: Int -> [a] -> [[a]] 

groupsOf 0 _ = undefined 

groupsOf _ [] = [] 

groupsOf n xs = take n xs : groupsOf n (drop n xs) 

 

and it is the main method which does the work .

main = do 

    contents <- getContents 

    let threes = groupsOf 3 (map read $ lines contents) 

        roadSystem = map (\[a,b,c] -> Section a b c) threes 

        path = optimalPath roadSystem 

        pathString = concat $ map (show . fst) path 

        pathPrice = sum $ map snd path 

    putStrLn $ "The best path to take is: " ++ pathString 

    putStrLn $ "The price is: " ++ show pathPrice 

 

 

 

 

分享到:
评论

相关推荐

    Atom-ide-haskell-hoogle,在光标下显示符号的滚动信息。对原子的贡献.zip

    Atom-ide-haskell-hoogle 是一个专门为 Atom 编辑器设计的插件,它整合了 Haskell 的 Hoogle 工具,以提供强大的代码提示和搜索功能。Atom 是一款由 GitHub 开发的开源文本编辑器,它利用现代 web 技术如 HTML、CSS ...

    haskell-mode emacs

    在 Emacs 中,`haskell-mode` 是一个专门为了提升 Haskell 开发体验而设计的模式。 `haskell-mode` 提供了多种增强功能,旨在帮助 Haskell 开发者更高效地编写、调试和理解代码。这个模式包括以下关键特性: 1. **...

    haskell-chart, haskell的2D 图表库.zip

    在数据可视化领域,`haskell-chart`库提供了一种高效且灵活的方式来创建2D图表,这对于数据分析、科学计算以及教学等场景非常有用。这个库是开源的,意味着任何人都可以查看其源代码,学习并贡献改进。 `haskell-...

    Atom-haskell-ghc-mod,哈斯克尔.zip

    **哈斯克尔编程语言与Atom-Haskell-GHC-Mod** 哈斯克尔(Haskell)是一种纯函数式编程语言,以其优雅的语法、强静态类型系统和编译时优化而受到程序员的喜爱。它鼓励使用不可变数据和惰性求值,这使得哈斯克尔在...

    Atom-haskell-debug,使用ghci在atom中实现图形haskell调试器.zip

    Atom-Haskell-Debug是针对Haskell开发者的一个强大工具,它允许你在流行的Atom文本编辑器中集成一个图形化的Haskell调试器。这个工具基于GHCi(Glasgow Haskell Compiler Interface),GHCi是Haskell的交互式环境,...

    Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码.zip

    Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码 Haskell-Data-Analysis-Cookbook这是 Haskell数据分析 cookbook的附带源代码。最新的源代码可以在GitHub上获得: ...

    haskell-ghc-mod:haskell-ghc-mod原子包

    从1.0.0开始,haskell-ghc-mod提供haskell-completion-backend服务。 注意:在1.0.0之前,提供了ide-backend服务。 它已被废弃以支持ide-haskell的UPI。 您可以在找到描述 执照 版权所有:copyright:2015 Atom-...

    haskell-ghc-mod:haskell-ghc-mod原子包

    haskell-ghc-mod原子包 该软件包主要用作后端。 Haskell ghc-mod打开通往ghc-modi的管道,并查询类型,信息并检查错误。 安装与配置 请参考官方文档站点 服务中心API 从1.0.0版本开始,haskell-ghc-mod提供...

    Get Programming with HASKELL-2018-英文版.pdf

    Get Programming with HASKELL-2018-英文版

    函数式编程-haskell-to-java

    ### 函数式编程:Haskell到Java的转换 #### 概述 本文旨在探讨函数式编程语言Haskell如何被编译或转换为Java语言。Haskell作为一种纯函数式编程语言,以其强大的类型系统、惰性求值机制以及高度抽象的能力在学术界...

    haskell-relational-record-driver-mysql:用于 haskell-relational-record 的 MySQL 驱动程序

    用于 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 ...

    Atom-atom-haskell-scry,扩散系数.zip

    【标题】:“Atom-atom-haskell-scry,扩散系数.zip” 涉及的主要知识点是 Atom 编辑器与 Haskell 语言的集成以及 SCRY 工具的使用。 【描述】:“Atom-atom-haskell-scry.zip”的描述指出,这个压缩包包含了一个名...

    Atom-ide-haskell-repl,原子中的ghci repl。对原子的贡献.zip

    Atom-ide-haskell-repl是针对Atom文本编辑器的一个扩展插件,专为Haskell编程语言提供集成的GHCi(Glasgow Haskell Compiler Interface)交互式环境,即REPL(Read-Eval-Print Loop)。这个插件允许开发者在Atom编辑...

    haskell-dev-tools:我用来安装升级与Haskell相关的开发工具的元项目

    在Haskell的世界里,开发环境的配置至关重要,而`haskell-dev-tools`就是一个方便的元项目,它专门设计用于简化Haskell开发工具的安装和升级过程。这个项目扮演了一个集合和自动化工具的角色,使得开发者无需手动...

    haskell-dap:Haskell DAP接口数据的实现

    Haskell-dap是一个开源项目,它实现了调试适应性协议(Debug Adapter Protocol,简称DAP)的接口,使得Haskell开发者可以充分利用这个协议进行程序调试。DAP是一个通用的、跨平台的协议,允许IDEs(集成开发环境)和...

    haskell-tools:Haskell的开发人员工具

    "haskell-tools"就是这样一个项目,它专注于为Haskell开发者提供一系列实用的辅助工具,以优化他们的开发流程。 ### 1. GHC:Glasgow Haskell Compiler GHC是Haskell的主要编译器,也是haskell-tools的重要组成...

    Atom-ide-haskell-cabal,IDE的Cabal后端提供商.zip

    Atom-ide-haskell-cabal.zip,Cabal backend provider for ide-haskellIDE Haskell Cabal套餐,atom是一个用web技术构建的开源文本编辑器。

    Haskell - The Craft of Functional Programming, 2ed (Addison-Wesley, 1999) by Tantanoid

    ### Haskell - The Craft of Functional Programming #### 一、概述 《Haskell - The Craft of Functional Programming》是一本深入探讨Haskell编程语言的经典著作。本书由Tantanoid编写,并于1999年由Addison-...

    Atom-ide-haskell-hie,用于hie的atom lsp插件(haskell ide引擎)。为Technix/IDE做出贡献.zip

    Atom-IDE-Haskell-HIE 是一个专门为 Haskell 开发者设计的 Atom 文本编辑器插件,它集成了 Haskell IDE Engine (HIE) 的 Language Server Protocol (LSP) 功能。这个插件允许开发者在 Atom 编辑器中享受到强大的代码...

    haskell-brainfuck:Haskel 脑残翻译

    haskell-brainfuck 解释 haskel-brainfuck 作为库分发,但它也包含一个可执行文件来运行 Brainfuck 程序。 你可以在找到 haskell-brainfuck用法图书馆 import HaskBF.Evalimport qualified Data.ByteString.Lazy as ...

Global site tag (gtag.js) - Google Analytics