This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Loop through strings

3 posts · 7795 views

I need help to find the function which takes two arguments and returns a string with only the characters that are found in exactly one of the strings and with no character repeated. Example: If I give (stringll "goodbye" "oddly"), it shoul return "gbel"

My code works with 3 arguments, I need ur suggestions to do this wih two arguments. Its urgent..
(defun stringll(str1 str2 str3)

               (defparameter *str1* (string str1))

               (defparameter *str2* (string str2))

               (setf s1 str3)

               (delete-duplicates *str1*)

               (delete-duplicates s1)

               (delete-duplicates *str2*)

                (loop for i from 0 to (1-(length s1)) do

                      (if (find (char s1 i) *str2*)

                          (progn(defparameter *temp* (string (find (char s1 i) *str2*)))

                            (delete (char *temp* 0) *str2*)

                            (delete (char *temp* 0) *str1*))))

               (concatenate 'string *str1* *str2*))

Last edited by ramarren on , edited 1 time in total. Reason: Added code tags

Re: Loop through strings

Please use code tags to post code and post in the correct forum. I assume you are asking about Common Lisp, and have moved the topic there.

You seem to be missing basic knowledge about the functioning of the language. You should probably read at least some of Practical Common Lisp or Gentle Introduction to Symbolic Computation.

Some basic mistakes are: defparameter creates global variables, you should establish local bindings with LET, destructive functions like DELETE-DUPLICATES shouldn't really be used for side effects, since they are not actually defined to occur, and you don't really need a loop, just MAP and SET-DIFFERENCE.

Re: Loop through strings

Yeah, your code looks a lot like Scheme in Common Lisp ...
Anyway, maybe what you want is something like this~
(defun make-set (x)
  (cond
   ((endp x) x)
   (t (adjoin (first x) (make-set (rest x))))))

(defun string-set-exclusive-or (lhs rhs)
  (coerce
   (set-exclusive-or (make-set (coerce lhs 'list))
                     (make-set (coerce rhs 'list)))
   'string))