4 ms·
Statements are orders, expressions are questions.
by FeepingCreature 4y ago
Statements are orders, expressions are questions.
- SeanLuke 4y agoAsk yourself why Lisp has no statements. Is it impossible to issue orders to the Lisp runtime?
- FeepingCreature 4y agoLisp does have statements, such as `setf` or `loop`. Just because you also attach an expression to it because hey, gotta evaluate to something, doesn't mean it's not fundamentally a statement. Sure, syntactically it's an expression-based language, but some of those are expressions in syntax only. If a function terminates in nil, what exactly did it compute? And what exactly is the point of all but the last argument to a `progn`? An expression that is evaluated for the side effects, not the result value, is a statement in every way that matters. (Haskell has a stronger claim because it actually captures the side effects as a value.)
- SeanLuke 4y ago> Lisp does have statements, such as `setf` or `loop`. Just because you also attach an expression to it because hey, gotta evaluate to something, doesn't mean it's not fundamentally a statement. I think the general consensus is that expressions evaluate to values in unexceptional situations, full stop. Whether they have side effects is immaterial. Whether the value returned is useful or not is also immaterial. Perhaps your examples weren't the best, since both setf and loop evaluate to values which are often useful. This isn't just Lisp. For example, the C99 specification explicitly declares that all function calls are expressions, even if they return void. Wikipedia also defines an expression (vs. statement) unambiguously as "a syntactic entity in a programming language that may be evaluated to determine its value.... Expression is often contrasted with statement—a syntactic entity that has no value (an instruction)." https://en.wikipedia.org/wiki/Expression_(computer_science) https://en.wikipedia.org/wiki/Expression_(computer_science)
- FeepingCreature 4y agoI think you're mixing up syntax and semantics. Function calls that return void are usually better classified semantically as procedures, at least when trying to understand how the program is arranged.
- afiori 4y ago> If a function terminates in nil, what exactly did it compute? nil. The fact that you can syntactically put loops and ifs in place of expressions is what makes them expressions
- FeepingCreature 4y agoLotta CPU time spent to get you that nil. One would think you could just `(defun fun () nil)`.
- afiori 4y agoThere seems to be a disagreement about what an expression is. I have the impression that you think that an expression is a way to compute a value, while I would posit that an expressions are a method of combining values. Not really an expression: a0 = 1; a1 = f(2); if (a0 < a1){ a2 = getInput(); } else { a2 = 3; } result = a0 + a1 + a2; More of an expression: result = 1 + f(2) + (a0 < a1 ? getInput() : 3);
- shikoba 4y agoThe answer to a question to be precise.
- nivertech 4y ago> The answer to a question to be precise. No. The expression before evaluation (i.e. as it's written) is a question/query/ask, after a successful evaluation you get a value - an answer. In case when the evaluation failed, you get either a compilation/parsing error, runtime error/exception, crash, infinite loop/no return, or undefined behavior. -- - Commands and Queries (as in CQS[1] and CQRS[2]) - Wishes and Asks (it's a wish b/c unlike command it can be rejected, and for the same reason it's an ask and not a query - i.e. "you may ask, but it doesn't mean I'll answer"). - Truth or dare?[3] (Dares and Truth-es ;) -- [1] https://en.wikipedia.org/wiki/Command%E2%80%93query_separation https://en.wikipedia.org/wiki/Command%E2%80%93query_separati... [2] https://martinfowler.com/bliki/CQRS.html https://martinfowler.com/bliki/CQRS.html [3] https://en.wikipedia.org/wiki/Truth_or_dare%3F https://en.wikipedia.org/wiki/Truth_or_dare%3F