Re: Checksum (noob)



"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
.



Relevant Pages

  • Checksum (noob)
    ... I've begun experimenting with Lisp again. ... checksum routine, which would look like this in C++: ... lists, integers, etc.) and causing havoc at runtime. ... Alas, it gets the indentation wrong, or at least it's different ...
    (comp.lang.lisp)
  • Re: Checksums and file sizes
    ... > able to figure out which of the hundreds of files it lists is the ... My only concern was the large number of file size and checksum differences ... is getting the Belkin Bulldog monitor to communicate over the serial port ... have to do is not leave the system up on Open Server when I leave for more ...
    (comp.unix.sco.misc)
  • Re: Cant understand this code snippet
    ... I only know it's trying to XOR the process ID. ... ps -A lists all the processes on a Unix machine. ... giving you a list of random longs. ... It causes pack to compute a checksum out of those ...
    (perl.beginners)
  • Re: Comparing large files under linux to find duplicates
    ... I have 8x16gb files that are basically just giant lists. ... I need to compare each file against every other file and list ... just don't know how to list duplicates amongst the files. ... file, checksum the pieces, and compare the checksums. ...
    (comp.os.linux.misc)
  • Re: re(4) incorrect checksum
    ... Pyun YongHyeon schreef: ... >> Hi lists, ... >> yongari Exp $) ... >> I get checksum errors on every packet I send, ...
    (freebsd-hackers)