简体   繁体   中英

Need help in understanding the function application operator in haskell

This haskell piece of code

map ($ 3) [(4+), (10*), (^2), sqrt]  

gives the output

[7.0,30.0,9.0,1.7320508075688772]  

I know that $ has the lowest precedence and hence the expression to the right of $ is evaluated together. But what I don't understand is how does ($ 3) behave as a function (Because Map takes a function and a list as parameters). I don't understand why each function in the list is applied to 3.

Remember that ($) is actually a function:

($) :: (a -> b) -> a -> b
f $ x = f x

($ 3) is shorthand for \\f -> (f $ 3) . And its type? Well:

3     ::                  Double -- for sake of simplicity
($)   :: (a      -> b) -> a      -> b
($ 3) :: (Double -> b)           -> b

So ($ 3) is a function that takes a function from Double to something and applies that function to 3 . Now, if we use map , we end up with:

map ($ 3) [(4+), (10*), (^2), sqrt]  
 = [($ 3) (4+), ($ 3) (10*), ($ 3)(^2), ($ 3) sqrt]  
 = [(4+) $ 3, (10*) $ 3, (^2) $ 3, sqrt $ 3]  
 = [4 + 3, 10 * 3, 3 ^ 2, sqrt 3]
 = [7, 30, 9, sqrt 3]

Let's first review the type signature of ($) :

ghci>> :t ($)
($) :: (a -> b) -> a -> b

And definition:

($) f x = f x

Or:

f $ x = f x

And above, we have a section where we've created a partially applied version of ($) with the second argument (of type a ) set as 3 . Now, we know that 3 has type Num a => a , so the type signature of our partial application must be Num a => (a -> b) -> b .

Next, let's look at each of the functions in our list, each of which will be an argument to ($ 3). As expected, they are functions and it turns out that their type Num a -> a -> a is actually more constrained than was required (so we're good). Just to be clear, we can look at what one application would entail:

($3) (4+)

Which we can rewrite without the section as:

($) (4+) 3

At which point it's pretty clear from the function definition above how application proceeds.

The last confusing part might be about the type of the list ($3) (4+) evaluates as 7 , rather than 7.0 in the repl. If we recall that lists are homogeneous and notice that one of the functions in the list, sqrt , accepts and returns a floating value, we see that this type enforced for all applications.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM