스트링 리스트에서 스트링 찾기(find) with Common Lisp
[파일명: testStringFindInList.lsp]------------------------------------------------
(defun my-find(arr s)
(let ((k -1)
(r -1))
(loop for i from 0 below (length arr) do
(setf k (search s (nth i arr)))
(if k (progn (setf r i) (return i)))
)
r
)
)
(defun printList(arr)
(format t "[")
(loop for i from 0 below (1- (length arr)) do
(format t "~A, " (nth i arr))
)
(if (plusp (length arr))
(format t "~A" (nth (1- (length arr)) arr))
)
(format t "]~%")
)
(setf words (list "하나" "둘" "셋" "넷" "다섯" "여섯"))
(format t "list: ")
(printList words)
(setf w (my-find words "셋"))
(if (>= w 0)
(progn
(format t "발견! ~%")
(format t "Next word of 셋 in list: ~A~%" (nth (1+ w) words))
)
)
(format t "Sorting...~%")
(sort words #'string<)
(format t "list: ")
(printList words)
(setf w (my-find words "셋"))
(if (>= w 0)
(progn
(format t "발견! ~%")
(format t "Next word of 셋 in list: ~A~%" (nth (1+ w) words))
)
)
------------------------------------------------
실행> clisp testStringFindInList.lsp
list: [하나, 둘, 셋, 넷, 다섯, 여섯]
발견! Next word of 셋 in list: 넷
Sorting...
list: [넷, 다섯, 둘, 셋, 여섯, 하나]
발견! Next word of 셋 in list: 여섯