Created
June 16, 2011 16:46
-
-
Save tamzinblake/1029665 to your computer and use it in GitHub Desktop.
perl quantum weirdness explained
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;This gist refers to the problem referenced at http://www.perlmonks.org/?node_id=369247 | |
;;;written in lispy format for clarity; the following is not necessarily valid lisp | |
;;;m is a variable currently equal to 20 | |
;;;expression to be evaluated: (++m + m++) | |
;;;pretend pre-increment and post-increment exist in lisp - then, this is the expression: | |
(+ (++m) (m++)) ; m = 20 | |
;;; `preincrement' increments the variable and returns the variable | |
(+ m (m++)) ; m = 21 | |
;;; the following line is the magic - replace `postincrement' with `increment, then return original value' | |
(+ m (progn (setq m (1+ m)) 21)) ; m = 21 | |
;;; setq does its job and there's only a return value left | |
(+ m (progn 21)) ; m = 22 | |
;;; return the 21 | |
(+ m 21) ; m = 22 | |
;;; time to evaluate +, so cash out the variable | |
(+ 22 21) ; m = 22 | |
;;; the return value | |
43 ; m = 22 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment