Haskell and the definition of function types. a couple of questions

if I do any isUpper "asBsd", I will receive True.
here the second element anyis a string.
but if I do this:

any ("1" `isInfixOf`) ["qas","123","=-0"]

the second element in anyis a list of strings.
how and why is the difference between these two functions?

another example.
if I write filter isUpper "asdVdf", I will receive "V".
here the second element to filter is the string.
but if I write this:
filter (isUpper . head) ["abc","Vdh","12"]I will receive ["Vdh"].
as you can see, the second element to filter is now a list of strings.
why are there differences and how haskell knows this correctly in both cases?

to summarize:
I don’t understand how in the same function one time haskell receives the second element, which is a string, and at another time haskell receives a list of lines in the second element.
once it happened in a function any, and at another time in a function filter.
how haskell (and i) know this correctly in both cases?

thank: -).

+3
source share
3 answers

Because isUpper- this is a function Char -> Bool, "1" ‘isInfixOf‘and isUpper . head- [Char] -> Boolfunctions


"1" `isInfixOf` xxx

can be rewritten as

isInfixOf "1" xxx

, isInfixOf [a] -> [a] -> Bool 1. isInfixOf "1", [Char], a Char:

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

, isInfixOf "1" [Char] -> Bool.

any - (a -> Bool) -> [a] -> Bool. ,

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

any (isInfixOf "1" ):: [a] → Bool                       = [[ Char]] → Bool

any (isInfixOf "1"), .


isUpper. isUpper - Char -> Bool. :

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

, any isUpper , .


, isUpper . head. Haskell :

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

, filter isUpper, a = 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]], .. .


:

  • isInfixOf (Eq a) => [a] -> [a] -> Bool, a, .
  • a b head, .
+11

, [Char]. , , "" ['h', 'e', 'l', 'l', 'o']. -, - Char, - ( , ).

+5

, . any - (a -> Bool) -> [a] -> Bool, a , . Char - Char - (Char -> Bool) -> String -> Bool (, [Char] String!); a String, (String -> Bool) -> [String] -> Bool.

filter .

+4

Source: https://habr.com/ru/post/1734039/


All Articles