26

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

2
  • 2
    I think you'll need to be clearer about what you mean by a "function without arguments". Do you mean something like a value like 5 of type Int, or something that really has a function type but is defined without any explicit arguments, like f = const 3 ? Commented Jan 8, 2014 at 22:32
  • 2
    f = const 3 has a type signature f :: b -> Integer. It has thus an argument, it just ignores it Commented Jan 8, 2014 at 22:59

2 Answers 2

64

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.

Sign up to request clarification or add additional context in comments.

6 Comments

you can call it action or procedure, but syntactically it is just a function like all the others, and yes, in my case i actually wanted to group some side effects together, and i wanted to know how to declare the expected returned type. thanks
It is syntactically a function in almost every programming language, but not in Haskell. Here, function has a very specific meaning – basically, a lambda abstraction. If something doesn't permit you to apply it to some argument, then it's not a function. Read this blog post by Conal.
So what should you use instead? How can you put a value in scope for every function?
@Max: any value that's declared on the top-level will be in scope for every function, no need to do anything. (And, of course, also for things that aren't functions...)
@Infinity: without having watched the referenced video, I assume they're talking about a point-free definition. You can define a function without explicitly mentioning any parameters, by using composition operators instead. But a function defined this way sure has parameters too, only, they're implicit in the definition. If you will, f = (+1) is just syntactic sugar for f x = x+1: both define a function with one argument, but the former style avoids actually giving it a name.
|
14

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.