Skip to content

Instantly share code, notes, and snippets.

@tamzinblake
Created June 16, 2011 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamzinblake/1029665 to your computer and use it in GitHub Desktop.
Save tamzinblake/1029665 to your computer and use it in GitHub Desktop.
perl quantum weirdness explained
;;;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