1) (def x (for [i (range 1 3)] (do (println i) i)))
2) (def x (for [i (range 1 3)] (do i)))
Both produces same output, then what is the use of println?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Values evaluate to themselves, and a do block returns the result of the last expression. That is why (do 2) returns 2, in the example below. Because it returns 2, when evalled from a REPL, 2 will be printed to the screen as the result.
The function println however causes a side effect, but returns nil. The side effect below is that the value 2 will be printed to standard output. Also nil is printed, because that is the return value from the function println.
In your example the outputs are not the same, because of what I explained above. Notice the difference yourself:
Also take note that for is lazy:
The second time x is requested, only (1 2) is printed. Why? Because for is lazy. It only produces its elements the first time they are requested (the first time x is requested). A next time, the elements are already produced, so the side effect within do will not happen again.