Skip to content

Commit 97f1eae

Browse files
committed
Improve ex01 explanation of lexical scope
1 parent 1e4d5d7 commit 97f1eae

1 file changed

Lines changed: 22 additions & 18 deletions

File tree

src/clojure_by_example/ex01_fundamentally_functional.clj

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,30 @@
225225

226226

227227
;; "Lexical Scope" in Clojure
228-
;; - Develop an intuition for what "Lexical scope" might mean
229-
;; by reasoning about the following exercises.
228+
;; - Lexical scope guarantees that the reference to a value will be
229+
;; "enclosed" in the scope in which it is being used.
230+
231+
(comment
232+
;; Strict lexical scope greatly simplifies our life, because
233+
;; it allows us to mechanically follow code, and determine
234+
;; where a value originated.
235+
;; - Start at the place of reference of the value.
236+
;; - Then "walk" outwards, until you meet the very first let binding,
237+
;; or arg-list, or def, where the value was bound.
238+
;; - Now you know where the value came from.
239+
;;
240+
;; This also helps reduce our mental burden of inventing
241+
;; new names to refer to things, because we can re-use
242+
;; a name within a limited scope, and be certain that
243+
;; it will not destroy anything with the same name outside
244+
;; the given scope.
245+
)
230246

231247
;; EXERCISE:
232-
;; Mentally evaluate and predict the results; then check.
248+
;; - Develop an intuition for what "Lexical scope" might mean
249+
;; by reasoning about the following exercises.
250+
;;
251+
;; - Mentally evaluate and predict the results; then check.
233252

234253
(def x 42) ; Bind `x` to 42, globally ("top-level" binding)
235254

@@ -293,21 +312,6 @@
293312
((let [x 10] (fn [x] x)) x)
294313

295314

296-
(comment
297-
;; Strict lexical scope greatly simplifies our life, because
298-
;; it allows us to mechanically work out where a value originated.
299-
;; - Start at the place of reference of the value.
300-
;; - Then "walk" outwards, until you meet the very first let binding,
301-
;; or argument list, or def, where the value was bound.
302-
;; - Now you know where the value came from.
303-
;;
304-
;; This also helps reduce our mental burden of inventing new names
305-
;; to refer to things, because we can re-use a name within a
306-
;; limited scope, and be certain that it will not destroy
307-
;; anything with the same name outside the given scope.
308-
)
309-
310-
311315
;; Function "Closure"
312316
;; - This is a way for a function to capture and "close over"
313317
;; any value available at the time the function is defined

0 commit comments

Comments
 (0)