Loop through strings

Discussion of Common Lisp
Post Reply
Posts: 1
Joined: Thu Feb 16, 2012 5:47 pm

Loop through strings

Post by pinky » Thu Feb 16, 2012 5:54 pm

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*))
Last edited by ramarren on Fri Feb 17, 2012 5:27 am, edited 1 time in total.
Reason: Added code tags

Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland

Re: Loop through strings

Post by ramarren » Fri Feb 17, 2012 5:38 am

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.

Posts: 2
Joined: Sat Jan 21, 2012 3:34 am

Re: Loop through strings

Post by crlf0710 » Sat Feb 18, 2012 7:43 pm

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)
   ((endp x) x)
   (t (adjoin (first x) (make-set (rest x))))))

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

Post Reply