10

如何在 Common-lisp 中循环文本字符串中的字符?

这是我想做的,但在 Ruby 中:

string = "bacon"

string.each_char do |c|

    putc c

end
4

2 回答 2

30
(map nil #'princ "bacon")

或者

(loop for c across "bacon" do (princ c))
于 2013-08-05T19:48:42.990 回答
3

可以使用loop如下方式循环遍历字符串:

(let ((string "bacon")) 

   (loop for idex from 0 to (- (length string)) 1)
      do 
         (princ (string (aref string idex)) ) ))

;=> bacon
;=> NIL

要将字符收集string为列表collect,请在循环中使用,而不是do像这样:

(let ((string "bacon")) 

   (loop for idex from 0 to (- (length string)) 1)
      collect 
         (princ (string (aref string idex)) ) ))

;=> bacon
;=> ("b" "a" "c" "o" "n")
于 2013-08-05T19:20:03.937 回答