Common Lisp Equivalent of `man` - lisp

I'm using clozure common lisp. Is there a common lisp equivalent of the unix man command or something analgous?

Start with describe.
You might also want to consider documentation, but that is lower level.

As jkiiski commented you, Hyperspec is the best online documentation of standard functions for Common Lisp. You can download it for offline use here.
If you also want to document your own code, you can use either:
(defun simple-function (a b)
"Documentation of a simple function"
(+ a b))
or this:
(defun simple-function (a b)
(:documentation "Documentation of a simple function")
(+ a b))
as sds suggested, you can access the docstring by:
(describe #'simple-function)
or in an interactive way with:
(inspect #'simple-function)
A good SO answer about Common Lisp documentation can be found here.

Related

Ordering of needle / haystack in Lisp functions

While learning Lisp, I've seen that if there are two parameters to a function, where one is a single element or a subset (needle), and the other is a list (haystack), the element or subset always comes first.
Examples:
(member 3 '(3 1 4 1 5))
(assoc 'jane '((jane doe)
(john doe)))
(subsetp '(a e) '(a e i o u))
To me, it seems as if there was a rule in Lisp that functions should follow this guidance: Part first, entire thing second.
Is this finding actually based on a guideline in Lisp, or is it accidentally?
Functions like member and assoc are at least from 1960.
I would simply expect that it followed mathematical notation, for example in set theory:
e ∈ m
Since Lisp uses prefix notation, the predicate/function/operator comes first, the element second and the set is third:
(∈ e m)
John McCarthy had a Ph.D. in Mathematics.
Generally it is also more useful in Common Lisp to have set-like argument last:
(defun find-symbol (name package) ...)
The actual definition in Common Lisp is:
(defun find-symbol (name &optional (package *package*)) ...)
This allows us to use the current package as a useful default.
Lets see. The first McCarthy LISP from 1960 had the list sometimes as the first argument. See page 123 in this LISP manual. E.g.
;; 1960 maplist
(defun maplist (list function)
...)
Now this is perhaps because this function was one of the first higher order functions that were made. In fact it predated the first implementation as it was in the first Lisp paper. In the same manual on page 125 you'll find sassoc and it looks very much like assoc today:
(defun sassoc (needle haystack default-function)
...)
Both of these look the same in the next version 1.5 of the language. (See page 63 for maplist and 60 for sassoc)
From here to Common Lisp there are divergent paths that joins again. A lot of new ideas came about but there has to be a reason to break compatibility to actually do it. I can think of one reason and that is support for multiple lists. In Common Lisp maplist is:
(defun maplist (function &rest lists+)
...)
A quick search in the CLHS for common argument names in "wrong" order gave me fill, map-into, and sort. There might be more.
Peter Norvigs style guide say to follow conventions but not more detailed than that. When reading Scheme SRFIs they often mention defacto implementations around and what Common Lisp has as solution before suggesting something similar as a standard. I do the same when choosing how to implement things.

How do I write anaphoric macros in portable scheme?

I'm exploring Scheme macros, but I've been unable to find a portable way of writing anaphoric macros.
I'm trying to write an each-it macro, such that this code:
(each-it (list 1 2 3)
(display it))
Expands to this:
(for-each (lambda (it)
(display it))
(list 1 2 3))
I've written a macro with syntax-rules, but this gives me an error about an undefined identifier when I try to use it.
(define-syntax each-it
(syntax-rules ()
((each-it lst body)
(for-each (lambda (it) body)
lst))))
This SO question mentions define-syntax-parameter, which seems to be Racket only. This blog post gives some Scheme code samples, but the code samples don't run in DrRacket in R5RS mode (I think it's the square brackets?).
R4RS has an interesting macro appendix but it is not present in R5RS and I don't know if I can depend on it.
Can I write my each-it macro in a completely portable way? If not, what are the most widely available macro system features for writing my macro?
This should be portable, at least in R6RS:
(define-syntax each-it
(lambda (x)
(syntax-case x ()
((_ lst body)
(with-syntax ((it (datum->syntax x 'it)))
#'(for-each (lambda (it) body) lst))))))
Yes, you can write it in a portable way assuming that R6RS is portable enough for you. (The same cannot be said on R7RS, which currently has nothing more than just syntax-rules, and it's unclear what will be included in the large language, or when it will happen.) See uselpa's for how to do that.
So why am I writing another answer? Because actually doing that is going to be a bad idea. A bad idea not in some vague academic sense that doesn't matter for most real world code -- bad in a sense that is likely to bite you later on. I know that "paper" makes it look intimidating, but read at least the first two sections of the paper mentioned in the other SO question you've seen. Specifically, Section 1.2 shows the problem you'll be running against. Then, Section 2 shows how to do it "properly", in a way that makes it tedious to write macros that expand to uses of your macro. At this point, it will be appealing to take the "just keep it hygienic", but at the end of Section 2 you'll see why that's not working either.
The bottom line, IMO, is to just not do it unless you have syntax parameters or something similar. Maybe the only exception to that (which might be your case) is when the macro is something that you intend to use yourself, and you will never provide it to others.

Why am I getting an unbound error for "atom?"

I'm trying to go through "The Little Lisper" and already running into snags in the first chapter. I'm relatively new to Emacs (which has fueled my interest in learning Lisp and clojure). I downloaded the Mit-scheme app, and am working the exercises on Edwin.
I'm trying:
(atom? (cons a l))
where a is an atom and l is a list already defined. I get the following error:
;Unbound variable: atom?
Why? I have no problems using the "null?" function. I thought "atom?" is an internal function checking to see if the value returned is an atom.
Any explanation would be much appreciated. I still haven't set up my emacs to run scheme, and the minor differences between all the lisp dialects is testing my patience.
In "The Little Schemer" ("The Little Lisper"'s updated version) the atom? procedure is defined as follows (because atom? doesn't exist in Scheme):
(define (atom? x)
(and (not (null? x))
(not (pair? x))))
If you're following an old version of the book, I advise you to either look for a newer version or use the same programming language used in the book: Common Lisp for The Little Lisper, Scheme for The Little Schemer - and Racket is a great Scheme IDE to work with! take a look at this answer for some tips when going through The Little Schemer using Racket.
I'm trying to go through "The Little Lisper"...
I downloaded the Mit-scheme
Common Lisp and Scheme are very different languages.
You have to either use a different book (e.g., SICP) to match your language implementation or a different language implementation (e.g., clisp or sbcl) to match your book.
Take a look at the Scheme R5RS specification; it includes a list of functions and syntactic keywords. Although not part of the Scheme standard, mit-scheme has a function apropos that will find functions (other stuff) with a given name. try:
(apropos "atom")
(but it won't show anything, :-).
An atom is something that is not a 'cons cell' (generally, if I remember my CommonLisp). In Scheme you could implement one as:
(define (atom? thing) (not (pair? thing)))
Note: this definition of atom? is consistent with CommonLisp atom.

Expand a macro form completely

I'd like to learn the internals of Lisp, so I want to see how everything is implemented.
For example,
(macroexpand '(loop for i upto 10 collect i))
gives me (in SBCL)
(BLOCK NIL
(LET ((I 0))
(DECLARE (TYPE (AND NUMBER REAL) I))
(SB-LOOP::WITH-LOOP-LIST-COLLECTION-HEAD (#:LOOP-LIST-HEAD-1026
#:LOOP-LIST-TAIL-1027)
(SB-LOOP::LOOP-BODY NIL
(NIL NIL (WHEN (> I '10) (GO SB-LOOP::END-LOOP)) NIL)
((SB-LOOP::LOOP-COLLECT-RPLACD
(#:LOOP-LIST-HEAD-1026 #:LOOP-LIST-TAIL-1027)
(LIST I)))
(NIL (SB-LOOP::LOOP-REALLY-DESETQ I (1+ I))
(WHEN (> I '10) (GO SB-LOOP::END-LOOP)) NIL)
((RETURN-FROM NIL
(SB-LOOP::LOOP-COLLECT-ANSWER
#:LOOP-LIST-HEAD-1026)))))))
But LOOP-BODY, WITH-LOOP-LIST-COLLECTION-HEAD, etc. are still macros. How can I expand a macro form completely?
To see the full expansion one needs to walk the Lisp form on all levels and expand them. For this it is necessary that this so-called code walker understands Lisp syntax (and not just s-expression syntax). For example in (lambda (a b) (setf a b)), the list (a b) is a parameter list and should not be macro expanded.
Various Common Lisp implementations provide such a tool. The answer of 6502 mentions MACROEXPAND-ALL which is provided by SBCL.
If you use a development environment, it is usually provided as a command:
SLIME: M-x slime-macroexpand-all with C-c M-m
LispWorks: menu Expression > Walk or M-x Walk Form, shorter M-Sh-m.
The other answers are excellent for you question but you say you want to see how everything is implemented.
Many macros (as you know already) are implemented using macros and whilst macroexpand-all is very useful but you can lose the context of what macro was responsible for what change.
One nice middle ground (if you are using slime) is to use slime-expand-1 (C-c Enter) which shows the expansion is another buffer. You can then useslime-expand-1 inside this new buffer to expand macros in-place.
This allows you to walk the tree expanding as you read and also to use undo to close the expansions again.
For me this has been a god-send in understanding other people's macros. Hope this helps you too, have fun!
You can try to use MACROEXPAND-ALL but what you may get is not necessarily useful.
In something like LOOP the real meat is the macro itself, not the generated code.
(Note: If you're not interested in portability, SBCL provides macroexpand-all, which will do what you're after. If you're after a portable solution, read on...)
The quick-and-dirty solution would be to macroexpand the form itself, then recursively macroexpand all but the first element of the resulting list. This is an imperfect solution; it will fail completely the moment it attempts to process a let's bindings (the first argument to let, the list of bindings, is not meant to be macroexpanded, but this code will do it anyway).
;;; Quick-and-dirty macroexpand-all
(defun macroexpand* (form)
(let ((form (macroexpand form)))
(cons (car form) (mapcar #'macroexpand (cdr form)))))
A more complete solution would consider special forms specially, not macroexpanding their unevaluated arguments. I could update with such a solution, if desired.

Which dialect of LISP is 'Paradigms of Artificial Intelligence Programming' written in?

What version/dialect/implementation of LISP is this meant to run on?
(I do understand that the book is written in Common LISP (as specified in the introduction) and that it predates the 1994 CL standard).
Split question into two to make it clearer.
Those aren't functions. Those are variable bindings. Not everything that appears as the first thing in a form is the name of a function; the enclosing form may introduce special meaning to internal forms. That's the case with e.g. let:
(let ((action 42)
(result 51))
(+ action result))
Neither action nor result names a function in that example.
If we look at auxfns.lisp found on Peter Norvig's web page for the book, there's this bit
(eval-when (eval compile load)
;; Make it ok to place a function definition on a built-in LISP symbol.
#+(or Allegro EXCL)
(dolist (pkg '(excl common-lisp common-lisp-user))
(setf (excl:package-definition-lock (find-package pkg)) nil))
;; Don't warn if a function is defined in multiple files --
;; this happens often since we refine several programs.
#+Lispworks
(setq *PACKAGES-FOR-WARN-ON-REDEFINITION* nil)
#+LCL
(compiler-options :warnings nil)
)
which suggests its supposed to work in Franz Allegro, Lucid Lisp, or Lispworks
The dialect of Lisp is called 'Common Lisp'. The book is written in relatively portable Common Lisp.