How to write the type declaration of an Haskell function with no arguments?

haskell

Solution

There is no such thing as a function without arguments, that would be just a value. Sure, you can write such a declaration:

five :: Int
five = 5

It might look more like what you asked for if I make it

five' :: () -> Int
five' () = 5

but that's completely equivalent (unless you write something ridiculous like `five' undefined`) and superfluent1.

If what you mean is something like, in C

void scream() {
  printf("Aaaah!\n");
}

then that's again not a function but an action. (C programmers do call it function, but you might better say procedure, everybody would understand.) What I said above holds pretty much the same way, you'd use

scream :: IO()
scream = putStrLn "Aaaah!"

Note that the empty `()` do in this case not have anything to do with not having arguments (that follows already from the absence of `->` arrows), instead it means there is also no return value, it's just a "side-effect-only" action.

1Actually, it differs in one relevant way: `five` is a constant applicative form, which sort of means it's memoised. If I had defined such a constant in some roundabout way (e.g. `sum $ 5 : replicate 1000000 0`) then the lengthy calculation would be carried out only once, even if `five` is evaluated multiple times during a program run. OTOH, wherever you would have written out `five' ()`, the calculation would have been done anew.

Problem

How to write the type declaration of an haskell function without arguments?

Original source