Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String to list without #\ in common lisp

I'd like to turn String into lists. For example, http => (h t t p).

I try:

(defun string-to-list (s)
  (assert (stringp s) (s) "~s :questa non e una stringa")
  (coerce s 'list))

but if I do

(string-to-list "http")

results:

(#\h #\t #\t #\p).

Can I remove #\ ? thanks in advance :)

like image 288
r1si Avatar asked Jan 30 '12 15:01

r1si


People also ask

How do I convert a string to a list without delimiter in Python?

Method#1: Using split() method If a delimiter is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.

How do I convert a string to a list?

The most straightforward way is to type cast the string into a list. Tyepcasting means to directly convert from one data type to another – in this case from the string data type to the list data type. You do this by using the built-in list() function and passing the given string as the argument to the function.

Can we use string in list?

To create a list of strings, first use square brackets [ and ] to create a list. Then place the list items inside the brackets separated by commas. Remember that strings must be surrounded by quotes. Also remember to use = to store the list in a variable.

Does split turn a string into a list?

The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.


2 Answers

Why would you do that? What you ask is to split a string (a one-dimensional array of characters) into a list of symbols. Do you really want that?

#\h is a character object printed.

You can print them differently:

CL-USER 8 > (princ #\h)
h

CL-USER 9 > (prin1 #\h)
#\h

Let's print the list using PRINC:

CL-USER 10 > (map nil #'princ (coerce "Hello!" 'list))
Hello!

Btw., since strings, vectors and lists are sequences, you can MAP directly over the string...

CL-USER 11 > (map nil #'princ "Hello!")
Hello!
like image 182
Rainer Joswig Avatar answered Dec 01 '22 02:12

Rainer Joswig


You can turn a string into a symbol with intern. You can turn a character into a string with string. Interning a lower-case string might cause it to be printed as |h| instead of h, so you'll want to string-upcase it. Putting all that together gives:

(loop for c in (coerce "http" 'list)
      collecting (intern (string-upcase (string c))))
like image 35
Fred Foo Avatar answered Dec 01 '22 01:12

Fred Foo