English 中文(简体)
*** 例外:Prelude.read:no parse in Haskell - Parsing, expression and Recursion
原标题: *** Exception: Prelude.read: no parse in Haskell - Parsing, Expressions and Recursion

This portion of code should read in two or more numbers (main io function omitted), then a "+" to give the sum. Rationals are used because later i will do multiplications and such other operations.

data Expression =  Number Rational
               | Add (Expression)(Expression)
               deriving(Show,Eq)

solve :: Expression -> Expression
solve (Add (Number x) (Number y)) = Number (x + y)

parse :: [String] -> [Expression] -> Double
parse ("+":s)(a:b:xs) = parse s (solve (Add a b):xs)
parse [] (answer:xs) = fromRational (toRational (read (show answer)::Float))
parse (x:xs) (y) = parse (xs) ((Number (toRational (read x::Float))):y)

(二) 差错是由于无法处理 par子功能。

*Main> parse ["1","2","+"] [Number 3]

*** Exception: Prelude.read: no parse

我研究了数据。 这一解决办法的网页和网页上,有点发现,希望得到一些帮助。 感谢

CSJC

最佳回答

第一个方程式,

parse ("+":s)(a:b:xs) = parse (s)((solve (Add (Number a) (Number b))):xs)

应当:

parse ("+":s)(a:b:xs) = parse (s)((solve (Add a b)):xs)

自每一类型签名以来,ab 页: 1

<>Or/strong>,根据第二和第三类,将类型改为

parse :: [String] -> [Rational] -> Double

并改变第一个方程式

parse ("+":s)(a:b:xs) = parse s ((a + b):xs)

两种确定守则的可能方法(更多有问题部分):

-- Complete solve to handle all cases
solve :: Expression -> Expression
solve expr@(Number _) = expr
solve (Add (Number x) (Number y)) = Number (x + y)
solve (Add x y) = solve (Add (solve x) (solve y))

-- Convert an Expression to Double
toDouble :: Expression -> Double
toDouble (Number x) = fromRational x
toDouble e = toDouble (solve e)

-- parse using a stack of `Expression`s
parse :: [String] -> [Expression] -> Double
parse ("+":s) (a:b:xs) = parse s ((solve (Add a b)):xs)
parse [] (answer:_) = toDouble answer
parse (x:xs) ys = parse xs (Number (toRational (read x :: Double)) : ys)
parse _ _ = 0

-- parse using a stack of `Rational`s
parseR :: [String] -> [Rational] -> Double
parseR ("+":s) (a:b:xs) = parseR s (a+b : xs)
parseR [] (answer:xs) = fromRational answer
parseR (x:xs) y = parseR xs ((toRational (read x::Double)):y)
parseR _ _ = 0

后者相当细致,因为最终产生了<代码>Double<>/code>,因此没有使用<代码>Rational的实际点。

在您的<代码>parse上,第三种方程式将<代码>Rational转换成Expression通过Number的构造者,但以其他方式予以罚款。 然而,第二个方程式含有不同类型的问题:

parse [] (answer:xs) = fromRational (toRational (read (show answer)::Float))

如果answer要么是Expression,要么是Rational,showwer不能作为Float加以统一,从而产生错误,如你的编辑所示:

(二) 差错是由于无法处理 par子功能。

*Main> parse ["1","2","+"] [Number 3]
*** Exception: Prelude.read: no parse

在使用第二个方程式时,中值的第一个要素(answer)为Number (3 % 1)show (Number (3 % 1)Number (3 % 1)",后者不是String。 可查阅

问题回答

暂无回答




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

How to get instance from string in C#?

Is it possible to get the property of a class from string and then set a value? Example: string s = "label1.text"; string value = "new value"; label1.text = value; <--and some code that makes ...

XML DOM parsing br tag

I need to parse a xml string to obtain the xml DOM, the problem I m facing is with the self closing html tag like <br /> giving me the error of Tag mismatch expected </br>. I m aware this ...

Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

Locating specific string and capturing data following it

I built a site a long time ago and now I want to place the data into a database without copying and pasting the 400+ pages that it has grown to so that I can make the site database driven. My site ...

热门标签