newLISP Fan Club

Forum => newLISP newS => Topic started by: hsmyers on March 23, 2008, 01:38:34 PM

Title: Character math?
Post by: hsmyers on March 23, 2008, 01:38:34 PM
What is the fastest way to increment/decrement a character? At the moment I'm looking at(define (incr-char c)
(char (+ (char c) 1)))

(define (decr-char c)
(char (- (char c) 1)))


While it works, I've got my doubts--- I'm fairly sure it could be made better. I noticed the code for ++ and I am thinking about the ability to change the original as well as just return char + 1 etc. In assembler this is either a 'inc' or 'dec' on the dereferenced pointer and it bothers me that I don't have something similar. What say you all?



--hsm
Title:
Post by: Lutz on March 23, 2008, 05:51:27 PM
You can do it like this:


(define (inc-char:inc-char init)
    (char (if init
        (set 'inc-char:char init)
        (inc 'inc-char:char))))

> (inc-char 65) ; initialize
"A"
> (inc-char)
"B"
> (inc-char)
"C"
> (inc-char)
"D"
>


This function keeps state in its own namespace, and is also faster.
Title:
Post by: hsmyers on March 23, 2008, 07:09:00 PM
Wow--- I'll have to study this one. Not at all along any lines I had thought about. Cool Lutz, thanks!



--hsm