String to List

Started by netytan, December 30, 2005, 08:42:14 PM

Previous topic - Next topic

netytan

Hi guys,



I'm using Common Lisp and I'm looking for a way to turn a string into a list of characters. I'm sure there must be a function to do this but I just can't find it :(.



I'm currently working on a sorting function however I'm having a few problems sorting strings in the same function, my hope is to covert the string into characters in order to make sorting easier.



Another idea I'm thinking over is to write my own version of min which will work with other types.



Any tips or examples would be very welcome here :).



Thanks guys,



Mark.

Lutz

#1
This forum is about newLISP from http://newlisp.org">http://newlisp.org , which is diffferent from Common Lisp, so the following suggestions may not apply.



In newLISP use 'explode' to turn a string in to a list of characters you could use the built-in function 'sort' to define a 'min' for other types than numbers. See the following interactive session in newLISP:



newLISP v.8.7.1 on OSX, execute 'newlisp -h' for more info.

> (explode "hello")
("h" "e" "l" "l" "o")
> (sort (explode "hello"))
("e" "h" "l" "l" "o")
> (define (my-min s) (first (sort (explode s))))
(lambda (s) (first (sort (explode s))))
> (my-min "world")
"d"
>

Lutz

cormullion

#2
It might be confusing that this area of the forum is labelled as "Lisp in general"? A better place to get help might be the comp.lang.lisp newsgroup. And there are plenty of Common Lisp mailing lists (Google tells me).



Mark, an enjoyable read for you, ifi you haven't already seen it, is http://www.dreamsongs.com/NewFiles/clcrit.pdf">//http://www.dreamsongs.com/NewFiles/clcrit.pdf, which explains (in a strangely prophetic way) why newLiSP is cool! :-)[/url]

netytan

#3
Sorry, I thought that this section was about any Lisp :). I'll refrain from asking such questions in the future.



CL has no (explode) so I've gone ahead and written that, this should allow me to sort strings in the same way I sort nested lists – assuming that I can figure out how to sort nested lists. The sort I've written is actually very simply so the task now about comparing two lists and finding which one comes first.



I'm drawing a blank here so any help would be welcome, any ideas or comments :). Because of the task I'm not able to take advantage of the built-in (sort) so that bit is out :(.



Thanks a lot for the example Lutz it helped more than you know.



I've downloaded the pdf and will read it when I have solved the problem, the reason I decided not to use newLisp originally was because the REPL refuses to work properly on my OSX – when typing code it all has to be on one line, pressing enter causes the form to be executed [usually with errors because the form is incomplete].



Take care guys and thanks again,



Mark.

mwg

#4
Maybe the first item here:



http://cl-cookbook.sourceforge.net/strings.html">http://cl-cookbook.sourceforge.net/strings.html



is what you want.



Regards,

mwg

mwg

#5
Oops, with the following:



(defun explode (string1)

      (let* ((character   (string (char string1 0)))

             (new-string  (subseq string1 1))

             (new-length  (length new-string)))

             (cond ((eql new-length 1) (list character new-string))

                  (t   (cons character (explode new-string))))))



...which leads to....



; SLIME: The Superior Lisp Interaction Mode for Emacs



CL-USER> (explode "This a string")

("T" "h" "i" "s" " " "a" " " "s" "t" "r" "i" "n" "g")

CL-USER>



mwg

netytan

#6
My common lisp versions of explode & implode:


(defun explode (string)
  (map 'list #'character string))

(defun explode2 (string)
  (coerce string 'list))

(defun implode (object)
  (coerce object 'string))


All of these create character arrays :).



Mark.

Ryon

#7
Quote from: "Lutz"This forum is about newLISP from http://newlisp.org">http://newlisp.org

While the main interest of this Fan Club will always be newLISP, we can always discuss other implementations of Lisp. And alternate languages too!



We've talked some about changing the four main headings of this forum, but no consensus has yet been reached. We definitely need to bring in the Mac users who have been banished to the 'General' forum for far too long. But just as soon as that change is made, the hand-held users will feel left out (and the main-framers and the homebrewers too!).



Discussion of alternate ways to solve problems can only be good, and Common Lisp questions will always be welcome here, though I agree with Lutz that they may need a separate topic forum.



Soooo... I'm going to throw this question back to the group. How would you factor the forums to ensure the participation of netytan and all who might show an interest? I'd like to expand the question to welcome discussion from adherents as diverse as: Flatland http://www.geom.uiuc.edu/~banchoff/Flatland/">http://www.geom.uiuc.edu/~banchoff/Flatland/ to Loglan http://www.loglan.org/">http://www.loglan.org/ .
\"Give me a Kaypro 64 and a dial tone, and I can do anything!\"

cormullion

#8
Quote from: "Ryon"How would you factor the forums to ensure the participation of netytan and all who might show an interest?


I've seen other forums where there are too many places to post and to keep track of, so the fewer the better. My factoring would be like this:



- newLISP (general discussion, news, etc)

- Windows-specific issues

- Unix-specific issues

- other LISP-related (not newLISP)



I'm happy to post MacOS issues to the Unix forum, if the Linux folks don't mind putting up with me! :-)

Lutz

#9
Renaming 'newLISP for LINUX" to "newLISP for UNIX" is a good Idea, because that is what it's really is used for. And most of the time issues discussed there cut across all UNIX versions, and yes OSX is right at home in that group and OSX specific issues are welcome there too.



About the group "LISP in general" it has mostly been used and been understood to be for newLISP issues which are not platform specific, and not to accommodate Common Lisp specific issues. There are other groups and forums on the net better suited for Common Lisp or Scheme topics. But this is just my opinion and I am a guest here like any body else.



Lutz

Ryon

#10
That's "Full Member, and Most Appreciated Guest," Lutz!



There is no chance of this site being taken over by any other dialect. I would spin it off into a separate group should that ever occur. And I think that it would be at least entertaining to get some ideas from the http://www.lcarscom.net">LCARS or http://www.catb.org/~esr/intercal/">INTERCAL people, should an expert in either wander in to this group.



I've moved some of the general newLISP discussion into the first forum, in order to broaden the field a bit in the last. Good? Bad?



It's certainly easy enough to change. So, let me know!
\"Give me a Kaypro 64 and a dial tone, and I can do anything!\"