Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This is a great project!

I believe there's a lack of clarity in the let form explanation:

  =>(let [object "light"]
  =>  (let [object "darkness"])
  =>  (println (str "God said let there be " object)))
  God said let there be light
  nil
'object' is overwritten but only within the second let form:

  =>(let [object "light"]
  =>  (let [object "darkness"]
  =>    (println (str "God said let there be " object))))
  God said let there be darkness
  nil
or even:

  =>(let [object "light"]
  =>  (println (str "God said let there be " object))
  =>  (let [object "darkness"]
  =>    (println (str "God said let there be " object))))
  God said let there be light
  God said let there be darkness
  nil
The reason for this is the let form uses lexical scoping. In other words it creates a new frame which includes the bindings declared in the square brackets. When 'object' is evaluated its binding is looked for in the current frame, then the frame that called that frame and all the way out into the global namespace stopping when a binding is found.

See also: http://stackoverflow.com/questions/1774417/scoping-rules-in-...



This would be better for illustrating that object is not overweritten, I think:

  =>(let [object "light"]
  =>  (let [object "darkness"]
  =>    (println (str "God said let there be " object)))
  =>  (println (str "God said let there be " object)))
  God said let there be darkness
  God said let there be light
  nil


Thanks for this. I added your example with slightly different form!


Thanks for your feedback! I think I could make it better.

http://kimh.github.io/clojure-by-example/#scope


Thank you!

I agree with fnordsensei's comment about ordering. You might also think about including this example from the docs which demonstrates bindings are immediately available:

    (let [x 1
          y x]
      y)
    -> 1




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: