TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Lexical vs Dynamic scope in clojure

61 pointsby chrismcbrideover 13 years ago

3 comments

kyleburtonover 13 years ago
I think the article is a great explanation. I'm not sure I (exactly) agree with the conclusion "dynamic vars also breaks referential transparency" - isn't it the combination of lexical closure (referring to symbols from outside the function) as well as mutable state that breaks referential transparency?<p>If I have a function that takes a java collection and returns the count, it has no referential transparency because the collection is mutable, not necessarily b/c of how it's operating on its arguments.<p>Of course this is one of the things I love about Clojure and Rich Hickey's use of immutability as the default behavior (as often as possible) - much [more] of the Clojure I write has referential transparency, hardly any of the Java I wrote did.<p>Thanks!<p>Kyle
评论 #3454681 未加载
评论 #3454462 未加载
sharkbotover 13 years ago
The GHC Haskell compiler also supports dynamically scoped parameters via the XImplicitParams switch [1]. Type constraints are used to indicate which variables are propagated to the function.<p>I've used implicit params in my toy compiler code. While it somewhat simplified the structure of the code, I could have easily written the program without them.<p>[1] <a href="http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#implicit-parameters" rel="nofollow">http://www.haskell.org/ghc/docs/latest/html/users_guide/othe...</a>
draegtunover 13 years ago
For those interested in how Perl does dynamic scoping here is dynamic2.clj in perl5:<p><pre><code> use 5.014; use warnings; our ($x, $y) = (2, 2); sub sum_of_squares { my ($a, $b) = @_; ($a * $a) + ($b * $b); } sub sum_of_squares_for_x_and_y { sum_of_squares $x, $y } say sum_of_squares_for_x_and_y; # =&#62; 8 { local ($x, $y) = (10, 5); no warnings 'redefine'; local *sum_of_squares = sub { my ($a, $b) = @_; $a / $b; }; say sum_of_squares_for_x_and_y; # =&#62; 2 } say sum_of_squares_for_x_and_y; # =&#62; 8 </code></pre> PS. I seem to be making a lot of HN comments on dynamic scoping lately :)<p><a href="http://news.ycombinator.com/item?id=3446384" rel="nofollow">http://news.ycombinator.com/item?id=3446384</a> | <a href="http://news.ycombinator.com/item?id=3423631" rel="nofollow">http://news.ycombinator.com/item?id=3423631</a>