How to write the type declaration of an haskell function without arguments?
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.
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.
Since functions in Haskell are pure (their result only depends on their arguments), the equivalent of a function with no arguments is just a value. For example, one = 1
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With