# Return destinations for an expression-oriented programming language

I am designing a programming language and I want to give everything the ability to return a value. For example, if I use a block as a right-hand side value, I can assign it to a lvalue:

sum := {
if (a < 0) { a := -a; }
if (b < 0) { b := -b; }
return a + b;
};


Also, if and while statements could return a value, and there is no need for something like a ternary operator then.

After a bit of research, I have found that Rust is capable of doing this. And this is great for language design, because it significantly reduces the number of language constructs needed. You don't really need a do-while loop, as that's just a while loop with a braced condition:

do {
foo();
} while (a > 0);


Is equivalent to:

while {
foo();
return a > 0;
}{}


Also, with ignored variable bindings, you can create continue and break statements for loops:

// Break
_ := while (a > 0) {
if (a == 5) {
return ();  // Empty tuple
}
a += 1;
};
// Continue
while (a > 0) { _ := {
if (a == 5) {
return ();  // Empty tuple
}
a += 1;
};}


I like the idea of having less language constructs, because the language is expressive enough to do this without labeling and goto statements.

1. Are these just very abstract lambda expressions?
2. How could the 'return destination' be labeled so we can return from a very nested expression even right out to the function? I think I would probably need some extra notation for that.
• "I like the idea of having less language constructs" -- trade-offs abound! It doesn't take much to be Turing complete; most language features are "only" sugar! – Raphael Jun 20 '17 at 13:06
• "Is there a language that has already done something similar?" -- I think that you should remove this since it's offtopic here. That said, IIRC every Ruby statement "returns a value" in the sense that every statement is an expression. – Raphael Jun 20 '17 at 13:07
• @Raphael Sorry, I've removed that. I also know that most of the things are just syntax sugar, but I like to give programmers a different set of building blocks, not the style extremes, like lambda calculus or assembly. – Peter Lenkefi Jun 20 '17 at 13:14
• @Raphael: I don't like that questions related to programming languages are considered off-topic. As someone doing research in programming languages, it is really annoying. "Is there a language that has already done something similar?" is akin to "Is there any prior research on this topic?" – Dave Clarke Jun 20 '17 at 13:20
• @DaveClarke I think that questions related to PLs are on-topic here, as long as the are about general concepts of PL design/implementation/analysis/etc. (as the tag itself states). Questions about specific PLs like "how does this work in C++" are off-topic, and SO material. Questions like "was this done before in some PL" sound general enough for me, and I would consider them on-topic (?) -- I think I agree with you on this. – chi Jun 20 '17 at 13:29

2) You should read about continuation passing style. Scheme, for instance, uses "call with current continuation" call/cc to achieve what you describe: "returning" from a deeply nested location.
call/cc is probably not the most convenient notation, but it works, and can be used with any expression. E.g.
(call/cc (lambda (k)

Above, if the condition is true, the if evaluates to 10, which is added to 1, so that the whole result is 11. If it is false, (k 2) is evaluated, "returning" to the point call/cc was used with result 2. Note that this skips the (+ 1 ..) part. You can nest call/cc arbitrarily, providing many "return points" as wanted.