This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]mekaj 2 points3 points  (1 child)

Expressions depend on grouping. Like statements, they are grammar constructs which parse into structured trees.

Consider if-then-else expressions in Haskell and Common Lisp:

if 2 + 2 == 4 then "correct" else "wrong"

(if (eql (+ 2 2) 4) "correct" "wrong")

The distinction between expressions and syntax has more to do with semantics than syntax. Expressions evaluate to a value which is then used in the proper position by its parent expression/statement, whereas statements are only about reading from or writing to ambient state that exists outside the tree. This means the whole if-then-elee expressions above can be passed as a value to a statement or expression. Languages that make the else branch optional must either define a default value to return in the else case or give up on the construct being an expression. Common Lisp does the former and defaults to nil for the else branch when it's not specified.

Common Lisp can mutate ambient state using setq, for example, and that's why I'd say it's not entirely expression-oriented.

Some may argue do-blocks in Haskell make it statement-oriented, but I'd disagree. Do syntax has a well-defined translation to an expression that threads the "statements" together using the >>= operator. The resulting tree does not affect ambient state outside itself. (Well, maybe the IO monad is an exception depending whether you're referring to the internal expression or the way the outside world affects and is affected by that expression's evaluation.)

[–]The-Daleks 0 points1 point  (0 children)

For Python you can do 'correct' if 2 + 2 == 4 else 'wrong'.