如何在实际实践中使用groupBy和zip?

发布于 2024-11-01 23:36:48 字数 1132 浏览 3 评论 0原文

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'
with actual type
[a1]'
In the return type of a call of nub'
In the first argument of
groupBy', namely `(nub (map tail q))'
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 技术交流群。

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

发布评论

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

评论(1

黯淡〆 2024-11-08 23:36:48

一个问题是 zip toInt(map q) (map last q) 没有按照您想象的方式进行解析。

与 C 风格语法的语言不同,haskell 将上面的内容解析为

 zip toInt (map head q) (map last q)

(注意空格)。

也就是说,它没有按照您希望的方式将 toInt 应用于 map head q 的结果。相反,它尝试执行 zip toInt (map head q) ,这会给你一个类型错误,因为你正在压缩一个函数和一个列表。

您想要的是

 zip (toInt (map head q)) (map last q)

或者稍微更简洁

 zip (toInt $ map head q) (map last 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

 zip toInt (map head q) (map last q)

(Note the space).

That is, it's not applying toInt to the result of map head q the way you want it to. Instead, it's attempting to do zip 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

 zip (toInt (map head q)) (map last q)

Or slightly more succinctly

 zip (toInt $ map head q) (map last q)

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.

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