haskell 和函数的类型定义。有几个问题
如果我执行 any isUpper "asBsd"
,我将得到 True
。
这里,any
的第二个元素是一个字符串。
但是,如果我这样做:
any ("1" `isInfixOf`) ["qas","123","=-0"]
any
的第二个元素是字符串列表。
这两个功能之间如何以及为何存在这种差异?
另一个例子。
如果我写 filter isUpper "asdVdf"
,我会得到 "V"
。
这里,要过滤的第二个元素是一个字符串。
但是,如果我这样写:filter (isUpper . head) ["abc","Vdh","12"]
,我会得到["Vdh"]
。
如您所见,要过滤的第二个元素现在是字符串列表。
为什么存在差异以及 haskell 如何知道这两种情况都是正确的?
总结一下:
我不明白如何在同一个函数中,一次 haskell 获取第二个元素(字符串),而另一次,haskell 获取第二个元素中的字符串列表。
一次发生在 any
函数中,另一次发生在 filter
函数中。
haskell(和我)如何知道这两种情况都是正确的?
谢谢 :-)。
if i do any isUpper "asBsd"
, i'll get True
.
here, the second element to any
is a string.
but, if i do this:
any ("1" `isInfixOf`) ["qas","123","=-0"]
the second element to any
is a list of strings.
how and why this difference between those 2 functions?
another example.
if i write filter isUpper "asdVdf"
, i'll get "V"
.
here, the second element to filter, is a string.
but, if i write this:filter (isUpper . head) ["abc","Vdh","12"]
, i'll get ["Vdh"]
.
as you can see, the second element to filter is now a list of strings.
why there is a differences and how haskell know's it's right in both cases?
to summarize it:
i don't understand how in the same function, one time haskell get a second element that is a string, and in other time, haskell get a list of strings, in the second element.
one time it happened in any
function, and the other time in filter
function.
how haskell(and me) know's it's right in both cases?
thanks :-).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为
isUpper
是一个Char -> Bool
函数和"1" 'isInfixOf'
和isUpper 。 head
是[Char] -> Bool
函数可以重写为
我们知道
isInfixOf
的类型是[a] -> [一]->布尔
1。现在,isInfixOf
的第一个参数是"1"
,其类型为[Char]
,因此我们可以推导出a
> 是一个Char
:这意味着
isInfixOf "1"
现在是一个[Char] ->布尔函数。
现在,
any
的类型为(a -> Bool) -> [一]->布尔函数。如上,
any (isInfixOf "1")::[a] ->布尔
= [[字符]] -> Bool
为了满足
any (isInfixOf "1")
的类型约束,参数必须是字符串列表。现在考虑
isUpper
。isUpper
的类型是Char ->布尔。因此:
所以
any isUpper
只需要接受一个字符串,而不是一个字符串列表。最后, isUpper 。头。在Haskell中,相关函数的类型为:
因此对于
filter isUpper
,a = Char
,类型为[Char] -> [Char]
,即需要以字符串作为参数。并且2:
因此对于
filter (isUpper . head)
,我们有a = [Char]
并且类型为[[Char ]]-> [[Char]]
,即需要接受一个字符串列表作为参数。注意:
isInfixOf
的类型实际上是(Eq a) =>; [一]-> [一]-> Bool
因为相等对于类型a
必须有效,但这与我们的分析无关。head
的变量a
更改为b
,但这并不重要。Because
isUpper
is aChar -> Bool
function and"1" ‘isInfixOf‘
andisUpper . head
are[Char] -> Bool
functionscan be rewritten as
We knew the type of
isInfixOf
is[a] -> [a] -> Bool
1. Now the first argument toisInfixOf
is"1"
which is of type[Char]
, so we can deducea
is aChar
:That means
isInfixOf "1"
is now a[Char] -> Bool
function.Now, the type of
any
is(a -> Bool) -> [a] -> Bool
function. As above,any (isInfixOf "1") :: [a] -> Bool
= [[Char]] -> Bool
In order to satisfy with the type constraint of
any (isInfixOf "1")
, the argument must be a string list.Now consider
isUpper
. The type ofisUpper
isChar -> Bool
. Hence:So
any isUpper
needs to take a string only, instead of a string list.Finally,
isUpper . head
. In Haskell, the types of the relevant functions are:Hence for
filter isUpper
,a = Char
and the type is[Char] -> [Char]
, i.e. it needs to take a string as parameter.And2:
Thus for
filter (isUpper . head)
, we havea = [Char]
and the type is[[Char]] -> [[Char]]
, i.e. it needs to take a string list as parameter.Note:
isInfixOf
is actually(Eq a) => [a] -> [a] -> Bool
as the equality must be valid for typea
, but this is irrelevant in our analysis.a
tob
forhead
, but it doesn't matter.字符串实际上只是字符列表,即
[Char]
。因此,如果您愿意,“hello”是['h', 'e', 'l', 'l', 'o']
的简写。因此,在这两种情况下,您都会得到一些内容的列表,只是一种情况是字符列表,另一种情况是字符串列表(如果您愿意,也可以是字符列表列表)。A String is actually just list of characters, a
[Char]
. So if you like, "hello" is shorthand for['h', 'e', 'l', 'l', 'o']
. Therefore in both cases, you're getting a list of something, it's just that one case is a list of Char and the other case is a list of Strings (or a list of lists of chars, if you like).嗯,这就是多态性。
any
的类型为(a -> Bool) -> [一]-> Bool
其中类型变量a
可以是您喜欢的任何内容。在第一种情况下,它是Char
- 因为第二个参数是Char
列表 - 并且类型变为(Char -> Bool) ->字符串 -> Bool
(请记住,[Char]
与String
相同!);第二个a
是String
并且类型变为(String -> Bool) ->; [字符串] ->布尔。
filter
的推理类似。Well, that's polymorphism. The type of
any
is(a -> Bool) -> [a] -> Bool
where the type variablea
can be anything you like. In the first case it'sChar
- because the second argument is a list ofChar
s - and the type becomes(Char -> Bool) -> String -> Bool
(remember that[Char]
is the same asString
!); in the seconda
isString
and the type becomes(String -> Bool) -> [String] -> Bool
.The reasoning for
filter
is similar.