Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top