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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Solving LeetCode problems with Racket: I don't know what I expected

3 点作者 TwentyPosts6 个月前

1 comment

gus_massa6 个月前
&gt; <i>You might think “So what’s the big deal? It’s not statically typed, just runtime-enforced contracts.” and you’re completely right. That said, the trick is going to come in handy soon.</i><p>If you use typed&#x2F;racket, they compiler checks a lot of them at compilation time, and even use them to eliminate implicit types checks.<p><pre><code> &gt; (let ([huge-list (range 1e7)]) &gt; (begin &gt; (displayln (current-process-milliseconds)) &gt; (first (map add1 huge-list)) &gt; (displayln (current-process-milliseconds)) &gt; ) &gt; ) </code></pre> There is some magic when you use in-range inside map like:<p><pre><code> &gt; (first (map add1 (in-range 1e7))) </code></pre> It would be nice if the compiler can automatically fix this examplet, but it has to be an incredible smart compiler.<p>&gt; <i>Let’s take a look at how the Racket standard library implements this macro (this is what proper Racket formatting looks like, apparently).</i><p>A lot of Racket is written in a simplified version of Racket informally call #&#x27;kernel. The problem is that #&#x27;kernel is very difficult to use because it has only the minimal stuff. One of the first definitions is `and`, so that explains the ugly definition. After a few modules that add more and more friendly tools, it&#x27;s possible to define `and` as<p><pre><code> (define-syntax (my-and stx) (syntax-case stx () [(_) #&#x27;#t] [(_ x) #&#x27;x] [(_ x y ...) #&#x27;(if x (my-and y ...) #f)]))</code></pre>