Вопрос

Im learning Common Lisp and writing a simple password generator as an intro project.

Here is my code:

(setq chars
  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
(print (nth (random (length chars)) chars))

But using CLISP I just get

*** - NTH: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" is not a list

I thought every string in Lisp was a list? How can I "cast" the string to a list?

Это было полезно?

Решение

NTH works only for lists. Strings are not lists, but vectors of characters.

Here is the dictionary for strings. CHAR is an accessor for strings.

CL-USER 7 > (char "abc" 1)
#\b

Since strings are also sequences, all sequence operations apply. See: Sequence dictionary.

CL-USER 8 > (elt "abc" 1)
#\b

Другие советы

Lisp is an interactive system. Learn to have conversations with the REPL:

CL-USER> (type-of "abc") (SIMPLE-ARRAY CHARACTER (3))

You should get a similar result from CLISP.

Can you take it from here?

A string is not a list. Not everything is a list in Lisp ;)

You can use coerce to create a list of the characters (coerce "some string" 'list).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top