3 ms·
Functional languages do have statements. a : Int -> Int a x = x + 2 both `:` and `=` are statements, not expressions.
by gnulinux 6y ago
Functional languages do have statements.
a : Int -> Int
a x = x + 2
both `:` and `=` are statements, not expressions.
- fiddlerwoaroof 6y agoThis is sometimes true, the `=` equivalent in Clojure is an expression, though: user=> (def a (def b 2)) user=> a #'b Lisps generally only have expressions (every form returns a value.
- gnulinux 6y agoLisp runtime implicitly runs each top-level expression as statement. When you run (def x y) it's not only true that this expression returns 'x, it's also true that it alters the static state of the program by adding 'x to the scope. To make a purely statementless language, I think you need to make everything happen in a single monadic computation. E.g. in pseudo-Haskell-ish: main : Scope -> StaticIO Scope main scope0 = do scope1, x' <- def scope0 "x" Int scope2, xv <- dec scope1 "x" 3 But the main part is still statement.
- tsimionescu 6y agoAt least in all of the discussions I have seen, the difference between statements and expressions is whether they return a value, not whether they have side-effects. So (def x b) is an expression, because it returns a value. {} in C is a statement, even though it has no side effects. Now, there are some statements in CL as far as I remember - I believe (declaim x integer) is a kind of statement, at least in the sense above. But you can write a CL program that doesn't use a single statement.
- fiddlerwoaroof 6y agoThe return value of some forms is implementation dependent, but all forms return a value in CL: CL-USER(1): (defparameter *foo* (declaim (special x))) *FOO* CL-USER(2): *foo* (X) CL-USER(3): (defparameter *bar* (declaim (type integer x))) *BAR* CL-USER(4): *bar* (X) In other cases, e.g. `(values)` which is an expression that returns no values, the value is nil
- fanf2 6y agoThey are usually called declarations rather than statements, I think.
- deleted 6y ago[deleted]
- jhgb 6y agoHow do any of those qualify as statements? Statements represent actions in imperative programming languages. Declarations and definitions are not actions.