Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

format question

36 views
Skip to first unread message

Nils Goesche

unread,
Nov 28, 2001, 12:16:35 PM11/28/01
to
Hi!

Can somebody think of a more elegant way to do this:

(format nil "~@[~D~]~{+~D~}" (car lst) (cdr lst))

where `lst' is a (possibly empty) list of integers?

Regards,
--
Nils Goesche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x42B32FC9

Tim Moore

unread,
Nov 28, 2001, 6:21:01 PM11/28/01
to
In article <lk8zcqo...@pc022.bln.elmeg.de>, "Nils Goesche"
<car...@cartan.de> wrote:

> Hi!
> Can somebody think of a more elegant way to do this:
> (format nil "~@[~D~]~{+~D~}" (car lst) (cdr lst))
>where `lst' is a (possibly empty)
> list of integers?

Elegance is in the eye of the beholder, you might find this more elegant:
(format nil "~{~D~^+~}" lst)

Tim

Nils Goesche

unread,
Nov 29, 2001, 7:00:40 AM11/29/01
to
"Tim Moore" <mo...@bricoworks.com> writes:

DARN! That's what I couldn't find. Thanks very much. Is this
anywhere documented in the HyperSpec, BTW? All I can find is

# Finally, the ~^ directive can be used to terminate the iteration
# prematurely.

in 22.3.7.4. Does that mean precisely the behavior of your example,
that is, it will terminate prematurely iff the last element of the
list has already been processed?

Janis Dzerins

unread,
Nov 29, 2001, 9:24:30 AM11/29/01
to
Nils Goesche <car...@cartan.de> writes:

> Can somebody think of a more elegant way to do this:
>
> (format nil "~@[~D~]~{+~D~}" (car lst) (cdr lst))
>
> where `lst' is a (possibly empty) list of integers?

This is not what you may have asked for, but yes:

(format nil "~@[~D~]~{+~D~}" (car list) (cdr list))

where `list' is a (possibly empty) list of integers.

--
Janis Dzerins

Eat shit -- billions of flies can't be wrong.

Tim Moore

unread,
Nov 29, 2001, 12:23:28 PM11/29/01
to
In article <lkn115n...@pc022.bln.elmeg.de>, "Nils Goesche"
<car...@cartan.de> wrote:


> "Tim Moore" <mo...@bricoworks.com> writes:
>> In article <lk8zcqo...@pc022.bln.elmeg.de>, "Nils Goesche"
>> <car...@cartan.de> wrote:
>>
>> > Can somebody think of a more elegant way to do this:
>
>> > (format nil "~@[~D~]~{+~D~}" (car lst) (cdr lst))
>
>> >where `lst' is a (possibly empty) list of integers?
>> Elegance is in the eye of the beholder, you might find this more
>> elegant: (format nil "~{~D~^+~}" lst)
> DARN! That's what I couldn't find. Thanks very much. Is this anywhere
> documented in the HyperSpec, BTW? All I can find is # Finally, the ~^
> directive can be used to terminate the iteration # prematurely.
> in 22.3.7.4. Does that mean precisely the behavior of your example,
> that is, it will terminate prematurely iff the last element of the list
> has already been processed?

22.3.9.2 "Tilde Circumflex: Escape Upward" covers all you want to know,
and more, about ~^. Basically, ~^ exits the loop if there are no more
arguments.

Tim

Kent M Pitman

unread,
Nov 29, 2001, 3:41:07 PM11/29/01
to
Nils Goesche <car...@cartan.de> writes:

> "Tim Moore" <mo...@bricoworks.com> writes:
>
> > In article <lk8zcqo...@pc022.bln.elmeg.de>, "Nils Goesche"
> > <car...@cartan.de> wrote:
> >
> > > Can somebody think of a more elegant way to do this:
>
> > > (format nil "~@[~D~]~{+~D~}" (car lst) (cdr lst))
>
> > >where `lst' is a (possibly empty) list of integers?
> >
> > Elegance is in the eye of the beholder, you might find this more elegant:
> > (format nil "~{~D~^+~}" lst)
>
> DARN! That's what I couldn't find. Thanks very much. Is this
> anywhere documented in the HyperSpec, BTW? All I can find is
>
> # Finally, the ~^ directive can be used to terminate the iteration
> # prematurely.
>
> in 22.3.7.4.

The CLHS index pre-version 5 doesn't well-index either format ops or
# readmacros. You want version 5 or above. (If it has a gray background,
you have a too-old version.)

To my knowledge, v5 is not downloadable directly from the Xanaly web site.
Not sure why. The way to get it is to download the Personal Edition of
Xanalys LispWorks (if you don't already have a Professional or Enterprise
edition) and unpack the browsable documentation. That has a v5. I don't
think they are trying to hide it there--probably just no one ever got around
to updating the web site.

v5 has very few other differences from v3. It doesn't have the little java
widget that prompts for a symbol name though. There were too many reports
of browsers not handling that correctly, so the widget was removed. You have
to click through to where you want, which I find is mostly just as fast as
typing, even for me who types fast.

Nils Goesche

unread,
Nov 29, 2001, 12:16:45 PM11/29/01
to
Janis Dzerins <jo...@latnet.lv> writes:

> Nils Goesche <car...@cartan.de> writes:
>
> > Can somebody think of a more elegant way to do this:
> >
> > (format nil "~@[~D~]~{+~D~}" (car lst) (cdr lst))
> >
> > where `lst' is a (possibly empty) list of integers?
>
> This is not what you may have asked for, but yes:
>
> (format nil "~@[~D~]~{+~D~}" (car list) (cdr list))
>
> where `list' is a (possibly empty) list of integers.

Yes, yes. I know there are seperate function and value slots in
symbols; but using them like this still frightens me...

Pierre R. Mai

unread,
Nov 29, 2001, 8:29:32 PM11/29/01
to
Nils Goesche <car...@cartan.de> writes:

> > This is not what you may have asked for, but yes:
> >
> > (format nil "~@[~D~]~{+~D~}" (car list) (cdr list))
> >
> > where `list' is a (possibly empty) list of integers.
>
> Yes, yes. I know there are seperate function and value slots in
> symbols; but using them like this still frightens me...

Try to overcome that fear. It will be worth it. And trust me on the
sun-screen...

Regs, Pierre.

--
Pierre R. Mai <pm...@acm.org> http://www.pmsf.de/pmai/
The most likely way for the world to be destroyed, most experts agree,
is by accident. That's where we come in; we're computer professionals.
We cause accidents. -- Nathaniel Borenstein

Nils Goesche

unread,
Nov 30, 2001, 2:52:56 PM11/30/01
to
Kent M Pitman <pit...@world.std.com> writes:

> Nils Goesche <car...@cartan.de> writes:
>
> > "Tim Moore" <mo...@bricoworks.com> writes:
> >
> > > Elegance is in the eye of the beholder, you might find this more elegant:
> > > (format nil "~{~D~^+~}" lst)
> >
> > DARN! That's what I couldn't find. Thanks very much. Is this
> > anywhere documented in the HyperSpec, BTW? All I can find is
> >
> > # Finally, the ~^ directive can be used to terminate the iteration
> > # prematurely.
> >
> > in 22.3.7.4.
>
> The CLHS index pre-version 5 doesn't well-index either format ops or
> # readmacros. You want version 5 or above. (If it has a gray background,
> you have a too-old version.)

Thanks, got it. Unfortunately, hyperspec-naggum.el from ILISP doesn't
work with the new CLHS anymore. (fun_format.htm is now named
f_format.htm, for instance). Has anybody made a new version of
hyperspec-naggum.el that works with the new version?

"Tim Moore" <mo...@bricoworks.com> writes:

> 22.3.9.2 "Tilde Circumflex: Escape Upward" covers all you want to
> know, and more, about ~^. Basically, ~^ exits the loop if there are
> no more arguments.

Actually, I would have found it if I had not forgotten that
`Circumflex' is the name of the ^ character :-)

Dr. Edmund Weitz

unread,
Nov 30, 2001, 7:58:37 PM11/30/01
to
Nils Goesche <car...@cartan.de> writes:

> Kent M Pitman <pit...@world.std.com> writes:
>
> > The CLHS index pre-version 5 doesn't well-index either format ops
> > or # readmacros. You want version 5 or above. (If it has a gray
> > background, you have a too-old version.)
>
> Thanks, got it. Unfortunately, hyperspec-naggum.el from ILISP
> doesn't work with the new CLHS anymore. (fun_format.htm is now
> named f_format.htm, for instance). Has anybody made a new version
> of hyperspec-naggum.el that works with the new version?

Until I read Mr. Pitman's note above, I didn't know that there were
significant differences between the CLHS versions (except for the
background color... :)

I include a quick fix with this article (see below). It works on my
laptop (Emacs 21.1, ILISP 5.11.1 from CVS I think, plus CLHS from
LispWorks for Linux Personal Edition). Bear with me if it's ugly,
grossly inefficient, or if it simply doesn't work with your setup. I
just hacked it together, and I'm not at all an Emacs Lisp wizard.

(Credits go to Daniel Barlow. I had to look at his version to find out
that such thing as a symbol table for the CLHS existed.)

HTH,
Edi.

;;; hyperspec.el --- Browse documentation from the Common Lisp HyperSpec

;; Copyright 1997 Naggum Software

;; Author: Erik Naggum <er...@naggum.no>
;; Keywords: lisp

;; This file is not part of GNU Emacs, but distributed under the same
;; conditions as GNU Emacs, and is useless without GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;; Kent Pitman and the Harlequin Group have made the text of American
;; National Standard for Information Technology -- Programming Language --
;; Common Lisp, ANSI X3.226-1994 available on the WWW, in the form of the
;; Common Lisp HyperSpec. This package makes it convenient to peruse this
;; documentation from within Emacs.

;;; Code:

(require 'cl)
(require 'browse-url) ;you need the Emacs 20 version
(require 'thingatpt)

(defvar common-lisp-hyperspec-root
"http://www.xanalys.com/software_tools/reference/HyperSpec/"
"The root of the Common Lisp HyperSpec URL.
If you copy the HyperSpec to your local system, set this variable to
something like \"file:/usr/local/doc/HyperSpec/\".")

(defvar common-lisp-hyperspec-symbol-table
"/usr/local/lib/LispWorksPersonal/lib/4-1-0-0/manual/online/web/CLHS/Data/Map_Sym.txt"
"The HyperSpec symbol table file. Must be a local file.")

(defvar common-lisp-hyperspec-history nil
"History of symbols looked up in the Common Lisp HyperSpec.")

;;if only we had had packages or hash tables..., but let's fake it.

(defvar common-lisp-hyperspec-symbols (make-vector 67 0))

(defun common-lisp-hyperspec (symbol-name)
"View the documentation on SYMBOL-NAME from the Common Lisp HyperSpec.
If SYMBOL-NAME has more than one definition, all of them are displayed with
your favorite browser in sequence. The browser should have a \"back\"
function to view the separate definitions.

The Common Lisp HyperSpec is the full ANSI Standard Common Lisp, provided
by Kent Pitman and the Harlequin Group. By default, the Harlequin WWW site
is visited to retrieve the information. The Harlequin Group allows you to
transfer the entire Common Lisp HyperSpec to your own site under certain
conditions. Visit http://www.harlequin.com/books/HyperSpec/ for more
information. If you copy the HyperSpec to another location, customize the
variable `common-lisp-hyperspec-root' to point to that location."
(interactive (list (let ((symbol-at-point (thing-at-point 'symbol)))
(if (and symbol-at-point
(intern-soft (downcase symbol-at-point)
common-lisp-hyperspec-symbols))
symbol-at-point
(completing-read
"Look up symbol in Common Lisp HyperSpec: "
common-lisp-hyperspec-symbols #'boundp
t symbol-at-point
'common-lisp-hyperspec-history)))))
(maplist (lambda (entry)
(browse-url (concat common-lisp-hyperspec-root "Body/" (car entry)))
(if (cdr entry)
(sleep-for 1.5)))
(let ((symbol (intern-soft (downcase symbol-name)
common-lisp-hyperspec-symbols)))
(if (and symbol (boundp symbol))
(symbol-value symbol)
(error "The symbol `%s' is not defined in Common Lisp"
symbol-name)))))

;;; Added the following just to provide a common entry point according
;;; to the various 'hyperspec' implementations.
;;;
;;; 19990820 Marco Antoniotti

(eval-when (load eval)
(setf (symbol-function 'hyperspec-lookup)
(symbol-function 'common-lisp-hyperspec)))

(mapcar (lambda (entry)
(let ((symbol (intern (car entry) common-lisp-hyperspec-symbols)))
(if (boundp symbol)
(push (cadr entry) (symbol-value symbol))
(set symbol (cdr entry)))))
(let ((index-buffer (find-file-noselect common-lisp-hyperspec-symbol-table)))
(labels ((one-item ()
(delete ?\n (delete ?\r (thing-at-point 'line)))))
(save-excursion
(set-buffer index-buffer)
(goto-char (point-min))
(loop with temp1 and temp2
while (< (point) (point-max))
do (setq temp1 (downcase (one-item)))
(forward-line)
(setq temp2 (one-item))
(forward-line)
collect (list temp1
(subseq temp2
(1+ (position ?\/ temp2 :from-end t)))))))))

(provide 'hyperspec)

;;; hyperspec.el ends here

Kent M Pitman

unread,
Nov 30, 2001, 7:58:08 PM11/30/01
to
Nils Goesche <car...@cartan.de> writes:

> Kent M Pitman <pit...@world.std.com> writes:
>
> > The CLHS index pre-version 5 doesn't well-index either format ops or
> > # readmacros. You want version 5 or above. (If it has a gray background,
> > you have a too-old version.)
>
> Thanks, got it. Unfortunately, hyperspec-naggum.el from ILISP doesn't
> work with the new CLHS anymore. (fun_format.htm is now named
> f_format.htm, for instance). Has anybody made a new version of
> hyperspec-naggum.el that works with the new version?

Does it have the data file literally included into it? It should load
it dynamically from the data directory, then it would compatibly see
the updates. It should look for either the short filename or the long
filename, though; I had no choice but to change the name in order to keep
filenames DOS compliant (i.e., compatible with the least common denominator
file system I could imagine, so no one would bother me more about file
system non-compliances).

In v5 and above, the Data dir contains these files:

Map_IssW.txt
Map_IssX.txt
Map_Sym.txt

I presume Erik's package cares mostly about the last one.

Keeping compatibility with earlier versions by having it also look for
the older, longer filenames (the names of which I don't have access to
on this computer I'm typeing to at the moment) seems worthwhile.

Dr. Edmund Weitz

unread,
Nov 30, 2001, 8:01:17 PM11/30/01
to
Kent M Pitman <pit...@world.std.com> writes:

> Nils Goesche <car...@cartan.de> writes:
>
> > Kent M Pitman <pit...@world.std.com> writes:
> >
> > > The CLHS index pre-version 5 doesn't well-index either format
> > > ops or # readmacros. You want version 5 or above. (If it has a
> > > gray background, you have a too-old version.)
> >
> > Thanks, got it. Unfortunately, hyperspec-naggum.el from ILISP
> > doesn't work with the new CLHS anymore. (fun_format.htm is now
> > named f_format.htm, for instance). Has anybody made a new version
> > of hyperspec-naggum.el that works with the new version?
>
> Does it have the data file literally included into it?

It doesn't. I just posted a quick fix in the other thread, see
<news:m3adx3l...@bird.agharta.de>.

Edi.

Dr. Edmund Weitz

unread,
Nov 30, 2001, 8:03:30 PM11/30/01
to
e...@agharta.de (Dr. Edmund Weitz) writes:

> Kent M Pitman <pit...@world.std.com> writes:
>
> > Does it have the data file literally included into it?
>
> It doesn't. I just posted a quick fix in the other thread, see
> <news:m3adx3l...@bird.agharta.de>.

Actually I wanted to say "It does". I should go to bed now... :)

Erik Naggum

unread,
Nov 30, 2001, 8:17:26 PM11/30/01
to
* Kent M Pitman

| Does it have the data file literally included into it?

I found no such thing in the version for which I wrote this package.

///
--
The past is not more important than the future, despite what your culture
has taught you. Your future observations, conclusions, and beliefs are
more important to you than those in your past ever will be. The world is
changing so fast the balance between the past and the future has shifted.

Dr. Edmund Weitz

unread,
Nov 30, 2001, 8:25:13 PM11/30/01
to
Kent M Pitman <pit...@world.std.com> writes:

> Nils Goesche <car...@cartan.de> writes:
>
> > Kent M Pitman <pit...@world.std.com> writes:
> >
> > > The CLHS index pre-version 5 doesn't well-index either format
> > > ops or # readmacros. You want version 5 or above. (If it has a
> > > gray background, you have a too-old version.)
> >
> > Thanks, got it. Unfortunately, hyperspec-naggum.el from ILISP
> > doesn't work with the new CLHS anymore. (fun_format.htm is now
> > named f_format.htm, for instance). Has anybody made a new version
> > of hyperspec-naggum.el that works with the new version?

If you're like me and you dig the KDE web/file browser for its "Web
Shortcuts" (like 'ggg:split-sequence' for a Google Group Search),
here's another quickie:

If you have a current version of LispWorks (or another local copy of
the CLHS), cd to the directory where the CLHS symbol table is
located. On my machine (LispWorks for Linux Personal Edition 4.1.20)
the file you're looking for is "Map_Sym.txt" and the directory is
"/usr/local/lib/LispWorksPersonal/lib/4-1-0-0/manual/online/web/CLHS/Data/".

Start the little Perl script below with Map_Sym.txt redirected to
stdin, i.e. something like './clhs.pl < Map_Sym.txt'.

Now you can start KDEs Konqueror, open its settings and add a new 'Web
Shortcut', something like:

Search Provider Name:
Common Lisp HyperSpec

Search URI:
file:/usr/local/lib/LispWorksPersonal/lib/4-1-0-0/manual/online/web/CLHS/Body/link_\1.html

URI shortcut:
cl

You can now type URIs like 'cl:*print-circle*' or 'cl:make-method' and
Konqueror will open the correct page from your local copy of the
CLHS.

Hope you like it,
Edi.

Note 1: The following six entries don't work due to the Unix file
naming conventions:

/
//
///
/=
CHAR/=
STRING/=

Note 2: All input should be lowercase. If you don't like that, replace
'lc' with '$_' in the Perl script.

Note 3: Sorry for using Perl instead of CL. But I think this is the
kind of stuff Perl was originally intended for... :)

#!/usr/bin/perl

while (<>) {
next
if m#/#;
chomp;
$url = <>;
chomp $url;
link $url, sprintf "../Body/link_%s.html", lc
or die $!;
}

Dr. Edmund Weitz

unread,
Dec 1, 2001, 2:41:12 AM12/1/01
to
e...@agharta.de (Dr. Edmund Weitz) writes:

> I include a quick fix with this article (see below).

Er, here's an improved version. It was too late yesterday... :)

Edi.

;; Copyright 1997 Naggum Software

;;; Commentary:

;;; Code:

;;; Added variable for CLHS symbol table. See details below.
;;;
;;; 20011201 Edi Weitz

(defvar common-lisp-hyperspec-symbol-table
"/usr/local/lib/LispWorksPersonal/lib/4-1-0-0/manual/online/web/CLHS/Data/Map_Sym.txt"
"The HyperSpec symbol table file. Must be a local file.")

(defvar common-lisp-hyperspec-history nil
"History of symbols looked up in the Common Lisp HyperSpec.")

;;if only we had had packages or hash tables..., but let's fake it.

(defvar common-lisp-hyperspec-symbols (make-vector 67 0))

;;; Removed facility for multiple definition because the symbol table
;;; has only one entry per SYMBOL-NAME
;;;
;;; 20011201 Edi Weitz

(defun common-lisp-hyperspec (symbol-name)
"View the documentation on SYMBOL-NAME from the Common Lisp HyperSpec.

The Common Lisp HyperSpec is the full ANSI Standard Common Lisp, provided


by Kent Pitman and the Harlequin Group. By default, the Harlequin WWW site
is visited to retrieve the information. The Harlequin Group allows you to
transfer the entire Common Lisp HyperSpec to your own site under certain
conditions. Visit http://www.harlequin.com/books/HyperSpec/ for more
information. If you copy the HyperSpec to another location, customize the
variable `common-lisp-hyperspec-root' to point to that location."
(interactive (list (let ((symbol-at-point (thing-at-point 'symbol)))
(if (and symbol-at-point
(intern-soft (downcase symbol-at-point)
common-lisp-hyperspec-symbols))
symbol-at-point
(completing-read
"Look up symbol in Common Lisp HyperSpec: "
common-lisp-hyperspec-symbols #'boundp
t symbol-at-point
'common-lisp-hyperspec-history)))))

(browse-url (concat common-lisp-hyperspec-root "Body/"


(let ((symbol (intern-soft (downcase symbol-name)
common-lisp-hyperspec-symbols)))
(if (and symbol (boundp symbol))
(symbol-value symbol)
(error "The symbol `%s' is not defined in Common Lisp"

symbol-name))))))

;;; Added the following just to provide a common entry point according
;;; to the various 'hyperspec' implementations.
;;;
;;; 19990820 Marco Antoniotti

(eval-when (load eval)
(setf (symbol-function 'hyperspec-lookup)
(symbol-function 'common-lisp-hyperspec)))

;;; Replaced hard-coded look-up table with loop through CLHS symbol table
;;; after reading KMP's posting to comp.lang.lisp:
;;; <http://groups.google.com/groups?selm=sfwvgftux7g.fsf%40shell01.TheWorld.com>
;;;
;;; 20011201 Edi Weitz

(let ((index-buffer (find-file-noselect common-lisp-hyperspec-symbol-table)))
(labels ((one-item ()
(delete ?\n (delete ?\r (thing-at-point 'line)))))
(save-excursion
(set-buffer index-buffer)
(goto-char (point-min))

(while (< (point) (point-max))
(let ((symbol (intern (downcase (one-item))
common-lisp-hyperspec-symbols))
relative-url)
(forward-line)
(setq relative-url (one-item))
(forward-line)
(set symbol (subseq relative-url
(1+ (position ?\/ relative-url :from-end t)))))))))

Paolo Amoroso

unread,
Dec 1, 2001, 11:49:35 AM12/1/01
to
On Sat, 1 Dec 2001 00:58:08 GMT, Kent M Pitman <pit...@world.std.com>
wrote:

> Nils Goesche <car...@cartan.de> writes:
[...]


> > Thanks, got it. Unfortunately, hyperspec-naggum.el from ILISP doesn't
> > work with the new CLHS anymore. (fun_format.htm is now named

[...]


> Does it have the data file literally included into it? It should load

The ILISP distribution comes with the following HyperSpec lookup tools in
the `extra' directory:

hyperspec-barlow.el
hyperspec-carney.el
hyperspec-naggum.el

Only hyperspec-naggum.el contains a hardcoded symbol index.


Paolo
--
EncyCMUCLopedia * Extensive collection of CMU Common Lisp documentation
http://web.mclink.it/amoroso/ency/README
[http://cvs2.cons.org:8000/cmucl/doc/EncyCMUCLopedia/]

0 new messages