Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

haskell

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

like image 311
danza Avatar asked Jan 08 '14 22:01

danza


2 Answers

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.
like image 199
leftaroundabout Avatar answered Oct 24 '22 04:10

leftaroundabout


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.

like image 13
Chuck Avatar answered Oct 24 '22 03:10

Chuck