(Show([(String,Int)]

编程入门 行业动态 更新时间:2024-10-11 09:20:47
本文介绍了(Show([(String,Int)] - > Int))的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果我使用lambda表达式来计算表达式在运行时生成规则中的表达式的值不起作用。

例如,代码

Exp:let var'='Exp在Exp {\p - > $ 6(($ 2,$ 4 p):p)} | Exp1 {$ 1} Exp1:Exp1'+'Term {\p - > $ 1 p + $ 3 p} | Exp1' - 'Term {\p - > $ 1 p - $ 3 p} |期限{$ 1} 期限:期限'*'因子{\ p - > $ 1 p * $ 3 p} |术语'/'因素{\ p - > $ 1 p`div` $ 3 p} |因子{$ ​​1} 因子:int {\ p - > $ 1} | var {\p - >案例查询$ 1 p 无 - >错误no var只是我 - >我} | '('Exp')'{$ 2}

from www.haskell/happy/doc/html/sec-using.html doesn' $ b

或者更精确地说,我收到了错误信息

没有使用`print'引起的(Show([(String,Int)] - > Int)) 可能的修正:添加一个实例声明( Show([(String,Int)] - > Int))在一个交互式GHCi命令的列表中:print it

如果你能向我解释我必须改变的东西,那将是非常好的。

它必须与lambda表达式有关,环境变量p。

当我使用数据类型时,一切正常。

解决方案

这里要注意的是,这个解析器的结果是一个函数,它需要一个环境可变绑定的入口。错误信息基本上是GHCi,它告诉你它不能打印函数,大概是因为你忘记了传递一个环境

> eval1 + 1

当您应该通过空的环境时

> eval1 + 1[]

或者带有一些预先定义的变量

> evalx + x[(x,1)]

to calculate the value of the expression on the fly at the production rules in happy doesn't work if I'm using the lambda expressions.

For example this code

Exp : let var '=' Exp in Exp { \p -> $6 (($2,$4 p):p) } | Exp1 { $1 } Exp1 : Exp1 '+' Term { \p -> $1 p + $3 p } | Exp1 '-' Term { \p -> $1 p - $3 p } | Term { $1 } Term : Term '*' Factor { \p -> $1 p * $3 p } | Term '/' Factor { \p -> $1 p `div` $3 p } | Factor { $1 } Factor : int { \p -> $1 } | var { \p -> case lookup $1 p of Nothing -> error "no var" Just i -> i } | '(' Exp ')' { $2 }

from www.haskell/happy/doc/html/sec-using.html doesn't work.

Or more precisly I 've got an error message

No instance for (Show ([(String, Int)] -> Int)) arising from a use of `print' Possible fix: add an instance declaration for (Show ([(String, Int)] -> Int)) In a stmt of an interactive GHCi command: print it

It would be nice if you could explain me what I have to change.

It must have something to do with the lambda expression and the environment variable p.

When I'm using data types everything is fine.

解决方案

The thing to note here is that the result of this parser is a function which takes an environment of variable bindings. The error message is basically GHCi telling you that it can't print functions, presumably because you forgot to pass an environment

> eval "1 + 1"

when you should have either passed an empty environment

> eval "1 + 1" []

or one with some pre-defined variables

> eval "x + x" [("x", 1)]

更多推荐

(Show([(String,Int)]

本文发布于:2023-07-09 20:57:39,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Show   String   Int

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!