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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Restarts in Common Lisp

139 点作者 sulami大约 5 年前

15 条评论

phoe-krk大约 5 年前
Nitpick:<p>&gt; While Common Lisp calls them “conditions”, I will stick with “error” in this post, because I don’t think “condition” is very self-explanatory if you’re not familiar with the language already. Please forgive my inaccuracy.<p>This is misleading. It is possible to signal conditions that are not errors, and therefore do not require stack unwinding, like errors do. The condition facility allows one to execute arbitrary code with arbitrary arguments at the SIGNAL site.<p>`SIGNAL` walks the list of condition handlers and executes the type-matching ones in order of their binding. If a handler returns normally, then next ones are executed; if all matching handlers are exhausted, `SIGNAL` returns, and code execution continues.<p><pre><code> CL-USER&gt; (handler-bind ((condition (lambda (c) (format t &quot;;; Foo: ~A~%&quot; c))) (condition (lambda (c) (format t &quot;;; Bar: ~A~%&quot; c))) (condition (lambda (c) (format t &quot;;; Baz: ~A~%&quot; c)))) (signal &#x27;condition) 42) ;; Foo: Condition CONDITION was signalled. ;; Bar: Condition CONDITION was signalled. ;; Baz: Condition CONDITION was signalled. 42 </code></pre> Of course, a handler can perform a non-local transfer of control elsewhere, e.g. by using `GO` or `THROW`. In such a case, control is transferred out of `SIGNAL`, so next handlers are - naturally - not invoked.<p>While I understand that this post is trying to appeal to language newcomers, simplifying the whole condition system to an &quot;error try-catch&quot; is a drastic misrepresentation of the condition system that will leave the newbies with the impression that that&#x27;s all that is possible with that feature. Since it seems that the author is a Lisp newcomer as well (which I wholeheartedly admire! :), I&#x27;d advise him to use the original notation of &quot;condition&quot;, instead for the one of &quot;error&quot;.
评论 #22750062 未加载
评论 #22751309 未加载
评论 #22752664 未加载
aidenn0大约 5 年前
Aside from restarts, the core difference between the Common Lisp condition system and exceptions in most other languages is that the exception handler can run before the stack is unwound.<p>Having the call-stack available at the time the handler runs can be very useful; many modern dynamic languages will save some of that information in the exception object. It&#x27;s still surprising to me that so very few languages have copied this feature (or even independently discovered it).
评论 #22749295 未加载
评论 #22749148 未加载
评论 #22750851 未加载
评论 #22753820 未加载
chaitanya大约 5 年前
Many years ago I wrote about restarts in CL using the example of a CSV parser: <a href="https:&#x2F;&#x2F;lisper.in&#x2F;restarts" rel="nofollow">https:&#x2F;&#x2F;lisper.in&#x2F;restarts</a><p>Fun fact: the restarts based CSV parser was based on some real world work me and a colleague had done for an online travel portal. I wrote about it here: <a href="https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;lisp&#x2F;comments&#x2F;7k85sf&#x2F;a_tutorial_on_conditions_and_restarts&#x2F;drceozm&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;lisp&#x2F;comments&#x2F;7k85sf&#x2F;a_tutorial_on_...</a>
metroholografix大约 5 年前
I find that Practical Common Lisp has not aged well. Maybe it&#x27;s the &quot;practical&quot; part with the specific examples the author used -that are now terribly dated- that is mainly responsible, but the end result is that I no longer recommend it.<p>My goto recommendation for newcomers is Norvig&#x27;s PAIP which is full of insightful moments and unbelievably good code and for people with more experience &quot;Let over Lambda&quot; by Doug Hoyte, which is a mind-blowing book mainly because of the time-transcendent nature of the examples Doug chose.
评论 #22749257 未加载
评论 #22748453 未加载
评论 #22749204 未加载
评论 #22748467 未加载
评论 #22752468 未加载
评论 #22748370 未加载
评论 #22756890 未加载
kazinator大约 5 年前
In TXR Lisp, I unified condidtions and restarts into one thing: exceptions. By programmer convention only, restarts are exceptions derived from <i>restart</i>.<p>The documentation has dialect notes about this with a comparative example:<p><a href="https:&#x2F;&#x2F;www.nongnu.org&#x2F;txr&#x2F;txr-manpage.html#N-00F77525" rel="nofollow">https:&#x2F;&#x2F;www.nongnu.org&#x2F;txr&#x2F;txr-manpage.html#N-00F77525</a>
artemonster大约 5 年前
Can somebody please also elaborate what is the difference between condition system in CL and &quot;new thing&quot; algebraic effect handlers? On the surface I see no difference, other that the latter is being a more formalized version of conditions? But we have same properties: 1. signal a condition to some handler(s), possibly with value. 2. reify the stack into a (delimited up to level with handler) continuation and let handler decide what to do with it: save, restart, resume with some value. Am I missing something?
评论 #22753828 未加载
评论 #22749212 未加载
jgrant27大约 5 年前
<a href="https:&#x2F;&#x2F;imagine27.com&#x2F;error-handling-or-the-emporors-old-clothes" rel="nofollow">https:&#x2F;&#x2F;imagine27.com&#x2F;error-handling-or-the-emporors-old-clo...</a>
mrow84大约 5 年前
A long time ago I wrote a conditions system for python: <a href="https:&#x2F;&#x2F;code.google.com&#x2F;archive&#x2F;p&#x2F;pyconditions&#x2F;" rel="nofollow">https:&#x2F;&#x2F;code.google.com&#x2F;archive&#x2F;p&#x2F;pyconditions&#x2F;</a><p>I wouldn&#x27;t recommend using it, but it is an interesting error&#x2F;control model, and I always wondered why it hasn&#x27;t seen wider implementation - I presume because it is closer to the &quot;dangerous&quot; end of the power spectrum.
olah_1大约 5 年前
Hey Sulami, this is off topic, but how did you get those nice sidebar footnotes? What theme are you using? Or how did you edit the css?
评论 #22748303 未加载
评论 #22748300 未加载
nabla9大约 5 年前
The condition system in Common Lisp is really amazing piece of work. Error and exception handling is just subclass of larger set of possible uses called conditions.
shadowgovt大约 5 年前
Interesting. This looks like an implementation of the abstract computer science notion of continuations (<a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Continuation);" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Continuation);</a> neat to see them used this way.
评论 #22754483 未加载
评论 #22752497 未加载
nickpsecurity大约 5 年前
With a quick DuckDuckGo-ing, I found this intro to be really understandable compared to most links I read:<p><a href="https:&#x2F;&#x2F;z0ltan.wordpress.com&#x2F;2016&#x2F;08&#x2F;06&#x2F;conditions-and-restarts-in-common-lisp&#x2F;" rel="nofollow">https:&#x2F;&#x2F;z0ltan.wordpress.com&#x2F;2016&#x2F;08&#x2F;06&#x2F;conditions-and-resta...</a>
nerdponx大约 5 年前
R, continuing its theme of being a weird Lisp with C syntax and really bad function names, has a condition system: <a href="https:&#x2F;&#x2F;adv-r.hadley.nz&#x2F;conditions.html" rel="nofollow">https:&#x2F;&#x2F;adv-r.hadley.nz&#x2F;conditions.html</a>
slifin大约 5 年前
Keep meaning to look at this, Clojure has some libraries for this I could use:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;clojureman&#x2F;special#alternative-libraries" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;clojureman&#x2F;special#alternative-libraries</a>
kazinator大约 5 年前
The author doesn&#x27;t seem to understand that not all exceptional situations are errors, which suggests he is a complete newbie to exception handling, which explains why he didn&#x27;t propose the word &quot;exception&quot; instead of &quot;condition&quot;.
评论 #22751909 未加载
评论 #22751901 未加载