如何在实际实践中使用groupBy和zip?
import Data.List.Split
import Data.List(nub, groupBy)
z = splitOn "+" "x^2+2*x^3+x^2"
y = map (splitOn "*") z
x = map head y
toInt :: [String] -> [Int]
toInt = map read
u1 = filter ((< 2) . length) y
u2 = filter ((> 1) . length) y
v = map ("1" :) u1
q = u2 ++ v
q2 = zip toInt(map head q) (map last q)
q6 = groupBy nub(map tail q) q
q3 = map tail q
q5 = nub(q3)
q1 = map head q
1.因为
zip toInt(map head q) (map last q)
我想在将头转换为整数后将头添加回尾部 结果应该是 [[1,"x^3"],[2,"x^2"],[1,"x^2"]]
我可以做到,
*Main Data.List> zip [2,1,1] ["x^3","x^2","x^2"]
[(2,"x^3"),(1,"x^2"),(1,"x^2")]
但上面不能,并且我注意到有一个区别是,这是()
,而不是[]
2.如何在列表上写groupBy
,我已经通过了groupBy
的不同元素 分组后,就是添加他们的头了
groupBy (nub(map tail q)) q
:1:10: 无法匹配预期类型 a0 -> a0->布尔' 与实际类型
[a1]' 在 nub' 调用的返回类型中
groupBy'的第一个参数中,即`(nub(map tail q))' 表达式中:groupBy(nub(map tail q)) q
q 就像一个哈希表,似乎不能按第二个元素进行分组
import Data.List.Split
import Data.List(nub, groupBy)
z = splitOn "+" "x^2+2*x^3+x^2"
y = map (splitOn "*") z
x = map head y
toInt :: [String] -> [Int]
toInt = map read
u1 = filter ((< 2) . length) y
u2 = filter ((> 1) . length) y
v = map ("1" :) u1
q = u2 ++ v
q2 = zip toInt(map head q) (map last q)
q6 = groupBy nub(map tail q) q
q3 = map tail q
q5 = nub(q3)
q1 = map head q
1. For
zip toInt(map head q) (map last q)
I would like to add back the head to the tail after convert head into integer
result should be [[1,"x^3"],[2,"x^2"],[1,"x^2"]]
I can do
*Main Data.List> zip [2,1,1] ["x^3","x^2","x^2"]
[(2,"x^3"),(1,"x^2"),(1,"x^2")]
but above can not, and there is a difference I noticed is, this is ()
, not []
2. How to write groupBy
on a list, I have passed distinct elements for groupBy
After grouping, it is for adding their head
groupBy (nub(map tail q)) q
:1:10:
Couldn't match expected type a0 -> a0 -> Bool'
[a1]'
with actual type
In the return type of a call of nub'
groupBy', namely `(nub (map tail q))'
In the first argument of
In the expression: groupBy (nub (map tail q)) q
q is like a hash table, it seems that it can not group by second element
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个问题是
zip toInt(map q) (map last q)
没有按照您想象的方式进行解析。与 C 风格语法的语言不同,haskell 将上面的内容解析为
(注意空格)。
也就是说,它没有按照您希望的方式将
toInt
应用于map head q
的结果。相反,它尝试执行 zip toInt (map head q) ,这会给你一个类型错误,因为你正在压缩一个函数和一个列表。您想要的是
或者稍微更简洁
至于您的第二个问题,您在语法方面遇到了类似的问题。此外,
groupBy
的第一个参数必须是一个函数,用于确定创建组时的相等性。One issue is that
zip toInt(map q) (map last q)
isn't getting parsed the way you think it is.Unlike languages with C-style syntax, haskell parses the above as
(Note the space).
That is, it's not applying
toInt
to the result ofmap head q
the way you want it to. Instead, it's attempting to dozip toInt (map head q)
, which will give you a type error, since you're zipping a function and a list.What you want instead is
Or slightly more succinctly
As for your second issue, you're having a similar issue with syntax. Also, the first argument to
groupBy
needs to be a function that determines equality for the purposes of creating groups.