[cl-faq] FAQ offerings, from Perl point of view

Matthew Astley clgard$mca at t8o.org
Fri Jan 6 20:49:31 CST 2006


#| I've followed Robb's numbering for what I've written here,
   http://www.lispniks.com/pipermail/cl-faq/2005-December/000030.html
   because I think it makes sense and will make it easier to spot
   duplicates (sort < faq | uniq)...  edit at will.


   What's the notation for a hyperlink?  Can you just mark them up in
   place ie. no support for custom anchor text?  This is usually
   adequate, and customary in usenet FAQs due to text formatting.

   IMHO there should be many links and copying of text.  A
   sufficiently large FAQ can be intimidating in its own right, and
   there's the maintenance burden.  Hyperlink lists can be maintained
   en masse with a link-ping sort of robot.


   I've written quite a bit here.  If it would be useful, I can sort
   it into questions I have, answers I believe are valid or at least
   somewhat helpful and...  other.  Also I can turn my submission into
   a diff, and I could invent a notation for scoring F'ness of AQs and
   A'ness of the As.
|#



* Section 0: About this FAQ

\faq{
\question{What other other Lisp FAQs are there?  How does this one fit
with the others?}
\answer{There are several,

 @ comp.lang.lisp: Google reports several copies, presumably each
 different,

    @ http://www.cs.cmu.edu/Groups/AI/html/faqs/lang/lisp/top.html

    @ http://www-jcsu.jesus.cam.ac.uk/~csr21/lispfaq.html

    @ http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.lisp.html

<more>
}
}

\faq{
\question{In what format would the FAQ maintainer prefer submissions?}
\answer{The first answer on the mailing list at
http://www.lispniks.com/pipermail/cl-faq/2005-December/000019.html .

This mentions that the master document is written with the markup tool
at http://www.gigamonkeys.com/lisp/markup/ .  There is no formating
rules summary but you can glork some of it from the source document at
http://www.lispniks.com/faq/faq.txt .
}
}

\faq{
\question{Is there an Emacs mode to help with the wordwrap?}
\answer{<I'm only half joking>}
}


** Section 2.1: Common prejudices for/against Common Lisp

#| may I suggest shuffling some of the existing items in here?  Might
   help consign them to history... |#


* Section 4: Comparisons to other languages

\faq{
\question{Can I break out into other languages?  Compare Perl's
Inline::C Inline::Java Inline::Tcl Inline::Befunge...

Inline::C can do really scary things: check CPR at the bottom of
http://www.perl.com/pub/a/2001/02/inline.html}
\answer{<refer to ffi questions>}
}

#| I've never used Inline::* in anger so I don't know whether they're
   more than a cute trick, but they're out there... |#

\faq{
\question{Can I embed a Common Lisp in my C (or other) application?
e.g. Exim3 MTA can contain Perl in the config}
\answer{}
}

There are various hooks into and out of Java, pipes and FFIs(?) into
Tcl...


** Section 4.2: C, C++, C#, Objective-C

<isn't this a rather broad grouping?>

\faq{
\question{Is there an enum type or something else similar?}
\answer{}
}

\faq{
\question{Can I break out into assembler?}
\answer{<only a C programmer...>}
}


** Section 4.5: For Perl programmers

As a long-time Perl programmer I have various questions which I choose
to ask in terms such as "how in Common Lisp do I do the Perl operation
<foo>".  This section is here to make it easier for Perl thinkers to
learn Lisp.

#| I've found answers to some but I've no reason to think they're much
   good!  Can you answer / check and correct / redirect please?

   I added some questions that YAPH may ask, too.  For many of these I
   didn't look for an answer because I don't need to know yet, but
   someone somewhere will ask sooner or later. |#

In the general case it's not wise to attempt to translate literally
from Perl to Lisp.  Programming languages influence thought, and this
becomes more obvious when you switch languages.  I think it's wise to
make the distinction between

 @ things Perl has that aren't in Lisp

 @ bodges Perl uses because it's missing features Lisp has

 @ features that have different names (with a special category for
 false friends)

#| If there were per-language sections, this warning would apply to
   most of them.  This may also help to avoid "my language is better
   than yours".

   It's also likely that some sub-sections will apply to several
   language comparisons. |#


*** General

\faq{
\question{What is the CL view of "there's more than one way to do it"?}
}

\faq{
\question{How do I run my Common Lisp program as an executable un*x
script, the way most Perl is used?}

\answer{The SBCL manual has hints on how to do this,
http://www.sbcl.org/manual/Shebang-Scripts.html}

\answer{See also "Is it possible to generate executables?  (in Linux,
in the ELF sense?)"}
}

\faq{
\question{How do Perl's concepts of packages, modules and class
translate into Lisp?}
\answer{
\bold{as far as I can make out so far!}

Lisp packages are equivalent to Perl packages, in the sense that
they're just namespaces for symbols, but they're not hierarchical.

Lisp source files seem to follow even \i{less} than Perl the notion of
one source file per package, but more often there are several files
per package rather than several packages per file.  Except in the
files that declare packages.

Lisp modules...?

The Lisp class (and more generally, type) structure is not tied to the
package structure the way it is in Perl.}
}

\faq{
\question{How do I grub about in the internals?  In Perl I can walk
the symbol tables of all loaded packages, find things that look like
classes, look up their methods and inheritance and replace or advise
methods.}

\answer{Dunno.

You can replace code in most other packages, (see locked package?) but
exactly when will this require other code to be recompiled to take
effect?}
}

\faq{
\question{Does anyone play Common Lisp golf?  (If you've not heard of
Perl Golf then open your eyes to a new horror, or not.)}
}


*** List operations

Perl has general purpose arrays which are cheap and easy to extend in
either direction, and nestable.  Operations:

   $foo[ $n ]	element get/set
   @foo[ @n ]	multi-element get/set; sub-list extraction
   push		append to right
   pop		remove and return one from the right
   unshift	insert at the left
   shift	remove and return one from the left
   splice	a superset of the above

Lisp has lists, vectors, arrays.
   pop		like Perl shift		for lists
   push		like Perl unshift	for lists? with caveats?
   elt		setf'able element
   aref
	... and much more.

Beyond this I won't attempt to answer.  Keep the reference book handy.


\faq{
\question{What about Perl's map and grep?}
\answer{Lisp invented it!  There are loads of them [and they make my
head hurt]

However, Perl's (native) notation is shorter in some cases

  my %pairs = map {( lc($_), [ $_, length($_) ] )} qw(eeKy bEeky bOO);

Assign to just-declared lexically scoped hash, map with anonymous
subroutine (i.e. lambda) processing a list of three strings.

Not that it would take long to write a Lisp macro to do this.}
}

\faq{
\question{What about bit vectors?}
\answer{Lisp has native bit vectors.  Perl has modules for these
things (he muttered vaguely, having never used them).}
}

\faq{
\question{Perl's scalar and list context?}
\answer{Lisp has (values ...) and an assortment of multiple-value-*
functions.  [They're not equivalent, just similar...  I don't know how
similar the idioms are.]

A noteworthy case is

  (values)

which returns nothing - not even NIL.  Used for example in the reader:
functions that read a comment return nothing.  Functions that read a
string return the string.  Functions that read a list return the list.
}
}


*** String quoting operators

\faq{
\question{Perl has many varied and useful quoting operators and I've
grown accustomed to using them.  Can I use them in Lisp?}
\answer{There's some discussion at http://www.cliki.net/heredocs%20in%20lisp}

\answer{It's easy to make reader macros that do these sorts of things,
but exactly which ones will be useful when one knows other Lisp idioms
isn't obvious.}
}


*** Inline documentation

\faq{
\question{Perl has POD, a comment and documentation format which
contains a limited text markup and can be interspersed with the code.
Sick people can even read sections of POD into their code using
heredocs.  Can I do these tricks in Lisp?}
\answer{Lisp has docstrings for functions, macros and variables.

<more about `markup' and other things?>}
\answer{<I've seen reference to a tool used by SBCL developers>}
}


*** Line numbers

\faq{
\question{How do I get the current line number for a stream?  In Perl
we have $. and (for instances of IO::Handle) the input_line_number
method.}
\answer{This appears to be non-trivial, as far as I can see from
(define-condition reader-error ...) in
/usr/share/sbcl-common/code/condition.lisp for v0.9.0.19

(file-position stream) will give you a byte position (?), which may
differ from the character position (?)}
}

\faq{
\question{Is there a stream subclass or wrapper which can keep track
of row & column without having to re-read the entire file?}
\answer{}
}

\faq{
\question{...or a library which will safely get row & column info by
re-reading the stream?}
\answer{}
}

\faq{
\question{How do I get values equivalent to __FILE__ __PACKAGE__
__LINE__ ?  The Perl compiler replaces them with the values referring
to the source.  They are useful for labelling error messages with the
origin, in much the same way that C uses them.}
\answer{<Presumably this is complicated by compile / load / eval
differences?>}
}


*** Constants

\faq{
\question{Perl defines constants with

  use constant FOO => "do_init";

These are accessed as barewords - they are a subroutine of no args, no
side-effects and constant return value, put in the current package's
symbol table.  They are used variously as "magic" strings or numbers,
program parameters and symbols to which variables may be set or
compared to avoid risking typos.

What does Lisp do?}
\answer{See defconstant and defparameter.  These are similar to
defvar.}
\answer{Have you considered using symbols instead?  They're good for
representing states but they won't protect you from typos:

 (defun frob (state)
   (cond ((eq state 'initialised)
	  (poke))
	 ((eq state 'stale)
	  (prod))
	 ((eq state 'spinning)
	  (tap))))

 (frob 'initialized) ; WRONG SYMBOL!
}
\answer{You can do (defun foo () "do_init") and encourage the compiler
to inline it...  is this a common idiom?  <What
declare/declaim/proclaim does it need?>}
}


*** Gluing processes together

\faq{
\question{Perl's support for Unix IPC is quite good for simple things.
There are also many standard recipes and packages around for more
complicated situations such as double-pipe-to-child or three-way open.
What's the Lisp library called?}
\answer{}
}


*** Safe code compilation and execution

\faq{
\question{Perl has taint checking, whereby values coming from outside
the program can be marked as "possibly not trustworthy".  This
provides a bit of a safety net to prevent these values being used
dangerously in filenames or shell commands.  Does Lisp need this?}
\answer{}
}

\faq{
\question{Perl has the "Safe" module which allows code to be compiled
and executed with a restricted set of bytecode operators and
restricted access to other packages.  In principle this makes it
possible to use untrusted code without allowing it to make a net
connection, delete your files, trojan your crontab etc..  How can Lisp
secure you against untrusted code?}
\answer{<Lisp interpreter in Lisp?  Macros to filter code before it's
compiled?>}
}

#| ...and the same again under Java, but with a different spin... |#


** Section 5.4: Getting help with your code

\faq{
\question{Where can I get help or advice on this Free software I'm
writing?}
\answer{<irc channels, usenet, ...?>}
}

#| I'm guessing that cl-gardeners isn't the right place, but I've not
   seen this explicitly |#

\faq{
\question{I can see my Lisp implementation has code to do <foo>, how
do I figure out how it's doing that so I can use the trick?}

\answer{I have sbcl installed under Debian.

  locate sbcl
  \i{many files listed}

Choose a directory that looks like it contains relevant stuff.

  grep -ri relevant-keyword /usr/share/sbcl-common/

Recursive keyword search is a dirty, inefficient trick but it works on
almost anything you care to have source for. 

(People writing code of any sort should kindly bear this in mind, and
avoid obfuscating strings or symbols unnecessarily.)
}
\answer{Use http://www.koders.com/ and do your keyword search there.

Caveat: it didn't find (define-condition reader-error ...) in SBCL
when I searched [bug reported to them 2006-01]}
}




* Edits to existing questions
**
\faq{
\question{There C, C++ library I need has not been ported to Lisp. 
What should I do?}
\answer{(cffi, verrazano stuff)}
}

 @ http://www.cliki.net/SWIG

**
#| original |#
\faq{
\question{I am a developer. I hear about some complications with 
licensing. Should I release my code under GPL, LGPL, BSD, 
LLGP?}
\answer{(said LGPL is focused on C-like linking and stuff, and it is 
not clear yet what does that mean in Lisp)}
}

#| suggested change |#

\faq{
\question{What are the commonly applied source (or other) licences?
How well do they work?  Which should I use when I release my code?}
\answer{There is some information at http://www.cliki.net/license

 @ LGPL: is focused on C-like linking and stuff, and it is not clear
 yet what does that mean in Lisp
}
}


More information about the cl-faq mailing list