Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select random char from string in Common Lisp

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?

like image 857
user3633999 Avatar asked Dec 12 '25 15:12

user3633999


1 Answers

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
like image 71
Rainer Joswig Avatar answered Dec 14 '25 11:12

Rainer Joswig