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 :-).
Because
isUpperis aChar -> Boolfunction and"1" ‘isInfixOf‘andisUpper . headare[Char] -> Boolfunctionscan be rewritten as
We knew the type of
isInfixOfis[a] -> [a] -> Bool1. Now the first argument toisInfixOfis"1"which is of type[Char], so we can deduceais aChar:That means
isInfixOf "1"is now a[Char] -> Boolfunction.Now, the type of
anyis(a -> Bool) -> [a] -> Boolfunction. 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 ofisUpperisChar -> Bool. Hence:So
any isUpperneeds 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 = Charand 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:
isInfixOfis actually(Eq a) => [a] -> [a] -> Boolas the equality must be valid for typea, but this is irrelevant in our analysis.atobforhead, but it doesn’t matter.