加入函数和类型问题haskell [英] join function and type issues haskell
问题描述
我有2个列表表达式和绑定(id = Expr),并试图用一个名为newE的新列表中的绑定列表替换每个表达式,其中Expression = id ..
最初,我有只有一个表达式:
blockquote
eq(div(add 2 7)(sub 5 2))3
/ blockquote>
我想用该表达式中的每个标识符替换绑定列表中的每个标识符,所以我试图将这个表达式拆分为一个字符串列表并删除括号,以分隔每个标识符..
这是我试图实现它的方式:
newE = [\x - > getExp(b)| x < - eStr,b < - bs,x == getId(b)]
其中es = getExpressions(prog)
bs = getBindings(prog)
- 一个字符串列表
- 删除括号
eStr = map(delete')')(map(delete')')
(map(delete'(')(split (show es))))
newE'= joinnewE
这是否正确
现在我得到的错误是,newE返回[t0 - > Expr],而它应该只返回Expr,为什么会这样?
,并且连接函数期望[Char] ..而它的类型在 Data.List.Utils 文档是:
join :: [a] - > [ [a]] - > [a]
所以,它不应该接受任何类型,而不仅仅是特征列表RS?还是与另一个图书馆的加入相混淆?
我搜索了我导入的库,但它们没有连接。
任何帮助解决这些错误并修改代码以执行它应该做什么?
谢谢
,这是一个传统方法的草图:
-
将字符串表达式转换为
Expr < code $ value,例如
add 2 7 - > App(App(Varadd)(Var2))(Var7)
编写一个函数
lookupBinding
来查找符号的绑定: b
lookupBinding :: [Binding] - > Symbol - > Maybe Expr
- 编写替代函数以将绑定定义替换为表达式:
$ bsubstitute :: [Binding] - > Expr - > Expr
它会像这样:
替代绑定(App e1 e2)= App e1'e2'
其中e1'=替代绑定e1
e2'=替换绑定e2
替换绑定(Var sym)= ... lookupBinding sym ...
替换绑定(Lam sym exp)= ...替代绑定'exp ...
I have 2 lists Expressions and bindings (id = Expr), and trying to replace each expression with its equivalent from the bindings list in a new list called newE, where Expression = id .. Initially, I have only one expression:
eq (div (add 2 7) (sub 5 2)) 3
I want to replace each identifier in this expression with its equivalent from the bindings list, so I tried to split this expression into a list of strings and removed brackets, to separate each identifier ..
This is how I tried to implement it:
newE = [\x -> getExp(b) | x <- eStr, b <- bs, x == getId(b)]
where es = getExpressions (prog)
bs = getBindings (prog)
-- Extracting expression into a list of strings
-- and removing brackets
eStr = map (delete ')')(map (delete ')')
(map (delete '(') (split " " (show es))))
newE' = join " " newE
Is this correct?
Now I'm getting errors that newE returns [t0 -> Expr] while it's supposed to return Expr only, why is that?
and the join function is expecting a [Char] .. while its type in the Data.List.Utils documentation is:
join :: [a] -> [[a]] -> [a]
so, isn't it supposed to accept any type not just list of characters? or did it get confused with a 'join' from another library? I searched the libraries I've imported, but they don't have a join.
Any help to resolve these errors and modify the code to do what it's supposed to do?
Thank you
Since you asked, here is a sketch of the conventional approach:
Convert the string expression into a
Expr
value, e.g.add 2 7 -> App (App (Var "add") (Var "2")) (Var "7")
Write a function
lookupBinding
to lookup a binding for a Symbol:lookupBinding :: [Binding] -> Symbol -> Maybe Expr
Write a substitute function to substitute binding definitions into an expression:
substitute :: [Binding] -> Expr -> Expr
It will go something like this:
substitute bindings (App e1 e2) = App e1' e2'
where e1' = substitute bindings e1
e2' = substitute bindings e2
substitute bindings (Var sym) = ... lookupBinding sym ...
substitute bindings (Lam sym exp) = ... substitute bindings' exp ...
这篇关于加入函数和类型问题haskell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!