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.

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

3 pointsby TwentyPosts6 months ago

1 comment

gus_massa6 months ago
&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>