Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

How about something like this?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...