Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 43 |
Nodes: | 6 (0 / 6) |
Uptime: | 98:07:08 |
Calls: | 290 |
Files: | 905 |
Messages: | 76,483 |
In the C version of my program I have:
for (i=0; i<nchar; i=i+1) {
if (tmp[i]>=0 && tmp[i]<=31) {
dat[i]=tmp[i]+128;}
else if (tmp[i]>=32 && tmp[i]<=63) {
dat[i]=tmp[i];}
else if (tmp[i]>=64 && tmp[i]<=95) {
dat[i]=tmp[i]-64;}
else if (tmp[i]>=128 && tmp[i]<=159) {
dat[i]=tmp[i]+64;}
else if (tmp[i]>=160 && tmp[i]<=191) {
dat[i]=tmp[i]-64;}
else if (tmp[i]>=192 && tmp[i]<=223) {
dat[i]=tmp[i]-128;
}
}
I'd like to achieve the same thing in Common Lisp (Clisp). But I'm
confused by the fact that there doesn't seem to be an elseif
construct. I see there is an if-then-else though.
Part of my problem is likely due to the fact that I'm slavishly
translating the C form into Lisp, when I should really be thinking
in different terms. That is, I probably need to do this in an
entirely new way - one, unfortunately, I cannot see at the moment.
Can anyone give me an idea of how this type of thing should be done in Lisp?
Try COND.
(loop for i from 0 below nchar
for e across tmp do
(cond
((>= 0 e 31) (setf (aref dat i) (+ e 128)))
((>= 32 e 63) (setf (aref dat i) e))
((>= 64 e 95) (setf (aref dat i) (- e 64)))
((>= 128 e 159) (setf (aref dat i) (+ e 64)))
((>= 160 e 191) (setf (aref dat i) (- e 64)))
((>= 192 e 223) (setf (aref dat i) (- e 128)))))
Or even:
(loop for i from 0 below nchar
for e across tmp do
(setf (aref dat i)
(+ e (cond
((>= 0 e 31) 128)
((>= 32 e 63) 0)
((>= 64 e 95) -64)
((>= 128 e 159) 64)
((>= 160 e 191) -64)
((>= 192 e 223) -128)
(loop for i from 0 below nchar
for e across tmp do
(setf (aref dat i)
(+ e (cond
((>= 0 e 31) 128)
((>= 32 e 63) 0)
((>= 64 e 95) -64)
((>= 128 e 159) 64)
((>= 160 e 191) -64)
((>= 192 e 223) -128)
Nice. But the cond is not exhaustive and would return nil for e from 96
to 127 or above 223, so perhaps a final clause is desirable:
= 0 e 31). [In other words, 0 >= e and e >= 31.]Let's see if we can find one.