(조금 더 강력한, 아마도) 당신은`setf의 '콜백 번 문자열에서 문자가 호출 될 수 있도록, 또한 쉽게 수정할 수 있지만 didn를 대략가는 또 다른 방법 당신이 이런 종류의 능력을 필요로하지 않는 것처럼 보였기 때문에 그렇게하지 마십시오. 또한 나중에 매크로를 사용하고 싶습니다.
(defun mapc-words (function vector
&aux (whites '(#\Space #\Tab #\Newline #\Rubout)))
"Iterates over string `vector' and calls the `function'
with the non-white characters collected so far.
The white characters are, by default: #\Space, #\Tab
#\Newline and #\Rubout.
`mapc-words' will short-circuit when `function' returns false."
(do ((i 0 (1+ i))
(start 0)
(len 0))
((= i (1+ (length vector))))
(if (or (= i (length vector)) (find (aref vector i) whites))
(if (> len 0)
(if (not (funcall function (subseq vector start i)))
(return-from map-words)
(setf len 0 start (1+ i)))
(incf start))
(incf len))) vector)
(mapc-words
#'(lambda (word)
(not
(format t "word collected: ~s~&" word)))
"a1 b1 c1 d1 e1
a2 b2 c2 d2 e2")
;; word collected: "a1"
;; word collected: "b1"
;; word collected: "c1"
;; word collected: "d1"
;; word collected: "e1"
;; word collected: "a2"
;; word collected: "b2"
;; word collected: "c2"
;; word collected: "d2"
;; word collected: "e2"
다음은 당신이 그것을 읽을 때 문자열을 수정하기를 원한다면 당신이 사용할 수있는 예제 매크로입니다,하지만 난 어쩌면 누군가가 더 나은 변형과 함께 올 것이다, 그것으로 완전히 행복하지 않다.
(defmacro with-words-in-string
((word start end
&aux (whites '(#\Space #\Tab #\Newline #\Rubout)))
s
&body body)
`(do ((,end 0 (1+ ,end))
(,start 0)
(,word)
(len 0))
((= ,end (1+ (length ,s))))
(if (or (= ,end (length ,s)) (find (aref ,s ,end) ',whites))
(if (> len 0)
(progn
(setf ,word (subseq ,s ,start ,end))
,@body
(setf len 0 ,start (1+ ,end)))
(incf ,start))
(incf len))))
(with-words-in-string (word start end)
"a1 b1 c1 d1 e1
a2 b2 c2 d2 e2"
(format t "word: ~s, start: ~s, end: ~s~&" word start end))
지금까지 기록 된 것을 우리에게 보여주십시오 . –
아직 아무것도 쓰지 않았어요. 원래 코드가 Perl로 작성되어있어서 그것을 리습으로 변환해야합니다. 실제로 그것을 수행하는 가장 좋은 방법은 무엇인지 모르겠습니다. 나는 txt 파일을 읽는 대신에 프로그램에 통합하고 필요에 따라 변경하는 것이 더 쉽다고 생각하기 시작했습니다. – Lpaulson