So far we have introduced the basic construct that underpinning the haskell runtime and etc... Now we can come to the point where we leverage the haskell language to functinally solving some problems.
the problem that we are going to solve in this post is the Reverse Polish Notation, which is a postfix notation of the mathmatical operations, which normally represent 1 + 2 as 1 2 +;
now that we want to develop some algorithm to to take a RPN notation string and then gives back the calculation result.
here is what we have developed, based on the fact we can use recursion and we can divide the problem into smaller ones, and each has a case calcuation based on the type of symbol that is current met on the input, the result will be put back on the top of the stack, here is the code.
-- file -- reverseRPN2.hs -- descrpition: -- solve the reverseRPN data issues import Data.List solveRPN :: String -> Float solveRPN = head . foldl foldingFunction [] . words where foldingFunction (x:y:ys) "*" = (x * y):ys foldingFunction (x:y:ys) "+" = (x + y):ys foldingFunction (x:y:ys) "-" = (y - x):ys foldingFunction (x:y:ys) "/" = (y / x):ys foldingFunction (x:y:ys) "^" = (y ** x):ys foldingFunction (x:xs) "ln" = log x:xs foldingFunction xs "sum" = [sum xs] foldingFunction xs numberString = read numberString:xs
so let's explicate on the code, the code will check which is the current symbol that is under inspection, where it finds that if it is a '+' symbol, then it will add the immediate two numbers, which it will append the result (x + y) back to the head of the remaining of the arguments... and the calculation continues.
Though the code does not deal with Exception so well, we will see later how we can better deal with Exceptions. (such as invalid input).
now let's see some live example of the function running on certain code.
ghci > solveRPN "10 4 3 + 2 * -" -4 ghci > solveRPN "2 3 +" 5 ghci > solveRPN "90 34 12 33 55 66 + * - +" -3947 ghci > solveRPN "90 34 12 33 55 66 + * - + -" 4037 ghci > solveRPN "90 34 12 33 55 66 + * - + -" 4037 ghci > solveRPN "90 3 -" 87
相关推荐
Atom-haskell-ghc-mod.zip,haskell-ghc-mod atom packagehaskell ghc mod atom包,atom是一个用web技术构建的开源文本编辑器。
Atom-ide-haskell-hoogle.zip,在光标下显示符号的滚动信息艾德·哈斯克尔·胡格尔,atom是一个用web技术构建的开源文本编辑器。
haskell-mode emacs haskell-mode emacs
Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码 Haskell-Data-Analysis-Cookbook这是 Haskell数据分析 cookbook的附带源代码。最新的源代码可以在GitHub上获得: ...
从1.0.0开始,haskell-ghc-mod提供haskell-completion-backend服务。 注意:在1.0.0之前,提供了ide-backend服务。 它已被废弃以支持ide-haskell的UPI。 您可以在找到描述 执照 版权所有:copyright:2015 Atom-...
Programming-in-Haskell-2nd-Edition.pdf
haskell-ghc-mod原子包 该软件包主要用作后端。 Haskell ghc-mod打开通往ghc-modi的管道,并查询类型,信息并检查错误。 安装与配置 请参考官方文档站点 服务中心API 从1.0.0版本开始,haskell-ghc-mod提供...
用于 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 ...
Get Programming with HASKELL-2018-英文版
演示医疗用例的参考DAML应用程序 -Haskell-TypeScript-下载
haskell-chart, haskell的2D 图表库 图 haskell的2D 图表库进一步的信息可以在关联的 wiki中找到。
Server Metaprogramming Ruby-Pyton-Groovy-Haskell-Erlang.pdf
A History of Haskell - Being Lazy With Class
Atom-ide-haskell-cabal.zip,Cabal backend provider for ide-haskellIDE Haskell Cabal套餐,atom是一个用web技术构建的开源文本编辑器。
haskell-lsp-client 该软件包适用于希望使其文本编辑器与兼容的文本编辑器的开发人员。 我已经开发了此软件包,并计划将其集成到。 示例客户端 该存储库中包含一个示例客户端。 此示例客户端仅运行并打开在命令行...
Atom-atom-haskell-scry.zip,De-emphasize qualified Haskell identifiers.SCRY,atom是一个用web技术构建的开源文本编辑器。
haskell-brainfuck 解释 haskel-brainfuck 作为库分发,但它也包含一个可执行文件来运行 Brainfuck 程序。 你可以在找到 haskell-brainfuck用法图书馆 import HaskBF.Evalimport qualified Data.ByteString.Lazy as ...
Atom-atom-haskell-pointfree.zip,atom包:将选择转换为无点或有点表示Haskell无点包,atom是一个用web技术构建的开源文本编辑器。
Haskell - The Craft of Functional Programming, 2ed (Addison-Wesley, 1999) by Tantanoid 已加书签
Atom-haskell-debug.zip,使用ghci在atom中实现图形haskell调试器Haskell调试,atom是一个用web技术构建的开源文本编辑器。