简体   繁体   中英

Haskell Function Definition without ->

What the following function definition / declaration means:

maxCollatz :: (Integer, Integer)

I am confused, because I am not sure what arguments takes and therefore what produces. Because normally there is -> in the function definition. E. g. Int -> Int .

PS Again sorry for this type of questions.

maxCollatz is a pair of integers (Integer, Integer) . It's not a function, it takes no arguments, and isn't called to produce anything; it just is a pair of integers.

The syntax for declaring the types of and then implementing top level declarations in Haskell is syntax for defining values. Functions are values, so they're included in that, but so is everything else.

The line of code is a valid function signature because it is important to understand that a function does not have to take any arguments.

The number of arguments a function takes is referred to as its arity.

In logic, mathematics, and computer science, the arity of a function or operation is the number of arguments or operands the function or operation accepts.

In this case, the function takes 0 arguments and is arity 0. A function with arity 0 is often referred to as a constant or nullary function.


In Python a similar function would look like this

def pair():
    return (1,1)

If you are familiar with Python it is clear that this function takes no arguments and returns a pair of numbers. This is exactly what the function signature you provided describes.

此函数不接受任何参数,因此它只是两个整数的常量元组(或对)。

This is a function signature, not a whole function definition.

And this is a constant, not a "true function" (I've seen it debated). It returns a tuple of 2 integers, but doesn't take anything.

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