haskell 和函数的类型定义。有几个问题

发布于 2024-08-22 19:00:19 字数 727 浏览 6 评论 0原文

如果我执行 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 技术交流群。

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

发布评论

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

评论(3

神经大条 2024-08-29 19:00:19

因为 isUpper 是一个 Char -> Bool 函数和 "1" 'isInfixOf'isUpper 。 head[Char] -> Bool 函数


"1" `isInfixOf` xxx

可以重写为

isInfixOf "1" xxx

我们知道 isInfixOf 的类型是 [a] -> [一]->布尔1。现在,isInfixOf 的第一个参数是 "1",其类型为 [Char],因此我们可以推导出 a > 是一个 Char

     isInfixOf :: [a]    -> [a] -> Bool
       "1"     :: [Char]
//∴ a = Char and
 isInfixOf "1" ::           [a] -> Bool
                =        [Char] -> Bool

这意味着 isInfixOf "1" 现在是一个 [Char] ->布尔函数。

现在,any 的类型为 (a -> Bool) -> [一]->布尔函数。如上,

               any :: (a      -> Bool) -> [a] -> Bool
     isInfixOf "1" :: ([Char] -> Bool)
 //∴ a = [Char] and

any (isInfixOf "1")::[a] ->布尔
= [[字符]] -> Bool

为了满足 any (isInfixOf "1") 的类型约束,参数必须是字符串列表。


现在考虑isUpperisUpper的类型是Char ->布尔。因此:

              any :: (a    -> Bool) -> [a] -> Bool
          isUpper :: (Char -> Bool)
//∴ a = Char and
      any isUpper ::                   [a] -> Bool
                   =                [Char] -> Bool

所以 any isUpper 只需要接受一个字符串,而不是一个字符串列表。


最后, isUpper 。头。在Haskell中,相关函数的类型为:

 filter :: (a -> Bool) -> [a] -> [a]
   head :: [a] -> a
isUpper :: Char -> Bool
    (.) :: (b -> c) -> (a -> b) -> a -> c

因此对于filter isUppera = Char,类型为[Char] -> [Char],即需要以字符串作为参数。

并且2

            (.) :: (b    -> c   ) -> (a   -> b) -> a -> c
        isUpper :: (Char -> Bool)
           head ::                   ([b] -> b)
//∴ c = Bool, b = Char, a = [b] = [Char], and
 isUpper . head ::                                 a -> c
                =                             [Char] -> Bool

因此对于filter (isUpper . head),我们有a = [Char]并且类型为[[Char ]]-> [[Char]],即需要接受一个字符串列表作为参数。


注意:

  1. isInfixOf的类型实际上是(Eq a) =>; [一]-> [一]-> Bool 因为相等对于类型 a 必须有效,但这与我们的分析无关。
  2. 我暂时将 head 的变量 a 更改为 b,但这并不重要。

Because isUpper is a Char -> Bool function and "1" ‘isInfixOf‘ and isUpper . head are [Char] -> Bool functions


"1" `isInfixOf` xxx

can be rewritten as

isInfixOf "1" xxx

We knew the type of isInfixOf is [a] -> [a] -> Bool1. Now the first argument to isInfixOf is "1" which is of type [Char], so we can deduce a is a Char:

     isInfixOf :: [a]    -> [a] -> Bool
       "1"     :: [Char]
//∴ a = Char and
 isInfixOf "1" ::           [a] -> Bool
                =        [Char] -> Bool

That means isInfixOf "1" is now a [Char] -> Bool function.

Now, the type of any is (a -> Bool) -> [a] -> Bool function. As above,

               any :: (a      -> Bool) -> [a] -> Bool
     isInfixOf "1" :: ([Char] -> Bool)
 //∴ a = [Char] and

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 of isUpper is Char -> Bool. Hence:

              any :: (a    -> Bool) -> [a] -> Bool
          isUpper :: (Char -> Bool)
//∴ a = Char and
      any isUpper ::                   [a] -> Bool
                   =                [Char] -> Bool

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:

 filter :: (a -> Bool) -> [a] -> [a]
   head :: [a] -> a
isUpper :: Char -> Bool
    (.) :: (b -> c) -> (a -> b) -> a -> c

Hence for filter isUpper, a = Char and the type is [Char] -> [Char], i.e. it needs to take a string as parameter.

And2:

            (.) :: (b    -> c   ) -> (a   -> b) -> a -> c
        isUpper :: (Char -> Bool)
           head ::                   ([b] -> b)
//∴ c = Bool, b = Char, a = [b] = [Char], and
 isUpper . head ::                                 a -> c
                =                             [Char] -> Bool

Thus for filter (isUpper . head), we have a = [Char] and the type is [[Char]] -> [[Char]], i.e. it needs to take a string list as parameter.


Note:

  1. The type of isInfixOf is actually (Eq a) => [a] -> [a] -> Bool as the equality must be valid for type a, but this is irrelevant in our analysis.
  2. I've temporarily changed the variable a to b for head, but it doesn't matter.
电影里的梦 2024-08-29 19:00:19

字符串实际上只是字符列表,即[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).

菩提树下叶撕阳。 2024-08-29 19:00:19

嗯,这就是多态性。 any 的类型为 (a -> Bool) -> [一]-> Bool 其中类型变量 a 可以是您喜欢的任何内容。在第一种情况下,它是 Char - 因为第二个参数是 Char 列表 - 并且类型变为 (Char -> Bool) ->字符串 -> Bool(请记住,[Char]String 相同!);第二个 aString 并且类型变为 (String -> Bool) ->; [字符串] ->布尔。

filter 的推理类似。

Well, that's polymorphism. The type of any is (a -> Bool) -> [a] -> Bool where the type variable a can be anything you like. In the first case it's Char - because the second argument is a list of Chars - and the type becomes (Char -> Bool) -> String -> Bool (remember that [Char] is the same as String!); in the second a is String and the type becomes (String -> Bool) -> [String] -> Bool.

The reasoning for filter is similar.

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