Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

P.S. Again sorry for this type of questions.

like image 310
Dimitar Avatar asked Jan 08 '23 07:01

Dimitar


2 Answers

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.

like image 111
Ben Avatar answered Jan 15 '23 18:01

Ben


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.

like image 24
ankh-morpork Avatar answered Jan 15 '23 18:01

ankh-morpork