2017-09-18 번역서에 있는글이 있어서 올린다.

표현식과 문장 모두 프로그램에서 작성하는 명령어들이며, 컴퓨터로 하여금 특정 일을 수행하도록 명령한다.

차이점은 

문장은 액션을 수행하지만 아무것도 리턴하지 않고

표현식은 액션을 수행하고 결과를 리턴한다. 람다 표현식을 사용하여 프로그래밍할 때 문장보다는 표현식을 생성하여 사용하는것이 낫다.

이유?

1. 문장은 아무것도 리턴하지 않기 때문에 사이드 이펙트를 발생하거나 메모리를 이상한값으로 채울 수 있다.

2. 문장과는 달리 표현식은 두개이상의 표현식을 하나로 조합하여 사용할 수 있음.



from http://lambda-the-ultimate.org/node/1044

Expressions have a value, while statements do not. If you can pass it as an argument to a function, it's an expression. If you can't, it's a statement. Control flow constructs in C-derived languages are generally statements (you can't pass an 'if {}' block as a function parameter). Consequently, those languages tend to provide an expression form for 'if' (like the ternary operator). Of course, the whole point of many functional languages is that everything has a value, and that functions are first-class values, which means that everything is an expression. Statements usually appear in imperative languages, where you wish to write commands that don't necessarily return a value.

구문이 값을 가지지 않을때 표현식은 값을 가진다. 만약 함수에게 아규먼트로 넘겨줄수 있다면 그건 표현식이다. 못한다면? 그건 구문이다. 제어흐름 기반의 C 파생언어들은 보통 구문( 함수파라미터 같은 'if {}' 블럭은 넘기지 못한다.) 이다. 따라서 저러한 언어들은 'if' (삼항연산같은 )를 위해 표현식 폼을 제공하는 경향이 있습니다. 물론 많은 함수언어의 중요 포인트는 모든것은 값을 가지고 함수는 일급함수 값(즉 모든것은 표현식이다)이라는 것이다. 구문은 보통 리턴값이 필요 없는 명령어를 코딩하는 명령형 언어에서 나타난다.


구문( 'if {}' ..조건이 아닌 전체 인..블럭은 함수의 아규먼트로 못넘김) 

함수파라미터처럼 if{} 블럭은 넘기지 못한다 == 구문 


from http://python.xwmooc.org/html/book004.html

3.6  표현식(Expression)

표현식 (expression)은 값, 변수, 연산자 조합니다. 값은 자체로 표현식이고, 변수도 동일하다. 따라서 다음 표현식은 모두 적합하다. (변수 x는 사전에 어떤 값이 대입되었다고 가정한다.)

17
x
x + 17



3.4  문장(Statement)

문장(statement)은 파이썬 인터프리터가 실행하는 코드 단위다. 지금까지 print, assignment 두 종류의 문장을 살펴봤습니다.

print 1
x = 2
print x



Expressions(표현식) 은 이 있고 statements(구문) 은 그렇지 않다. 

function 에게 arguments로 넘겨줄수 있는것이라면 그건 표현식이다. 그럴수 없다면? 구문이다. 

구문도 표현식처럼 값을 줄 수 있는데 그게바로 return문을 이용할때이다!


이게 가장 쉬운 설명인것 같다.

구문은 if {} 이런게 구문이다. 이건 값으로 넘겨줄 수 있는.. 어떤 값이 아니다. 


Statements are commands, expressions are requests

Statements are commands, i.e. 'do this', 'open file', 'set collection size to 1', 'clear collection' etc.

Expressions are requests: 'calculate this', 'give me the contents of this file', 'give me a collection with size 1', 'give me a new collection'.

Superficially, they are both commands, in the sense that expressions are commands to perform calculations. But the real difference lies inside statements and expressions:

-In statements, the programmer tells the computer where to store the results of computations.
: 구문은 프로그래머가 컴퓨터에게 계산의 결과를 어디에 저장할지 말하는것이고...

-In expressions, the programmer does not tell the computer where to store the result of computations, but takes the result in his/her hand, and passes it to other expressions.
: 표현식은 프로그래머가 컴퓨터에게 계산의 결과를 어디에 저장할지 말하지 않는것이다. 그러나 그 결과는 다른 표현식에 전달된다.

The problem created by statements is that the programmer often forgets the right place to store the result of computations, and therefore uses a wrong place, thus creating havoc with a program; whereas in expressions the store does not matter, because the result is to be immediately used in a nearby expression.

It's a very subtle difference, but once someone understands it, he/she automatically understands the real value of functional programming.



'IT > java' 카테고리의 다른 글

map 의 .merge ( java 8 lambda) 값 증가 를 간단히  (0) 2016.09.06
json <-> map  (0) 2016.08.17
java 8 tutorial 한글  (2) 2014.04.09
잘못쓰면 큰일나는 loop 과 객체선언 ... 그리고 성능저하  (0) 2013.09.26
call by ref ? value?  (0) 2013.06.21

+ Recent posts