I can't figure out why this is broken.
[360]> (let ((s (make-sequence 'string 15)))
(loop for i across s
do (setf i #\a)
finally (return s)))
" "
[361]>
Help?
--
B.B. --I am not a goat! thegoat4 at airmail.net
Fire the stupid--Vote.
"B.B." <·················@airmail.net.com.org.gov.tw.ch.ru> writes:
> I can't figure out why this is broken.
> [360]> (let ((s (make-sequence 'string 15)))
> (loop for i across s
> do (setf i #\a)
> finally (return s)))
> " "
Perhaps you meant to write
(setf (char s i) #\a)
?
--
Lars Brinkhoff, Services for Unix, Linux, GCC, HTTP
Brinkhoff Consulting http://www.brinkhoff.se/
Lars Brinkhoff <·········@nocrew.org> writes:
> "B.B." <·················@airmail.net.com.org.gov.tw.ch.ru> writes:
> > I can't figure out why this is broken.
> > [360]> (let ((s (make-sequence 'string 15)))
> > (loop for i across s
> > do (setf i #\a)
> > finally (return s)))
> > " "
>
> Perhaps you meant to write
> (setf (char s i) #\a)
No, since the i does not represent the index, but rather the value at
the index
(let ((s "abcd"))
(loop for i across s
collect i))
(#\a #\b #\c #\d)
Petter
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
"B.B." <·················@airmail.net.com.org.gov.tw.ch.ru> writes:
> I can't figure out why this is broken.
>
>
> [360]> (let ((s (make-sequence 'string 15)))
> (loop for i across s
> do (setf i #\a)
> finally (return s)))
> " "
> [361]>
>
> Help?
I think it's because you're extracting the characters of the
string, and overwriting the copies of the characters, instead of
changing the string.
The following works:
(let ((s (make-sequence 'string 15)))
(let ((l (length s)))
(loop for i below l
do (setf (char s i) #\a)
finally (return s))))
--
Raymond Wiker Mail: ·············@fast.no
Senior Software Engineer Web: http://www.fast.no/
Fast Search & Transfer ASA Phone: +47 23 01 11 60
P.O. Box 1677 Vika Fax: +47 35 54 87 99
NO-0120 Oslo, NORWAY Mob: +47 48 01 11 60
Try FAST Search: http://alltheweb.com/
B.B. wrote:
> I can't figure out why this is broken.
>
>
> [360]> (let ((s (make-sequence 'string 15)))
> (loop for i across s
> do (setf i #\a)
> finally (return s)))
> " "
> [361]>
>
> Help?
>
'i' is set to each element of the string (vector), hence the vector is
not modified. This will work
(let ((s (make-string 15 :initial-element #\Space)))
(loop for i from 0 below (length s)
do (setf (char s i) #\a)
finally (return s)))
Alternatively
(let ((s (make-string 15 :initial-element #\Space)))
(map-into s (constantly #\a) s))
Cheers
Marco