Practical use of curried functions?

后端 未结 10 859
粉色の甜心
粉色の甜心 2020-12-14 07:01

There are tons of tutorials on how to curry functions, and as many questions here at stackoverflow. However, after reading The Little Schemer, several books, tutorials, blog

10条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-14 07:16

    They can make code easier to read. Consider the following two Haskell snippets:

    lengths :: [[a]] -> [Int]
    lengths xs = map length xs
    
    lengths' :: [[a]] -> [Int]
    lengths' = map length
    

    Why give a name to a variable you're not going to use?

    Curried functions also help in situations like this:

    doubleAndSum ys = map (\xs -> sum (map (*2) xs) ys
    
    doubleAndSum' = map (sum . map (*2))
    

    Removing those extra variables makes the code easier to read and there's no need for you to mentally keep clear what xs is and what ys is.

    HTH.

提交回复
热议问题