Principles, Best Practices and Design Patterns for functional programming

后端 未结 7 1497
我在风中等你
我在风中等你 2021-01-31 09:56

Are there any known principles, best-practices and design patterns that one can follow while writing code in a functional programming language?

7条回答
  •  囚心锁ツ
    2021-01-31 10:48

    Design pattern: let the compiler infer types for your functions, and make sure those types are exactly as general as you expect. If the types are more polymorphic or less polymorphic, figure out why.

    For example, if you are writing a sort function in Haskell, expect

    Ord a => [a] -> [a]
    

    If your type is

    Num a => [a] -> [a]
    

    or

    [a] -> b
    

    then something is horribly wrong.

    Best practice: once you've confirmed with the compiler that the type is as you expect, put an explicit type signature for every top-level function. (Or if you are using ML or Caml, write an explicit interface.) Set compiler options so that values with missing signatures trigger an error.

提交回复
热议问题