Re: Checksum (noob)
- From: Bill Atkins <not-a-real-email-address@xxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 27 Dec 2006 17:58:09 -0500
"atgraham@xxxxxxxxx" <atgraham@xxxxxxxxx> writes:
After a 10 year hiatus, I've begun experimenting with Lisp again.
Among other things, I've written a typical 8-bit modulo arithmetic
checksum routine, which would look like this in C++:
std::accumulate(lst.begin(), lst.end(),
0, std::minus<unsigned char>());
Lisp version:
(defun checksum (lst)
(logand (- 0 (apply '+ lst)) #xff))
I like it. However, I'm wondering if it's appropriate to be paranoid
about a user passing invalid parameters to such a function (lists of
lists, integers, etc.) and causing havoc at runtime.
There shouldn't be any havoc - if the caller passes anything but a
list of numbers, a condition will be signaled. If you are using SLIME
(or any other Lisp environment), you'll get a special interface to
deal with conditions when they arise.
Generally, the approach taken in dynamically-typed languages is to
make type-checking implicit in your code and your documentation,
i.e. if someone breaks CHECKSUM's contract and passes an LST
containing invalid data, the + call becomes invalid and an error will
be signalled at runtime. If the input to checksum is coming from an
external source (like a network connection or typed user input), it
might be worthwhile to check the types yourself so you can present a
more meaningful error message. Otherwise, any type-checking you might
do in CHECKSUM itself would be superfluous, since + checks types on
its own.
Does the following code do what you want:
(defun checksum (list)
(mod (reduce #'+ list) 256))
?
(The REDUCE and APPLY calls are nearly equivalent, except that the
list passed to APPLY can't be longer than CALL-ARGUMENTS-LIMIT; REDUCE
doesn't suffer from this limitation.)
In general, code that deals with low-level bit representations is not
in the Lisp spirit, at least IMO.
I've been looking over Graham's book, and he stresses the importance of
indentation. Traditionally, I've shunned emacs, since it seems to get
*all* default behavior wrong, in every situation, and I don't have the
willpower to maintain a configuration. But I thought I would give it
another try, since it seems to have been designed specifically for
Lisp. Alas, it gets the indentation wrong, or at least it's different
than what Graham suggests (for example, in "if" expressions, it indents
the "then" differently than the "else"). Is there a configuration
parameter that I can use to fix it?
(setq indent-lisp-correctly t) ; wouldn't that be nice
(setq lisp-indent-function 'common-lisp-indent-function)
HTH,
Bill
.
- Follow-Ups:
- Re: Checksum (noob)
- From: Peter Scott
- Re: Checksum (noob)
- References:
- Checksum (noob)
- From: atgraham@xxxxxxxxx
- Checksum (noob)
- Prev by Date: Re: Checksum (noob)
- Next by Date: a bug in SBCL 1.0 under Windows?
- Previous by thread: Re: Checksum (noob)
- Next by thread: Re: Checksum (noob)
- Index(es):
Relevant Pages
|
|