Haskell解析前缀评估 [英] Haskell parsing prefix evaluation

查看:152
本文介绍了Haskell解析前缀评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  import Data.Char(isDigit)
data Ast = V Int | Neg Ast | A Ast Ast | M表示方程式(Show,Eq)

parseE(+:xs)= let(e1,r1)= parseE xs; (e2,r2)= parseE r1 in(A e1 e2,r2)
parseE(*:xs)= let(e1,r1)= parseE xs; (e2,r2)= parseE r1 in(M e1 e2,r2)
parseE( - :xs)= let(a,r)= parseE r in(N a,r)
parseE (a,r)
parseE(x:xs)=(V(read x :: Int),xs)((:xs)= let(a,):r)= parseE r

eval xs = parseE xs

当我的输入如下所示: * + 1 2 * 3 + 7 - 2



我希望输出为: ((1 + 2)* 3)*(3 *(7-2​​))应显示 45



当我在haskell中加载我的文件时,出现此错误:

 :loadnewf。 hs
[1的1]编译主(newf.hs,解释)

newf.hs:6:44:错误:数据构造函数不在范围内:N :: Ast - > ; Ast
|
6 | parseE( - :xs)= let(a,r)= parseE r in(N a,r)

| ^
失败,加载了0个模块。


解决方案

错误信息表明数据构造函数 N 不在范围内。数据构造函数是 data 语句右侧的内容。在你的例子中, V Neg A M 是数据构造函数。 不在范围内的意思是没有定义它在哪里使用。



看起来你写了 N where你打算写 Neg ,反之亦然。将您的 data 语句修改为:

  data Ast = V Int | N Ast | A Ast Ast | M Ast Ast派生(Show,Eq)

允许程序编译。



程序中仍然存在一些错误。例如,以下内容会陷入循环:

 > (A(V 1)(V 2))(M(V 3)(A(V 7)(N  - 冻结) 

因为声明:



<$ p $ (a,r)= parseE r in(N a,r)

它引入了一个无意的递归定义 - 您正在定义 r 作为调用 parseE r 的结果,这会导致无限循环。在尝试处理括号的情况下,您也遇到了类似的问题。


I'm struggling with this code.

import Data.Char (isDigit)
data Ast = V Int | Neg Ast | A Ast Ast | M Ast Ast deriving (Show,Eq)

parseE ("+":xs) = let (e1,r1) = parseE xs; (e2,r2) = parseE r1 in (A e1 e2, r2)
parseE ("*":xs) = let (e1,r1) = parseE xs; (e2,r2) = parseE r1 in (M e1 e2, r2)
parseE ("-":xs) = let (a,r) = parseE r in (N a, r)
parseE ("(":xs) = let (a,")":r) = parseE r in (a,r)
parseE (x:xs) = (V (read x :: Int), xs) 

eval xs = parseE xs

When my input is something like: * + 1 2 * 3 + 7 - 2

I want the ouput to be: ((1+2)*3)*(3*(7-2)) which should show 45

When I load my file in haskell, I get this error :

:load "newf.hs"
[1 of 1] Compiling Main             ( newf.hs, interpreted )

newf.hs:6:44: error: Data constructor not in scope: N :: Ast -> Ast
  |
6 | parseE ("-":xs) = let (a,r) = parseE r in (N a, r)

  |                                            ^
Failed, 0 modules loaded.

解决方案

The error message says that the data constructor N is not in scope. Data constructors are the things on the right-hand side of data statements. In your example, V, Neg, A, and M are data constructors. "Not in scope" means "not defined where it was used".

It looks like you wrote N where you meant to write Neg, or vice versa. Fixing your data statement to read:

data Ast = V Int | N Ast | A Ast Ast | M Ast Ast deriving (Show,Eq)

allows the program to compile.

There are still a few bugs in the program. For example, the following gets stuck in a loop:

> parseE (words "* + 1 2 * 3 + 7 - 2")
(M (A (V 1) (V 2)) (M (V 3) (A (V 7) (N  -- freezes

because of the statement:

let (a,r) = parseE r in (N a, r)

which introduces an unintended recursive definition -- you're defining r as the result of calling parseE r, which causes an infinite loop. You have a similar problem in the case that tries to handle parentheses.

这篇关于Haskell解析前缀评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆