Subject: Re: low-level hacking [was Re: Calling All Noobs... ]
From: (Rob Warnock)
Date: Wed, 11 Jan 2006 06:04:28 -0600
Newsgroups: comp.lang.lisp
Message-ID: <>
John Thingstad <> wrote:
| Rob Warnock <> wrote:
| > (defun w32 (addr &rest values)
| >   (declare (optimize (speed 3) (debug 0) (safety 0)))
| >   (loop for i fixnum from 0 by 4
| > 	    and v of-type (unsigned-byte 32) in values
| >     do (setf (system:sap-ref-32 (system:int-sap addr) i) v))
| >     (values))
| Speaking of which the type declarations in the loop variable
| really interested me. If fills a whole which declare can't cover.
| But it dosn't seem to work in LspWorks. Is this a SBCL spesific addition?

Nope, it's right in the CLHS:
    6.1.9 Notes about Loop

    Types can be supplied for loop variables. It is not necessary to
    supply a type for any variable, but supplying the type can ensure
    that the variable has a correctly typed initial value, and it can
    also enable compiler optimizations (depending on the implementation). 

I'm using CMUCL, but I suspect SBCL is similar. And CLISP has no
problem with it.

Various sub-forms can take a TYPE-SPEC, though the "for-as-arithmetic"
sub-form is probably the most common usage:
    with-clause::= with var1 [type-spec] [= form1]
		     {and var2 [type-spec] [= form2]}* 
    numeric-accumulation::= {count | counting | sum | summing | } 
			     maximize | maximizing | minimize | minimizing
			     {form | it} [into simple-var] [type-spec] 
    for-as-arithmetic::= var [type-spec] for-as-arithmetic-subclause
    for-as-in-list::= var [type-spec] in form1 [by step-fun] 

    for-as-on-list::= var [type-spec] on form1 [by step-fun] 

    for-as-equals-then::= var [type-spec] = form1 [then form2] 

    for-as-across::= var [type-spec] across vector 

UNSIGNED-BYTE is also standard:

What Lispworks *might* not have is (UNSIGNED-BYTE 32) as a type
you can use in LOOP. Or it might require the optional OF-TYPE in
the FIXNUM declaration: Try replacing the "FOR I FIXNUM ..." with
"FOR I OF-TYPE FIXNUM ...". If that doesn't fix it, I'd file a bug.


p.s.: The SYSTEM:SAP-REF-32 & SYSTEM:INT-SAP functions above
*are* unique to CMUCL, but that wasn't what you asked about.

Rob Warnock			<>
627 26th Avenue			<URL:>
San Mateo, CA 94403		(650)572-2607