Haskell中的解析函数

发布于 2024-10-12 16:05:51 字数 1351 浏览 2 评论 0原文

我是 Haskell 的新手,我正在尝试解析表达式。我发现了秒差距,也找到了一些文章,但我似乎不明白我必须做什么。我的问题是我想给出一个像“x^2+2*x+3”这样的表达式,结果是一个接受参数 x 并返回一个值的函数。如果这是一个简单的问题,我很抱歉,但我真的需要一些帮助。谢谢!我插入的代码来自您可以在此链接上找到的文章。

import Control.Monad(liftM)
import Text.ParserCombinators.Parsec 
import Text.ParserCombinators.Parsec.Expr  
import Text.ParserCombinators.Parsec.Token  
import Text.ParserCombinators.Parsec.Language  

data Expr = Num Int       | Var String    | Add Expr Expr
          | Sub Expr Expr | Mul Expr Expr | Div Expr Expr
          | Pow Expr Expr
          deriving Show

expr :: Parser Expr
expr = buildExpressionParser table factor
    <?> "expression"

table = [[op "^" Pow AssocRight],
         [op "*" Mul AssocLeft, op "/" Div AssocLeft],
         [op "+" Add AssocLeft, op "-" Sub AssocLeft]]
    where
        op s f assoc
            = Infix (do{ string s; return f}) assoc
factor = do{ char '('
        ; x <- expr
        ; char ')'
        ; return x}
    <|> number
    <|> variable
    <?> "simple expression"

number :: Parser Expr
number = do{ ds<- many1 digit
        ; return (Num (read ds))}
    <?> "number"

variable :: Parser Expr
variable = do{ ds<- many1 letter
        ; return (Var ds)}
    <?> "variable"

I'm new to Haskell and I am trying to parse expressions. I found out about Parsec and I also found some articles but I don't seem to understand what I have to do. My problem is that I want to give an expression like "x^2+2*x+3" and the result to be a function that takes an argument x and returns a value. I am very sorry if this is an easy question but I really need some help. Thanks! The code I inserted is from the article that you can find on this link.

import Control.Monad(liftM)
import Text.ParserCombinators.Parsec 
import Text.ParserCombinators.Parsec.Expr  
import Text.ParserCombinators.Parsec.Token  
import Text.ParserCombinators.Parsec.Language  

data Expr = Num Int       | Var String    | Add Expr Expr
          | Sub Expr Expr | Mul Expr Expr | Div Expr Expr
          | Pow Expr Expr
          deriving Show

expr :: Parser Expr
expr = buildExpressionParser table factor
    <?> "expression"

table = [[op "^" Pow AssocRight],
         [op "*" Mul AssocLeft, op "/" Div AssocLeft],
         [op "+" Add AssocLeft, op "-" Sub AssocLeft]]
    where
        op s f assoc
            = Infix (do{ string s; return f}) assoc
factor = do{ char '('
        ; x <- expr
        ; char ')'
        ; return x}
    <|> number
    <|> variable
    <?> "simple expression"

number :: Parser Expr
number = do{ ds<- many1 digit
        ; return (Num (read ds))}
    <?> "number"

variable :: Parser Expr
variable = do{ ds<- many1 letter
        ; return (Var ds)}
    <?> "variable"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蹲在坟头点根烟 2024-10-19 16:05:51

这只是带有变量的表达式的解析器。实际上解释这个表达式是完全不同的事情。

您应该创建一个函数,该函数接受已解析的表达式和变量值,并返回计算表达式的结果。伪代码:

evaluate :: Expr -> Map String Int -> Int
evaluate (Num n) _ = n
evaluate (Var x) vars = {- Look up the value of x in vars -}
evaluate (Plus e f) vars = {- Evaluate e and f, and return their sum -}
...

我故意省略了一些细节;希望通过探索缺失的部分,您能够更多地了解 Haskell。

下一步,您可能应该查看 Reader monad,以获取传递变量映射 vars 的便捷方法,并使用 MaybeError 表示错误,例如引用未在 vars 中绑定的变量,或除以零。

This is just a parser for expressions with variables. Actually interpreting the expression is an entirely separate matter.

You should create a function that takes an already parsed expression and values for variables, and returns the result of evaluating the expression. Pseudocode:

evaluate :: Expr -> Map String Int -> Int
evaluate (Num n) _ = n
evaluate (Var x) vars = {- Look up the value of x in vars -}
evaluate (Plus e f) vars = {- Evaluate e and f, and return their sum -}
...

I've deliberately omitted some details; hopefully by exploring the missing parts, you learn more about Haskell.

As a next step, you should probably look at the Reader monad for a convenient way to pass the variable map vars around, and using Maybe or Error to signal errors, e.g. referencing a variable that is not bound in vars, or division by zero.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文