Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you return nothing from a function in Scheme?

Tags:

lisp

scheme

I'm writing a scheme interpreter, and in the case of an if statement such as:

(if (< 1 0) 'true)

Any interpreter I've tried just returns a new prompt. But when I coded this, I had an if for whether there was an alternative expression. What can I return in the if such that nothing gets printed?

(if (has-alternative if-expr)
  (eval (alternative if-expr))
  #f) ;; what do I return here?
like image 506
Kai Avatar asked Mar 19 '09 22:03

Kai


2 Answers

According to the R6RS specification:

If <test> yields #f and no <alternate> is specified, then the result of the expression is unspecified.

So go wild, return anything you want! Although #f or '() are what I, personally, would expect.

like image 53
Andrey Fedorov Avatar answered Oct 04 '22 11:10

Andrey Fedorov


Scheme can indeed return no values:

   > (values)

In R5RS the one-armed form of if is specified to return an unspecified value. That means it is up to you, to decide which value to return. Quite a few Schemes have chosen to introduce a specific value called "the unspecified value" and returns that value. Others return "the invisible value" #<void> and the REPL is written such that it doesn't print it.

   > (void)

At first one might think, this is the same as (values), but note the difference:

  > (length (list (void)))
  1

  > (length (list (values)))
  error>  context expected 1 value, received 0 values
  (Here (list ...) expected 1 value, but received nothing)

If #<void> is part of a list, it is printed:

  > (list (void))
  (#<void>)
like image 41
soegaard Avatar answered Oct 04 '22 12:10

soegaard