Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

lisp - Loop Though Strings

I seem to be stuck trying to loop through strings to find characters that are not in the other string. The goal of the program is to loop though string one and document the characters that are not in the other string. The characters that are not in the other string will be printed out after all the checking is finished. They may not be repeated, hence I attempt to use three loops.

I am trying to debug the code below, since I have to eventually check both strings against each other, and I want to do this manually for the know how.

CG-USER(258): (defun stringprod (string1 string2) 
(let ((newString nil))
(let ((letterSearchOn nil))
(loop for i from 0 below (length string1)
    always
    (setf (letterSearchOn (char string1 i))           
         (loop for j from 0 below (length string2)            
             (for ch =  (char string2 j)
             (/when (find ch letterSearchOn :test #'equal)
             (append newString ch)))))))))
STRINGPROD
CG-USER(260): (stringprod "abc" "abc")
Error: (FOR CH = (CHAR STRING2 J)
    (/WHEN (FIND CH LETTERSEARCHON :TEST #'EQUAL)
     (APPEND NEWSTRING CH))) found where LOOP keyword expected.
Current LOOP context: FOR J FROM 0 BELOW (LENGTH STRING2)
   (FOR CH = (CHAR STRING2 J)
    (/WHEN (FIND CH LETTERSEARCHON :TEST #'EQUAL) (APPEND NEWSTRING CH))).
[condition type: PROGRAM-ERROR]
CG-USER(261): 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How about something like this?

(defun remove-unsafe (str unsafe)
  (remove-duplicates
   (remove-if #'(lambda (c) (find c unsafe)) str)))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...