Page 1 of 1

Loop through strings

Posted: Thu Feb 16, 2012 5:54 pm
by pinky
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..

Code: Select all

(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*))

Re: Loop through strings

Posted: Fri Feb 17, 2012 5:38 am
by ramarren
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

Posted: Sat Feb 18, 2012 7:43 pm
by crlf0710
Yeah, your code looks a lot like Scheme in Common Lisp ...
Anyway, maybe what you want is something like this~

Code: Select all

(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))