通过两个输入列表映射函数 [英] Mapping a function over two input lists

查看:125
本文介绍了通过两个输入列表映射函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能,我想用几组输入来测试。假设这个函数是

  f :: a  - > b  - > c 

现在我有两个输入列表:

  inputA :: [a] 
inputB :: [[b]]

对于 inputA !!我,我想评估 f $输入!我为 inputB !!中的每个元素列出了。 I 。我知道我需要几个 map 这样的应用程序来执行此操作,但是我很难绕过解决方案。



我最近的尝试是

  map f inputA< $> inputB 

给出以下错误:


无法匹配预期的类型 a0 - > b0'与实际类型 [b1]'

地图'

调用的返回类型中可能的原因:
map'应用于太多参数

(< $>)'的第一个参数中,即 map f inputA'

在表达式中:map f inputA inputB


我该如何解决这个问题?我不一定需要一个完整的解决方案。推动(甚至推动)有益的方向肯定会受到赞赏。



其他想法

  map f inputA :: [b  - > c] 

我认为这是正确的方向。现在我需要将每个函数映射到 inputB 中的每个输入列表上。为了澄清,我想要通过 i map f inputA 中的 i th函数输入 inputB 中的输入列表以获得结果 outputC :: [[c]]

解决方案

如果我正确地理解了你的话,像这样: $ b

  mapNested ::(a  - > b  - > c) - > [a]  - > [[b]]  - > [[c]] 
mapNested f [] _ = [] $ b $ mapNested f _ [] = [] $ b $ mapNested f(x:xs)ys = concatMap(map(fx))ys :mapNested f xs ys

Main> mapNested(+)[1,2,3] [[1,2,3],[4,5,6],[7,8,9]]
[[2,3,4,5, 6,7,8,9,10],[3,4,5,6,7,8,9,10,11],[4,5,6,7,8,9,10,11,12] ]

如果这不是您要查找的内容,您能否提供示例输入和输出?



编辑



或者这是你想要的吗?

mapNested ::(a - > b - > c) - > [a] - > [[b]] - > [[c]]
mapNested f xs = zipWith map(map f xs)

Main> mapNested(,)[1,2,3] [[1,2,3],[4,5,6],[7,8,9]]
[[(1,1),(1 ,2),(1,3)],[(2,4),(2,5),(2,6)],[(3,7),(3,8),(3,9)] ]


I have a function that I want to test with several sets of inputs. Let's say the function is

f :: a -> b -> c

Now I have two lists of inputs:

inputA :: [a]
inputB :: [[b]]

For inputA !! i, I want evaluate f $ input !! i for each element of the list at inputB !! i. I know I need several applications of map to do this, but I am having difficulty wrapping my head around a solution.

My most recent attempt is

map f inputA <$> inputB

which gives the following error:

Couldn't match expected type a0 -> b0' with actual type[b1]'
In the return type of a call of map'
Probable cause:
map' is applied to too many arguments
In the first argument of (<$>)', namelymap f inputA'
In the expression: map f inputA inputB

How should I go about solving this problem? I don't necessarily want a complete solution. A push (or even a shove) in a helpful direction would definitely be appreciated.

Additional thoughts:

map f inputA :: [b -> c]

I think this is the right direction. Now I need to map each of the functions over each list of inputs in inputB.

To clarify, I want to map the ith function in map f inputA over the ith list of inputs in inputB to get a result outputC :: [[c]].

解决方案

If I'm understanding you correctly, something like:

mapNested :: (a -> b -> c) -> [a] -> [[b]] -> [[c]]
mapNested f [] _ = []
mapNested f _ [] = []
mapNested f (x:xs) ys = concatMap (map (f x)) ys : mapNested f xs ys

Main> mapNested (+) [1, 2, 3] [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[2,3,4,5,6,7,8,9,10],[3,4,5,6,7,8,9,10,11],[4,5,6,7,8,9,10,11,12]]

If this isn't what you're looking for, could you provide an example input and output?

EDIT

Or is this what you're wanting?

mapNested :: (a -> b -> c) -> [a] -> [[b]] -> [[c]]
mapNested f xs = zipWith map (map f xs)

Main> mapNested (,) [1, 2, 3] [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[(1,1),(1,2),(1,3)],[(2,4),(2,5),(2,6)],[(3,7),(3,8),(3,9)]]

这篇关于通过两个输入列表映射函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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