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.

Show HN: Minimal Lisp in ~70 lines of Haskell

76 pointsby die_sekteabout 14 years ago

7 comments

telabout 14 years ago
I think this might be some of the most beautiful code I've seen in a long while<p><pre><code> value :: Parser Value value = List &#60;$&#62; (char8 '(' *&#62; sepBy value (takeWhile1 isSpace_w8) &#60;* char8 ')') &#60;|&#62; Number . fst . fromJust . B.readInteger &#60;$&#62; takeWhile1 isDigit_w8 &#60;|&#62; Symbol &#60;$&#62; takeWhile1 (inClass "A-Za-z\\-") </code></pre> The power of parser combinators representing the elegant lisp syntax.
评论 #2502887 未加载
Peakerabout 14 years ago
The content of Fun is actually the type: Context -&#62; Value -&#62; (Context, Value).<p>If you flip the arguments, you get: Value -&#62; Context -&#62; (Context, Value). The (Context -&#62; (Context, Value)) is actually the State type:<p><pre><code> State s a = s -&#62; (s, a) </code></pre> I modified your code to use this type. During the process, I encountered some peculiar things (and factored out some things), see comments in code.<p><a href="https://gist.github.com/950445" rel="nofollow">https://gist.github.com/950445</a><p>Funny that it turns out slightly longer when re-using more code due to syntactic artifacts. Monad comprehensions (recently restored to GHC via an extension) would resolve that.
评论 #2503681 未加载
stiffabout 14 years ago
I do not know Haskell enough to fully judge this, so maybe someone can correct me, but that seems _very_ minimal. From what I understand, it doesn't even have arithmetic operations, it "executes" incorrect programs without any error (try repl "())") and also it doesn't have any kind of scoping, for example in the following code a function argument is bound in the global (and only) environment:<p><i>Main&#62; repl "(begin ((fun (x y) y) 1 2) y)"<p>([("x",Number 1),("y",Number 2),("begin",Fun),("car",Fun),("cdr",Fun),("cons",Fun),("cond",Fun),("def",Fun),("eval",Fun),("fun",Fun),("t",Symbol "t"),("quote",Fun)],Number 2)</i><p>I don't think it's fair to call this a Lisp at all at this point. There is a nice writeup of implementing a Lisp in Python by Peter Norvig, where at least the most basic things are implemented correctly (and the code is documented): <a href="http://norvig.com/lispy.html" rel="nofollow">http://norvig.com/lispy.html</a><p>Also, I get errors even with some very simple statements that theoretically seem to be implemented like:<p><i>Main&#62; repl "(cons 1 2)"<p>([("begin",Fun),("car",Fun),("cdr",Fun),("cons",Fun),("cond",Fun),("def",Fun),("eval",Fun),("fun",Fun),("t",Symbol "t"),("quote",Fun)],List<p>[Exception: lisp.hs:48:8-48: Irrefutable pattern failed for pattern (ctx', [v', (Main.List vs')])</i><p>Am I doing something wrong here?
评论 #2503582 未加载
评论 #2503657 未加载
kleibaabout 14 years ago
Cool, only 70 lines!!<p>(plus a ton of libraries)<p>j/k ;-) Actually, I like this - it shows how cool Haskell really is! No, wait - it shows how cool Lisp really is! No wait... I'm confused... :-)
评论 #2504258 未加载
评论 #2503603 未加载
评论 #2503050 未加载
guard-of-terraabout 14 years ago
See also <a href="http://www.defmacro.org/ramblings/lisp-in-haskell.html" rel="nofollow">http://www.defmacro.org/ramblings/lisp-in-haskell.html</a>
评论 #2502845 未加载
nkassisabout 14 years ago
Now if someone will just write a 70 line Haskell interpreter in lisp we can have lispkell all the way down.
mericabout 14 years ago
How long did it take to write?
评论 #2502555 未加载