스트링 벡터에서 스트링 찾기(find) with Common Lisp
[파일명: testStringFindInVector.lsp]------------------------------------------------
#!/usr/bin/env clisp
;; Filename: testStringFindInVector.lsp
;;
;; Approximate square roots, cubic roots and n-th roots of a given number.
;;
;; Execute: clisp testStringFindInVector.lsp
;; Or
;; Execute: ./testStringFindInVector.lsp
;;
;; Date: 2013. 9. 12.
(defun my-exact-find(arr s)
; (position s arr))
(let ((r -1))
(loop for i from 0 below (length arr) do
(if (string= s (aref arr i))
(progn
(setf r i)
(return i))))
r))
(defun printArray(arr)
(format t "[")
(loop for i from 0 below (- (length arr) 1) do
(format t "~A, " (aref arr i)))
(if (plusp (length arr))
(format t "~A" (aref arr (- (length arr) 1))))
(format t "]~%"))
(setf *words* (vector "하나" "둘" "셋" "넷" "다섯" "여섯"))
(setf where (my-exact-find *words* "셋"))
(format t "vector: ")
(printArray *words*)
; (format t "where = ~A~%" where)
(if (and where (>= where 0))
(progn
(format t "발견! ~%")
(if (< where (length *words*))
(format t "Next word of 셋 in vector: ~A~%" (aref *words* (+ where 1))))))
(format t "Sorting...~%")
(sort *words* #'string<)
(setf where (my-exact-find *words* "셋"))
(format t "vector: ")
(printArray *words*)
(if (and where (>= where 0))
(progn
(format t "발견! ~%")
(if (< where (length *words*))
(format t "Next word of 셋 in vector: ~A~%" (aref *words* (+ where 1))))))
#|
Output:
vector: [하나, 둘, 셋, 넷, 다섯, 여섯]
발견!
Next word of 셋 in vector: 넷
Sorting...
vector: [넷, 다섯, 둘, 셋, 여섯, 하나]
발견!
Next word of 셋 in vector: 여섯
|#
------------------------------------------------