Let's start Scheme

2012-04-06

SRFI-4 has been supported

I have wrote a library which supports SRFI-4. This might be the very first library using reader macro from Scheme. (I don't remember if I have already written something before). Well, actually R6RS already has bytevector to handle these homogeneous numeric vectors, however it is sometimes convenient if I can write #s8(-128 127 ...) instead of #vu8(255 127 ...). So now, I can write like this.
#<(srfi :4)>        ;; enable the reader macro
(import (rnrs) (srfi :4))
(define s8 #s8(-128 127)) ;; #s8(...) is reader macro 
(display s8) (newline)    ;; prints #s8(-128 127)
(s8vector-set! s8 0 1)    ;; unspecified
(s8vector-ref s8 0)       ;; returns 1
(s8vector-set! s8 0 -128) ;; unspecified
(s8vector->list s8)    ;; (-128 127)
(list->s8vector '(1 2));; #s8(1 2)
(bytevector? s8)          ;; #f
The homogeneous vectors are not bytevector defined in R6RS. So bytevector? returns #f. Maybe I will write a conversion procedure someday.

The reason why I wanted this is really simple. When I want to read a byte array dumped from Java, it is convenient to be able to write it. (Well, if I dump it, I can convert it to unsigned. But if I peek it from debugger, byte in Java is signed. I really hate this). I actually didn't have any intention to make it a library but when I wrote the reader macro, I thought I will write this again if I don't make the library.

Why is it #s8 instead of #vs8?
Implementation restriction... #vs8 was too long for dispatch macro and if I did it, I also need to rewrite existed bytevector reader.

No comments:

Post a Comment