Understanding "let" expression in LISP -


i extremely new lisp, had previous experience functional programming (haskell, sml). why code returning 14, , not 10 (ie. 1 + 2y + 3 + 1)?

(defvar x 1)  (defun g (z)   (+ x z))  (defun f (y)   (+ (g 1)      (let ((x (+ y 3)))         (g (+ y x)))))  (f 2) 

because used (defvar x 1), declares x global special variable. causes every other later binding of x use dynamic binding: here in (let ((x ....

style & convention in lisp

convention in lisp: use *x* instead of x special variables.

(defvar *x* 1) 

your code is:

(defvar *x* 1)   ; global special variable *x*  (defun g (z)   (+ *x* z))     ; use special variable *x*  (defun f (y)   (+ (g 1)      (let ((x (+ y 3)))    ; lexical binding of x         (g (+ y x)))))     ; use lexical binding of x 

run:

? (f 2) 10 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -