Search code examples
compiler-constructionschemeexpression-evaluationlanguage-implementationchez-scheme

In Scheme, what is the returned value of `(begin)`?


I know that (begin expr1 expr2 ...) will evaluate all of the expressions and will return the last one evaluated.

I found that in Chez Scheme it's allowed to use begin without expressions like so: (begin). I'm using Chez Scheme as part of my studies. When I write in the console (begin) it does not send a syntax error it just displays nothing as if I was getting void.

My question is, what will be returned then?

I thought it was the void object that you can get by running (void). However, when I tested it I realized it is not.

I'm studying Chez Scheme as part of a compiler course I'm taking and I need to implement a part of a compiler that can handle the special word begin. So I need to know why is this even possible and what this function returns.

EDIT:

I'm adding this link regarding this topic. I wasn't able to fully understand the answer. Plus, he hasn't mentioned the return type.


Solution

  • I think the question is confusing different uses of begin. If you look at R6RS section 11.4.7 about begin, it is used in two different ways: 1) with 0 or more "forms", in which case the forms are syntactically "spliced" into the surrounding body just like if the begin wasn't there (I believe this is mainly useful when writing macros); and 2) with 1 or more expressions, in which case it evaluates the expressions in sequence and returns the result of the last one.

    When you talk about "return value", you are talking about (begin ...) as an expression, but as an expression (the second form of begin above), it must take one or more expressions. An empty (begin) must be the first form of begin, which simply splices "nothing" (0 forms) into the surrounding body, but it isn't "evaluated" separately from the evaluation of the body as a whole.