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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Does anybody find this Scheme code readable?

15 点作者 ApplaudPumice大约 9 年前

12 条评论

jsnell大约 9 年前
I don&#x27;t think this is code that someone learning Scheme should be reading, it&#x27;s pretty damn unreadable. Some obvious issues, just starting from the top and continuing for a while:<p>- Defining a new iteration macro FOR that&#x27;s used in just a single function, has a very odd syntax, and that&#x27;s way too generic for uses.<p>- The first parameter to the PICK function (the position) is a pair. That might make sense if those pairs were values that had been passed throughout the program. But actually every place where PICK is called constructs a totally new pair just for that call. It should be split to separate ROW and COLUMN parameters. This would remove a lot of noise at the call sites.<p>- Or alternatively, if the parameter is kept as is, all of this boilerplate: (cons (- (car kpos) 1) (- (cdr kpos) 1)) should be replaced by calls to a function that does both the consing, caring and cdring. Something like (add-to-position kpos -1 -1)<p>- The is-piece-foo functions use eq? and equal? inconsistently.<p>- The CH-HOF function is full of copy-paste boilerplate with each instance having tiny tweaks. Turning these into calls to sensibly parametrized functions would make a big difference.<p>- There&#x27;s like 20 instances of (set! rsc (+ rsc 0)) . What in the world is that supposed to achieve?<p>- The indentation of IFs is horrible. Putting the THEN branch on the same line as the test, and then putting the ELSE on the next line is just criminal. Especially given how deeply nested some of the condition expression are, it&#x27;s very hard to notice that it&#x27;s in fact a two-branch rather than one-branch IF. It&#x27;s as if this code was deliberately written to be obfuscated.
aaronbrethorst大约 9 年前
It&#x27;s been about 15 years[1] since I last spent a lot of time with Scheme, but yes. As others have mentioned, the author could&#x27;ve done a better job with variable names, and syntax highlighting would do a world of good, but the code itself looks pretty understandable.<p>If you&#x27;re not using DrRacket, then please start using it. I learned what was then Scheme from SICP[2], but I&#x27;ve heard that the Little Schemer is perhaps a bit more approachable. Try starting with it[3].<p>[1] That moment when you realize it&#x27;s been over fifteen years since you started college...Yeesh.<p>[2] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Structure_and_Interpretation_of_Computer_Programs" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Structure_and_Interpretation_o...</a><p>[3] <a href="http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;13003850&#x2F;little-schemer-and-racket" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;13003850&#x2F;little-schemer-a...</a>
elliotec大约 9 年前
Quite, yeah.<p>But if the author&#x27;s goal was maximum readability, they might have considered doing away with the abbreviations.
anaphor大约 9 年前
Yes, although I&#x27;m not sure why they had to use single letter symbols to identify the different types of chess pieces. Also it seems like they could&#x27;ve applied the DRY principle a bit with all of those conditionals. Also the use of set! is a little bit ugly.
S4M大约 9 年前
I can somewhat make sense of that code, but the code of function ch-hof is a bit hard to read - and the function name is not helping. I guess he&#x27;s trying to see if the king is under check, and if so by which piece(s).<p>Also I notice code like:<p><pre><code> (- (cdr kpos) 1)) </code></pre> How is that possible? If kpos is a list of two elements to represent the king&#x27;s position, so (cdr kpos) should be a list, not compatible with subtraction.<p>For example:<p><pre><code> #;4&gt; (- (cdr &#x27;(1 2)) 1) Error: (-) bad argument type: (2)</code></pre>
评论 #11189334 未加载
评论 #11189288 未加载
adwf大约 9 年前
Yep, although I only use Common Lisp, this is still readable to me.<p>A couple of things could be improved for me; I almost never use abbreviated variable names unless it&#x27;s for simple iteration.<p>Also things like this being repeated on 8 lines, just screams for some sort of abstraction:<p><pre><code> (set! rsc (+ rsc 3)) (set! rsc (+ rsc 0))) (set! rsc (+ rsc 0))</code></pre>
Grue3大约 9 年前
I don&#x27;t find it readable. I immediately see a lot of repetition, which means an opportunity for helper functions&#x2F;macros. However the so-called &quot;hygienic macros&quot; in Scheme seem pretty confusing to me. I prefer non-hygienic macros like in Lisp.<p>My biggest Scheme program is [1]. I wrote it years ago, but I can still read it. Using sanely named variables is key.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;tshatrov&#x2F;scriptfu&#x2F;blob&#x2F;master&#x2F;animstack.scm" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tshatrov&#x2F;scriptfu&#x2F;blob&#x2F;master&#x2F;animstack.s...</a>
kogir大约 9 年前
Looks fine to me, though I tend to write my lisp narrower and taller. Part of that is breaking long argument lists out onto multiple lines with proper indentation.<p>Here&#x27;s some Racket code in DrRacket: <a href="http:&#x2F;&#x2F;i.imgur.com&#x2F;GKveHtl.jpg" rel="nofollow">http:&#x2F;&#x2F;i.imgur.com&#x2F;GKveHtl.jpg</a><p>You get used to it pretty quickly, and good editors will provide paren matching and completion.
ApplaudPumice大约 9 年前
I want to learn scheme but that is too much for me. So either<p>1. That is badly written code. 2. I&#x27;m a bad programmer. 3. Scheme&#x27;s syntax is ugly.<p>What do you think?
评论 #11189229 未加载
评论 #11189254 未加载
评论 #11189183 未加载
评论 #11189222 未加载
评论 #11189225 未加载
评论 #11189235 未加载
jiyinyiyong大约 9 年前
I will always hate parentheses there. So mine:<p>![](<a href="https:&#x2F;&#x2F;pbs.twimg.com&#x2F;media&#x2F;CcT4B8MUMAAiuvK.jpg:large" rel="nofollow">https:&#x2F;&#x2F;pbs.twimg.com&#x2F;media&#x2F;CcT4B8MUMAAiuvK.jpg:large</a>)
msie大约 9 年前
Yes
lispm大约 9 年前
No, the code looks ugly.