Hacker News new | ask | show | jobs
by parenthephobia 2925 days ago
It's impossible to pass around functions without arguments in Haskell, because there are no functions without arguments in Haskell. It looks like the same is true of Gluon. (So you don't really pass around the function name: you pass around the variable which contains the result of evaluating the expression.)

In languages where there can be functions with no arguments, if referring to the function without parentheses calls it, it can be inconvenient to get a reference to the function itself.

2 comments

In both Gluon and Haskell, functions without arguments can be represented as functions over the unit type:

  f: () -> SomeType
which can be called via:

  f ()
There are examples of this in the Gluon book (http://gluon-lang.org/book/syntax-and-semantics.html). There are no syntax-related difficulties at all here. This is also how it's done in OCaml and related languages.

(Note: This is (basically) useless in Haskell, since laziness makes this have the same semantics as a constant:

  f :: SomeType
But since Gluon is strict, there's a pretty important difference between the two.)
getChar is a Haskell function without arguments.
In Haskell, getChar isn't a function (something of type a -> b), its type is IO Char

http://hackage.haskell.org/package/base-4.11.1.0/docs/Prelud...

It represents an interaction with the outside world that results in a Char when you perform it. It isn't a function because in Haskell functions must be pure (with no side effects).

This is false. getChar is a constant.
I am confused :S

From your other post: "In both Gluon and Haskell, functions without arguments can be represented as functions over the unit type: f: () -> SomeType"

Isn't `f` a constant here too?, how is this different than getChar? (I get I don't understand something here but not sure what)

`getChar` is not of type `() -> SomeType`, but directly of type `SomeType`.

(Though yes, `f` also is a constant... it's just a constant that happens to be a function, which `getChar` is not.)

I feel the responses to you were somewhat unhelpful. Would they also claim that something of type `MyFun Char` is not a function, where

    data MyFun a = MyFun (() -> Char)
Technically they'd be right.