In eager languages like Scheme and Python, you can use a lambda expression without parameters to delay evaluation, e.g. in Scheme (Chicken Scheme):

#;1> (define (make-thunk x) (lambda () (+ x 1)))
#;2> (define t (make-thunk 1))
#;3> (t)
2

In line 2, t is bound to the unevaluated expression (lambda () (+ 1 1)), which is then evaluated to 2 in line 3.

Similarly, in Python:

>>> def make_thunk(x): return lambda: x + 1
... 
>>> t = make_thunk(1)
>>> t()
2

Using this technique one can implement lazy evaluation in an eager language.

So, I was expecting that Haskell would not have lambda expressions without parameters because the language is already lazy and there is no need to build delayed expressions. To my surprise, I found out that in Haskell it is possible to write the lambda expression

\() -> "s"

which can only be applied to the () value like so:

(\() -> "s") ()

giving the result

"s"

Applying this function to any argument other than () throws an exception (at least as far as I could see during my tests). This seems different from delayed evaluation in Scheme and Python, because the expression still needs an argument to be evaluated. So what does a lambda expression without variables (like \() -> "s") mean in Haskell and what can it be useful for?

Also, I would be curious to know if similar parameterless lambda expressions exist in (some variety of) lambda-calculus.

有帮助吗?

解决方案

Well, the other answers cover what \() -> "something" means in Haskell: an unary function that takes () as argument.

  • What is a function without arguments? – A value. Actually, it can occasionally be useful to think of variables as nullary functions that evaluate to their value. The let-syntax for a function without arguments (which doesn't actually exist) ends up giving you a variable binding: let x = 42 in ...

  • Does lambda calculus have nullary functions? – No. Every function takes exactly one argument. However, this argument may be a list, or the function may return another function that takes the next argument. Haskell prefers the latter solution, so that a b c is actually two function calls ((a b) c). To simulate nullary functions, you have to pass some unused placeholder value.

其他提示

You're misinterpreting what () means in Haskell. It isn't the lack of a value, it is rather the only value of the Unit type (the type itself being referred to by an empty set of parentheses ()).

Since lambdas can be constructed to use pattern matching, the lambda expression \() -> "s" is explicitly saying "create an anonymous function, expecting an input that matches the () pattern". There isn't much point to doing it, but it's certainly allowed.

You can use pattern matching with lambdas in other ways as well, for example:

map (\(a, b) -> a + b) [(1,2), (3,4), (5,6)] -- uses pattern matching to destructured tuples

map (\(Name first _) -> first) [Name "John" "Smith", Name "Jane" "Doe"] -- matches a "Name" data type and its first field

map (\(x:_) -> x) [[1,2,3], [4,5,6]] -- matches the head of a list

() -> "s" is a lambda function which takes one argument:

ghci> :t (\() -> "s")
(\() -> "s") :: () -> [Char]

(), the empty tuple (sometimes known as Unit), is a fully-fledged type in Haskell. It only has one member (ignoring _|_*), which is also written as (). Look at the definition of ():

data () = ()

This means the form on the left-hand side of your lambda expression is syntactically a pattern-match which matches (), the only member of the type (). There's only one valid way to call it (which is to supply () as an argument) because there's only one valid member of the type ().

Such a function is not very useful in Haskell, because by default terms are lazily evaluated anyway, as you observed in your question.

For a much more detailed explanation, see this answer.

*_|_ is called "bottom" or "undefined". It always type-checks, but it crashes your program. The classic example of how to get a _|_ is let x = x in x.

许可以下: CC-BY-SA归因
scroll top