emacs.d/clones/lisp/www.cliki.net/Lisp newbie.html
2022-10-07 15:47:14 +02:00

103 lines
No EOL
17 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>CLiki: Lisp newbie</title>
<link rel="alternate" type="application/atom+xml" title="ATOM feed of edits to current article"
href="https://www.cliki.net/site/feed/article.atom?title=Lisp%20newbie">
<link rel="stylesheet" href="static/css/style.css">
<link rel="stylesheet" href="static/css/colorize.css">
</head>
<body>
<span class="hidden">CLiki - Lisp newbie</span>
<div id="content"><div id="content-area"><div id="article-title">Lisp newbie</div><div id="article">Where do I start?<p><b>Q:</b> What is the purpose of this page?<br>
<b>A1:</b> Well I am one, and I have lots of questions. I was going to do the wiki thing of "braindump onto the page, then delete or refactor later". Hopefully <a href="CLiki.html" class="internal">CLiki</a> could be left with a page which other newbies could use to get started.<br>
<b>A2:</b> A repository for stupid questions that might otherwise be answered in part 1 of the <a href="FAQ.html" class="category">FAQ</a>.<br>
<b>A3:</b> An editting area for questions to cull for the FAQ? I'm afraid I don't do Usenet any more, like I don't do TV. Just can't keep up with it.<p><hr><p><h3>Background on a newbie</h3><p>My apologies to Larry Wall for jilting Perl (but see <a href="https://www.cliki.net/Cut%20and%20shut" class="new">Cut and shut</a>), but I think the next project requires something Lisp-flavoured, although I don't know what. The spec for the project changes whenever I contemplate it, see <a href="http://noosphere.org/">noosphere.org</a> for something in the same vein.<p>I suspect my questions would be too far OffTopic for WardsWiki, so I trundled over here.<p>I've dabbled in <a href="elisp.html" class="internal">elisp</a>, but I need something that can live outside emacs (yes I know I can fire it up on the command line .. but that doesn't seem right).<p><i>"Emacs Lisp considered harmful" moved to the <a href="elisp.html" class="internal">elisp</a> page. That said, I would like to see one comparison page if such a beast could exist.</i><p>Naturally I'm after something that's already ported to <a href="Debian.html" class="internal">Debian</a>. Laziness and moral soundness in one easy to swallow package(ing system). *g*<p><i>Well, assuming you mean Debian on x86/Linux, you have the choice between <a href="CMUCL.html" class="internal">CMUCL</a>, <a href="SBCL.html" class="internal">SBCL</a>, <a href="CLISP.html" class="internal">CLISP</a>, <a href="gcl.html" class="internal">gcl</a>, <a href="ecl.html" class="internal">ecl</a> and installer packages for Allegro and LispWorks. I have most of them installed.</i><p><h3>The problem</h3><p>I'm circling the Lisp family like a vulture. I'm pretty sure it has what I need, but I'm not sure where to attack to get going efficiently.<p>The thorough route would be to read half a dozen books on <a href="Lisp.html" class="internal">Lisp</a>, <a href="Scheme.html" class="internal">Scheme</a> and Haskell, learn each, and then compare. However I'm an idle sod, so that doesn't fly.<p>
<h3>Lisp or Scheme or ...?</h3><p>I don't mean to start a fight here .. I suppose the only fair way to find out whether I want Lisp or Scheme is to try both, but that takes three times as long...<p>(See <a href="http://c2.com/cgi-bin/wiki?LispSchemeDifferences">Wiki:LispSchemeDifferences</a> for a start.)<p><b>Q:</b> If I start with one and then decide to change to the other, how much trouble will I have? Can I just translate keywords and port to the different library, or would I need to start from scratch?<p><i>They really are completely different languages.</i><p>I'm getting that impression, but at this stage this is about as helpful as telling a Basic programmer that Java is completely different to C#. I'm trying to be objective about something I don't understand.<p><i>If you are viewing Lisp &amp; Scheme from the vantage point of a Java/C++/C# programmer then they look awfully similar. A lot of the points on the early learning curve are similar - getting used to the syntax, the idea of closures, functions as parameters, s-expressions, macros rewriting code etc. It's only once you have peeled away this layer that you start noticing the differences, and that they are in fact quite different languages. I'm still pretty much a newbie, but the best way of expressing the difference by analogy seems to be comparing CL to C++, and Scheme to C. Sure, Scheme is smaller, easier to learn, more lightweight and has generally more implementations, so might be more suitable for the newbie aclimatising to the LISP family, but CL is a much more thoroghly thought - out language, with a much broader spec, more suitable for multi-paradigm programming in the large..</i><p><b>Q:</b> What am I missing out on with CL? Does anyone actually use continuations? Without regret?<p><i>
You can do continuation-passing-style without call/cc. The classes I've taken where we've used CPS never touched call/cc (though we did implement it).
</i><p>Is the continuation done by arranging to return its value, or do you have a funky macro to redirect the execution path?<p>SchemeVsLisp: Scheme has <a href="https://www.cliki.net/Hygienic%20macros" class="new">Hygienic macros</a>, which are supposed to stop you shooting yourself in the foot. The CL people tell me that sometimes you need to shoot something very close to your foot, and should be allowed to; and that when you don't, CL can restrict your macros in a similar way.<p><i>What's more, Scheme's hygienic macros don't actually stop you from shooting yourself in the foot. See postings in <tt>comp.lang.scheme</tt> by Al Petrovsky for mind-boggling uses of Scheme macros.</i><p><b>Q:</b> Can I get the best of both worlds? (i.e. use CL libraries from <a href="Scheme.html" class="internal">Scheme</a>). <i>Well I did say "stupid questions".</i><p><i>In what sense is this the best of both worlds?</i><p><h3>
<a href="Common&#32;Lisp&#32;implementation.html" class="internal">Common Lisp implementation</a>s</h3><p>So many to choose from. Do I pick one that's beginner-friendly or just start with something that has convenient <a href="Database.html" class="internal">Database</a> and <a href="Web.html" class="internal">Web</a> hooks? Do I worry about performance now or when I'm firmly stuck with one implementation? etc.<p><i>Well, probably <a href="CLISP.html" class="internal">CLISP</a> is the most "beginner friendly" (if there is such a thing) due to readline, but you can put readline around <a href="CMUCL.html" class="internal">CMUCL</a> too (look around this wiki). Convenient database and web hooks are available for all major LISPs. Web application too (<a href="Araneida.html" class="internal">Araneida</a>, <a href="https://www.cliki.net/CL-HTTP" class="new">CL-HTTP</a>, <a href="https://www.cliki.net/PortableAllegroserve" class="new">PortableAllegroserve</a>). So, there you have it: I managed not to answer your question. ;)</i><p><h3>Language features</h3><p><b>Q:</b> Is Lisp suitable for <a href="http://c2.com/cgi-bin/wiki?ExtremeProgramming">ExtremeProgramming</a> or other agile processes?<br>
<b>A:</b> It would seem that it is not only suitable, but they did it first. See <a href="http://c2.com/cgi-bin/wiki?AgileLisp">Wiki:AgileLisp</a> for example.
Of course the language can't help you talk to your Customer, but it streamlines testing remarkably. So I'm told, but I can see it happening already.<p><b>Q:</b> I like heredocs. How do I do them in Lisp?<br>
<b>A:</b> You could probably cook up a funky reader-macro to do <a href="https://www.cliki.net/heredocs%20in%20lisp" class="new">heredocs in lisp</a>. Probably a good first exercise in reader-macro-land.<p><b>Q:</b> What does the compiler do for me behind my back?<p>I started asking about this on WardsWiki, <a href="http://c2.com/cgi/wiki?WhyLisp">Wiki:WhyLisp</a> but may bounce back over here to keep the topic police happy.<p>I've found <a href="https://www.cliki.net/site/HyperSpec/Body/fun_disassemble.html" class="hyperspec">disassemble</a> now and I'm quite impressed. It's good to be able to get that close to whats going on, when you need to of course, and so easily too. I would consider this a selling point when pitching to a hacker.<p><b>Q:</b> Hypothesis: If a function <tt>f</tt> is known to be pure, can I assume that the worst <tt>f</tt> can do if it turns out to be malicious is waste CPU and memory, until terminated?<p><i>It could return an <b>eeevil</b> data structure or maybe call a <b>purely eeevil</b> continuation.</i><p>Well naturally one doesn't trust the data it returns, but I'd forgotten about continuations. Surely it can only get one from a global or other callable function? Global variables would <b>all</b> have to be eliminated. <tt>8-/</tt><p><b>Q:</b> How can I detect whether a function is pure? (I mean it's just data, right? But it refers to lots of other code, whose purity is unknown)<p><i>First, solve the halting problem. Then...<p>But really, who cares? This is Schemeish thinking, but CL isn't Scheme. You will probably end up writing code in all sorts of paradigms, eventually, and excessive worrying about making functions ``pure'' is a waste of time, often.</i><p>This would only be useful as applied to the previous question. I think instead perhaps it would be better to reimplement a subset of Lisp in Lisp but paying attention to security? Not that I need to do this yet, but the ability would fit the project I think I'm headed towards.<p>Don't wanna play with Java, but it would fit better in other respects.<p><b>Q:</b> Is there a reverse list of functions and syntax? Something like a list of all functions (um, how many are there in CL?) mapped to related concepts, e.g. '(("joiners" cons list nconc append) ("splitters" car cdr nth))<p><blockquote>
I've got to the point where I <i>think</i> I need "something that does the opposite of <tt>list</tt>", and I'm not sure where to look.<p><i>Not sure what you mean here, are you thinking of <a href="https://www.cliki.net/site/HyperSpec/Body/mac_destructuring-bind.html" class="hyperspec">DESTRUCTURING-BIND</a>?</i><p>I don't quite follow it, but it sounds ilke the right sort of thing, although having looked at some list traversal examples I'm already seeing I was going down the wrong track anyway.<p>I think my interntion for this question has changed, sorry. It was a fuzzy Q anyway.<p><i> On the other hand, the <a href="HyperSpec.html" class="internal">HyperSpec</a>, as well as having a page on <em>everything</em>, is organized into chapters... so you can read the Sequences chapter, or the Conditions chapter, and learn 'vertically' that way.</i>
</blockquote><p><b>Q:</b> Lisp advocates talk about the advantages of the Lisp environment in operating incrementally: fixing bugs and then retrying and continuing execution, making changes while your program is still running etc. This sounds way cool, but are there any documented examples of how to do this stuff, especially with <a href="CLisp.html" class="internal">CLisp</a> or <a href="CMUCL.html" class="internal">CMUCL</a>?<p><blockquote>
<b>A:</b> For a simple "proof of concept," just start <a href="CLISP.html" class="internal">CLISP</a> or <a href="CMUCL.html" class="internal">CMUCL</a> and
define a function or two in the reader (as opposed to writing them in a file
and then loading the file). As you see, you can test the functions, redefine
them, debug them, etc. It is much more pleasant, of course, to write code in
an editor, so your next step should be to hook up a <a href="Lisp.html" class="internal">Lisp</a> to <a href="Emacs.html" class="internal">Emacs</a>. <a href="ILISP.html" class="internal">ILISP</a> is a
popular package that works with most Lisps. ELI is, IMHO, much better, but
doesn't work with other Lisps than Franz's <a href="Allegro.html" class="internal">Allegro</a> CL out of the box. (There
are patches and hacks that make it work with other Lisps, though.) If
setting up <a href="ILISP.html" class="internal">ILISP</a> or ELI is too much work for you, you can download the demo
version of <a href="Lispworks.html" class="internal">Lispworks</a>, as it has an integrated editor. <p><i><a href="Slime.html" class="internal">Slime</a> is the preferred Lisp environment these days (June '04).</i><p> Once you have your Lisp hooked up to an editor, you can edit a file full of code in an editor buffer and press some special key each time you change a
definition (such as a DEFUN). The code you changed will be automatically sent to your Lisp and compiled. (There will also be a reader available, which is nice for one-liners you don't intend to keep.)
</blockquote><p><b>Q:</b> I've also heard about hitting a bug and being able to make a change to the code and resume execution (the sort of thing you can only dream about in Java). How do we do this?<p><blockquote>
This is slightly implementation- and problem-dependent. Here's a very simple example, from SBCL:
</blockquote><pre>
* (defun fact (x) (when (&gt; x 0) (* x (fact (1- x))))) ; factorial function
FACT
* (fact 5) ; test it. Oh look, it&#039;s buggy!
debugger invoked on condition of type SIMPLE-TYPE-ERROR in thread 16927:
Argument Y is not a NUMBER: NIL
restarts (invokable by number or by possibly-abbreviated name):
0: [ABORT ] Reduce debugger level (leaving debugger, returning to toplevel).
1: [TOPLEVEL] Restart at toplevel READ/EVAL/PRINT loop.
(SB-KERNEL:TWO-ARG-* 2 1 NIL)[:EXTERNAL]
0] (defun fact (x) (if (&gt; x 0) (* x (fact (1- x))) 1)) ; fix the function
STYLE-WARNING: redefining FACT in DEFUN
FACT
0] f 1 ; go down a frame to somewhere where we know the answer
(FACT 1)
1] ret 1 ; return 1 from the current frame (1! is of course 1)
120 ; the right answer to our original problem
</pre><p>Bear in mind that this was a very simple example.<p>
<b>Q:</b> In the incremental style of development, if I make a new function with (defun ....) and do lots of typing, and it finally all works, how do I then get that into the "source code"? Do I have to scroll back to what I typed and copy and paste it into a file? I understand that I can save an image file, but that's not source code.<p><b>A:</b> Most of the time, you probably won't be typing your defuns at the REPL; instead, most people tend to use something like <a href="SLIME.html" class="internal">SLIME</a> or <a href="ILISP.html" class="internal">ILISP</a>, which enables communication between an Emacs and your Lisp. This way, you type your defuns in a real editor, complete with good line editing functions and all the Emacs sexpr navigation goodness, and when done type control-meta-x to send the form to your Lisp.<p>Also, you ought to consider writing your code in files and invoking compile-file on them (in <a href="SLIME.html" class="internal">SLIME</a>, control-c control-k does this in one step). This is still interactive development - even though your code is stored in a file and compiled, you can still send new definitions via control-meta-x as you test and debug, and the REPL is still available for you to test your definitions out.<p><b>Q:</b> Where do I get information on Gray Stream?<p><h3>Useful links</h3>
- An ECL + Qt4 Windows/Linux <a href="http://sourceforge.net/forum/forum.php?forum_id=516261"> example </a>
; <a href="http://sourceforge.net/projects/lisp-cffi-qt4">this one</a> is newer (requires CFFI, works with many recent Lisps)<p><h3>On Flaming</h3>
Newbies are friends, not food!</div></div>
<div id="footer" class="buttonbar"><ul><li><a href="Lisp&#32;newbie.html">Current version</a></li>
<li><a href="https://www.cliki.net/site/history?article=Lisp%20newbie">History</a></li>
<li><a href="https://www.cliki.net/site/backlinks?article=Lisp%20newbie">Backlinks</a></li><li><a href="https://www.cliki.net/site/edit-article?title=Lisp%20newbie&amp;from-revision=3637877419">Edit</a></li><li><a href="https://www.cliki.net/site/edit-article?create=t">Create</a></li></ul></div>
</div>
<div id="header-buttons" class="buttonbar">
<ul>
<li><a href="https://www.cliki.net/">Home</a></li>
<li><a href="https://www.cliki.net/site/recent-changes">Recent Changes</a></li>
<li><a href="CLiki.html">About</a></li>
<li><a href="Text&#32;Formatting.html">Text Formatting</a></li>
<li><a href="https://www.cliki.net/site/tools">Tools</a></li>
</ul>
<div id="search">
<form action="https://www.cliki.net/site/search">
<label for="search_query" class="hidden">Search CLiki</label>
<input type="text" name="query" id="search_query" value="" />
<input type="submit" value="search" />
</form>
</div>
</div>
<div id="pageheader">
<div id="header">
<span id="logo">CLiki</span>
<span id="slogan">the common lisp wiki</span>
<div id="login"><form method="post" action="https://www.cliki.net/site/login">
<label for="login_name" class="hidden">Account name</label>
<input type="text" name="name" id="login_name" class="login_input" />
<label for= "login_password" class="hidden">Password</label>
<input type="password" name="password" id="login_password" class="login_input" />
<input type="submit" name="login" value="login" id="login_submit" /><br />
<div id="register"><a href="https://www.cliki.net/site/register">register</a></div>
<input type="submit" name="reset-pw" value="reset password" id="reset_pw" />
</form>
</div>
</div>
</div>
</body></html>