Scanl在Haskell中 [英] Scanl in Haskell

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

问题描述

  scanl(\exp y  - > scanl(\ x  - > if(isLetter x)then update exp(Literal x)else 
if x =='+'然后更新exp Epsilon+else
if x =='*'然后更新exp Epsilon*else
更新exp Epsilon|)y)Epsilon q

我有一个[Char]的列表,我想将它们转换为我的数据类型,但我遇到了一些问题用它。
我喜欢这样的[a + b,c * d],我希望将scanl应用于a + b
时的结果保留下来,以便我可以将它用于第二个列表。有人可以帮我吗?

我的数据类型是:

pre $ data Reg = Epsilon |文字字符|或Reg Reg |然后Reg Reg | Star Reg推导出Eq

  update :: Reg  - >注册 - > [Char]  - >注册
更新ab=(a`Then` b)
更新ab|=(a`或`b)
更新ab*=(Star a)
更新ab+=(加a)
更新ab? =(opt a)

我试图改变这样的形式[a |,为了记录, scanl code>在传递状态方面并不是很好,特别是在这里你不希望结果成为一个列表,而只是最终的结果。更广义的方法是使用 fold 来代替。


scanl (\exp y -> scanl (\x -> if (isLetter x) then update exp (Literal x) "" else 
                    if x=='+' then update exp Epsilon "+" else
                    if x=='*' then update exp Epsilon "*" else
                    update exp Epsilon "|") y) Epsilon q

I have a list of [Char] and I want to transform them into my data type but I am having some problems with it. I have somenthing like this ["a+b","c*d"] and I want the result of when I apply scanl on "a+b" to remain so I can use it for the second list. Can someone help me ?

My data type is:

data Reg = Epsilon | Literal Char | Or Reg Reg |  Then Reg Reg |  Star Reg  deriving Eq

and

update:: Reg -> Reg -> [Char] -> Reg 
update a b "" = (a `Then` b)
update a b "|"= (a `Or` b) 
update a b "*" = (Star a) 
update a b "+" = (plus a)
update a b "?" = (opt a)  

And I am trying to transform something like this ["a|","bc"] to (Then a (Or b c))

解决方案

For the record, scanl isn't great at passing state along, and especially here you don't want the result to be a list at all, but just a final result. The more generalized approach would be to use a fold instead.

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

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