简体   繁体   中英

Haskell function definition and arguments list

I have the following function definitions (which are equivalent):

reverseWords :: String -> String
reverseWords wds = unwords . map reverse . words $ wds

and

reverseWords :: String -> String
reverseWords = unwords . map reverse . words

I understand that they are equivalent as they yield the same result but I'm a bit confused about the second form.

If I call a function like:

reverseWords abc

How does haskell decide where to place this 'abc' parameter?

Haskell does not need to decide where to place abc . When you call reverseWords in your second example it will simply return unwords . map reverse . words unwords . map reverse . words unwords . map reverse . words .

And then abc gets applied to that with which you will end up with the same result as in your first example.

This is also called Point-free style as you can read more about it here: https://wiki.haskell.org/Point-free

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