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

Not sure if you just want commiseration and are just having trouble remembering which keyword is which, so I apologize if I'm explaining too much, but for me deciding which to use is more grappling with the difference between dynamic and lexical scoping rather than which concept maps to which keyword, and just as knowing more about a person seems to help me remember their name, so too does knowing more about the function help me remember its name.

What helped me straighten it out was to evaluate the examples here with Trace: http://reference.wolfram.com/mathematica/tutorial/BlocksComp...

  In[1]:= m=i^2
  Out[1]= i^2

  In[2]:= Block[{i=a},i+m]
  Out[2]= a+a^2

  In[3]:= Module[{i=a},i+m]
  Out[3]= a+i^2

  In[4]:= Block[{i=a},i + m] // Trace
  Out[4]= {Block[{i=a},i+m],{i=a,a},{{i,a},{m,i^2,{i,a},a^2},a+a^2},a+a^2}

  In[5]:= Module[{i=a},i+m]//Trace
  Out[5]= {Module[{i=a},i+m],{i$5592=a,a},{{i$5592,a},{m,i^2},a+i^2},a+i^2}
It's a bit dense to read through but with Block, "i" has been assigned the value "a", but still remains in the form "i" throughout the evaluation, and once "m" is replaced with "i^2", the "i" is seen and replaced with "a", acting as normal dynamic scoping.

In Module, however, "i" is replaced by a temporary variable with the form "i$5592", which does not have a definition anywhere in Mathematica, so when "m" is evaluated, "i" is left alone, which is how lexical scoping acts.

I dislike forgetting parts of languages in the ones I use. C# is full of things whose behavior I have to look up, as is javascript, python, and especially while I'm learning them: clojure and cljs.

But what's nice about this language is that these two concepts, Module and Block, while having different meaning, have the same form.



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

Search: