TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Lexical vs Dynamic scope in clojure

61 点作者 chrismcbride超过 13 年前

3 条评论

kyleburton超过 13 年前
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 未加载
sharkbot超过 13 年前
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>
draegtun超过 13 年前
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>