地图不是带函数并且列表返回列表吗? [英] Isn't map takes a function and a list return a list?

查看:138
本文介绍了地图不是带函数并且列表返回列表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

map2_List :: (a -> b -> c) -> [a] -> [b] -> [c]
map2_List f [] _ = []
map2_List f (a:as) bs = map (f a) bs ++ map2_List f as bs

这是我演讲中的一个示例,该示例尝试将二进制函数应用于两个列表的所有成对元素. (f a)部分使我感到困惑.它应该是一个值而不是一个函数吗?那么map value bs会做什么?

This is an example from my lecture, which try to apply a binary function to all pairs of elements of two lists. The part (f a) makes me confused. Does it suppose to be a value but not a function? Then what does map value bs do?

推荐答案

"(f a)部分使我感到困惑."

"The part (f a) makes me confused."

这里发生的事情称为 currying ,如果您是从命令式语言来的Haskell,它会可能会造成混乱.

What is happening here is called currying and if you are coming to Haskell from imperative languages, it can be confusing.

在Haskell中,所有函数在技术上都采用一个值并返回一个值.返回的值可以是另一个函数.大多数程序员会想到思维捷径,即考虑函数将定义中的所有值(术语为饱和" BTW)并产生最终值,但是即使有了这种捷径,在很多情况下,诸如此类并非如此.

In Haskell, all functions technically take a single value and return a single value. The returned value could be another function. Most programmers take the mental shortcut of thinking of functions taking all the values in their definition (the term is "saturated" BTW) and producing the final value, but even with this mental shortcut, there are a lot of times, like this, when it is not the case.

函数f是二进制函数,而(f a)是该函数部分应用. (f a) :: b -> c的类型.

The function f is a binary function, and (f a) is that function partially applied. The type of (f a) :: b -> c.

那么map value bs会做什么?"

"Then what does map value bs do?"

地图函数(map :: (a->b) -> [a] ->[b])是标准前奏的一部分.它具有一个简单的功能,并将其应用于列表中的每个元素.

The map function (map :: (a->b) -> [a] ->[b]) is part of the standard prelude. It takes a simple function and applies it to each element in a list.

所以让我们分开map (f a) bs:

  • map :: (b->c) -> [b] ->[c] 将功能应用于列表的每个元素
  • (f a) :: b -> c使用 currying 将函数f :: a -> b -> c应用于a并返回函数b -> c
  • bsmap2_List
  • 中的第二个列表
  • 此函数的结果为[c],该函数f应用于第一个列表的一个元素,然后应用于第二个列表的每个元素.
  • map :: (b->c) -> [b] ->[c] applies a function to each element of a list
  • (f a) :: b -> c using currying a function f :: a -> b -> c is applied to an a and returns a function b -> c
  • bs is the second list from map2_List
  • The result of this function is [c], the function f applied to one element of the first list then applied to every element of the second list.

这篇关于地图不是带函数并且列表返回列表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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