具有“无数据”的列表操作

发布于 2024-12-08 14:31:33 字数 426 浏览 1 评论 0原文

关于列表的进一步问题:

aaa ::  [[(Char, Float)]] -> Float ->  [[(Char, Float)]]
aaa [[]] a = error "no indata"
aaa [[(a,b)]] c = [[(a, b/c)]] 
aaa ([(a,b)]:tail) c = [(a, b/c)] : (aaa tail c)

如何使其工作:

aaa [[('a',3),('b',4),('c',5)],[('a',3),('b',4),('c',5)] ] 4

结果:

[[('a',0.75),('b',1),('c',1.25)],[('a',0.75),('b',1),('c',1.25)]]

A further question about List:

aaa ::  [[(Char, Float)]] -> Float ->  [[(Char, Float)]]
aaa [[]] a = error "no indata"
aaa [[(a,b)]] c = [[(a, b/c)]] 
aaa ([(a,b)]:tail) c = [(a, b/c)] : (aaa tail c)

How to make it work with:

aaa [[('a',3),('b',4),('c',5)],[('a',3),('b',4),('c',5)] ] 4

the result:

[[('a',0.75),('b',1),('c',1.25)],[('a',0.75),('b',1),('c',1.25)]]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

梦在深巷 2024-12-15 14:31:33

有多种方法可以实现您的目标。我认为尝试显式地实现递归是有启发性的,就像您尝试做的那样。但是,我认为如果您将其分成更小的部分,您的代码会更清晰。考虑使用两个具有以下签名的函数:

innerTransform :: [(Char, Float)] -> Float -> [(Char, Float)]

aaa :: [[Char, Float]] -> Float -> [[(Char, Float)]]

然后您可以在 aaa 的实现中使用 innerTransform

当您通过显式递归实现 aaa 时,您可以尝试使用 map 等函数来实现它,如另一个答案中所述。作为这个方向的中间步骤,尝试通过将 innerTransform 映射到外部列表来解决它。您需要稍微调整一下 innerTransform: 的参数顺序,

innerTransformFlipped :: Float -> [(Char, Float)] -> [(Char, Float)]

然后将该函数部分应用于 float 以获得:

mappingFunction :: [(Char, Float)] -> [(Char, Float)]

,您可以将其用作 map 的参数。

There is a number a ways you can achieve your goal. I think it is instructive to try to implement the recursion explicitly, as you have tried to do. However, I think your code will be more clear, if you split it up into smaller parts. Consider having two functions, with the following signatures:

innerTransform :: [(Char, Float)] -> Float -> [(Char, Float)]

aaa :: [[Char, Float]] -> Float -> [[(Char, Float)]]

Then you can use innerTransform in your implementation of aaa.

When you have implemented aaa through explicit recursion, you can try to work towards implementing it with functions such as map, as described in another answer. As a middle step in that direction, try solving it by mapping innerTransform over the outer list. You need to fiddle around a bit with the argument order to innerTransform:

innerTransformFlipped :: Float -> [(Char, Float)] -> [(Char, Float)]

and then partially apply that function to the float to obtain:

mappingFunction :: [(Char, Float)] -> [(Char, Float)]

which you can use as an argument to map.

翻身的咸鱼 2024-12-15 14:31:33

使用列表理解,这可能是这样的:

aaa l v = [map (\(a,­b) -> (a,b/v))  x | x <- l]

Using list comprehension this could be something like :

aaa l v = [map (\(a,­b) -> (a,b/v))  x | x <- l]
心碎无痕… 2024-12-15 14:31:33

怎么样:

> let l = [[('a',3),('b',4),('c',5)],[('a',3),('b',4),('c',5)] ]
> let aaa list n = map (map (\(c,y) -> (c,(fromIntegral y) / n))) list
> aaa l 4.0
[[('a',0.75),('b',1.0),('c',1.25)],[('a',0.75),('b',1.0),('c',1.25)]]

您的代码片段中的类型匹配。您尝试将在浮点上定义的函数应用于整数。
您必须将浮点数传递给函数。这就是为什么我在对它们应用浮点除法“/”之前将整数(fromIntegral y)转换为浮点数。第二个参数也必须是浮点数,因此使用 4.0 而不是 4。

how about:

> let l = [[('a',3),('b',4),('c',5)],[('a',3),('b',4),('c',5)] ]
> let aaa list n = map (map (\(c,y) -> (c,(fromIntegral y) / n))) list
> aaa l 4.0
[[('a',0.75),('b',1.0),('c',1.25)],[('a',0.75),('b',1.0),('c',1.25)]]

The types in your snippet down match. You try to apply functions defined on float on integers.
You have to pass floats to the functions. That's why i convert the integers with (fromIntegral y) to floats before applying the floating point division "/" on them. The second argument has to be a floating point number too, so use 4.0 instead of 4.

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