How would I loop over the characters in a string of text in Common-lisp?
Here's what I want to do, but in Ruby:
string = "bacon"
string.each_char do |c|
putc c
end
(map nil #'princ "bacon")
or
(loop for c across "bacon" do (princ c))
Looping over a string can be done using loop
like so:
(let ((string "bacon"))
(loop for idex from 0 to (- (length string)) 1)
do
(princ (string (aref string idex)) ) ))
;=> bacon
;=> NIL
To gather up the characters in string
as a list use collect
in the loop instead of do
like so:
(let ((string "bacon"))
(loop for idex from 0 to (- (length string)) 1)
collect
(princ (string (aref string idex)) ) ))
;=> bacon
;=> ("b" "a" "c" "o" "n")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With