Why does foldr function with three arguments?
I looked at some list operations and came across !!
:
(!!) :: [a] -> Int -> a
xs !! n
| n < 0 = negIndex
| otherwise = foldr (\x r k -> case k of
0 -> x
_ -> r (k-1)) tooLarge xs n
A function (\x r k -> ...)
has a type a -> (Int -> a) -> Int -> a
, but it foldr
accepts a function that must take only two arguments:
foldr :: (a -> b -> b) -> b -> [a] -> b
foldr k z = go
where
go [] = z
go (y:ys) = y `k` go ys
Can someone explain to me why it foldr
takes a function that takes 3 arguments with the following type a -> (Int -> a) -> Int -> a
? Moreover, the result must be of the same type as the second argument?
+3
source to share