Haskell: Function to determine the arity of functions?

后端 未结 6 618
一个人的身影
一个人的身影 2021-02-05 07:00

Is it possible to write a function arity :: a -> Integer to determine the arity of arbitrary functions, such that

> arity map
2
> arity fol         


        
6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-05 07:08

    Yes, it can be done very, very easily:

    arity :: (a -> b) -> Int
    arity = const 1
    

    Rationale: If it is a function, you can apply it to exactly 1 argument. Note that haskell syntax makes it impossible to apply to 0, 2 or more arguments as f a b is really (f a) b, i.e. not f applied to a and b, but (f applied to a) applied to b. The result may, of course, be another function that can be applied again, and so forth.

    Sounds stupid, but is nothing but the truth.

提交回复
热议问题